Fix some pthread related linker stuff. (#5504)

The px4_os_add_flags defined in cmake/posix/px4_impl_posix.cmake did
add (threading) libraries to added_exe_linker_flags, which subsequently
end up in CMAKE_EXE_LINKER_FLAGS and then have no effect because those
flags are passed to the linker before any of the object files and static
libraries.

Those libraries are already added correctly in the corresponding
src/firmware/*/CMakeLists.txt files (for qurt, nuttx AND posix).

I left in the non-library linker flag '-pthread' for the bebop board,
although it seems very weird to me that this is needed (is it?).
If that is needed then it seems weird to link manually (that is,
src/firmware/*/CMakeLists.txt) with -lpthread.

For linux/g++ -pthread is added to the CXXFLAGS as it should be:
this causes the compiler to define _REENTRANT which is needed
for (the interface of) certain libraries to become thread-safe.
Offically one also can just pass -pthread to the linker, which then
causes the right libraries to be linked, but just linking with
-lpthread -lm -lrt works too.

I ran into this while adding support for libcwd, which explicitly
complains that _REENTRANT isn't defined when trying to link with
the thread-safe version of libcwd (-lcwd_r) and then tells you
to use -pthread.
This commit is contained in:
Carlo Wood 2016-09-26 22:53:41 +02:00 committed by Lorenz Meier
parent 6d655d2d6e
commit c6a2641507
3 changed files with 35 additions and 18 deletions

View File

@ -389,6 +389,10 @@ endfunction()
# LINK_DIRS : link directories
# DEFINITIONS : definitions
#
# Note that EXE_LINKER_FLAGS is not suitable for adding libraries because
# these flags are added before any of the object files and static libraries.
# Add libraries in src/firmware/nuttx/CMakeLists.txt.
#
# Example:
# px4_os_add_flags(
# C_FLAGS CMAKE_C_FLAGS

View File

@ -109,7 +109,7 @@ endfunction()
#
# px4_os_add_flags
#
# Set ths posix build flags.
# Set the posix build flags.
#
# Usage:
# px4_os_add_flags(
@ -133,6 +133,10 @@ endfunction()
# LINK_DIRS : link directories
# DEFINITIONS : definitions
#
# Note that EXE_LINKER_FLAGS is not suitable for adding libraries because
# these flags are added before any of the object files and static libraries.
# Add libraries in src/firmware/posix/CMakeLists.txt.
#
# Example:
# px4_os_add_flags(
# C_FLAGS CMAKE_C_FLAGS
@ -170,15 +174,17 @@ function(px4_os_add_flags)
mavlink/include/mavlink
)
# Use the pthread instead of lpthread if the firmware is build for the parrot
# bebop. This resolves some linker errors in DriverFramework, when building a
# static target.
# This block sets added_exe_linker_flags.
if ("${BOARD}" STREQUAL "bebop")
set(PX4_PTHREAD_BUILD "-pthread")
# Use the -pthread if the firmware is build for the parrot bebop.
# This resolves some linker errors in DriverFramework, when building
# a static target.
set(added_exe_linker_flags "-pthread")
else()
set(PX4_PTHREAD_BUILD "-lpthread")
set(added_exe_linker_flags)
endif()
# This block sets added_definitions and added_cxx_flags.
if(UNIX AND APPLE)
set(added_definitions
-D__PX4_POSIX
@ -189,9 +195,7 @@ if(UNIX AND APPLE)
-include ${PX4_INCLUDE_DIR}visibility.h
)
set(added_exe_linker_flags
${PX4_PTHREAD_BUILD}
)
set(added_cxx_flags)
else()
@ -204,12 +208,14 @@ else()
-include ${PX4_INCLUDE_DIR}visibility.h
)
set(added_exe_linker_flags
${PX4_PTHREAD_BUILD} -lrt
# Use -pthread For linux/g++.
set(added_cxx_flags
-pthread
)
endif()
# This block sets added_c_flags (appends to others).
if ("${BOARD}" STREQUAL "eagle" OR "${BOARD}" STREQUAL "excelsior")
if ("$ENV{HEXAGON_ARM_SYSROOT}" STREQUAL "")
@ -220,9 +226,13 @@ if ("${BOARD}" STREQUAL "eagle" OR "${BOARD}" STREQUAL "excelsior")
# Add the toolchain specific flags
set(added_cflags ${POSIX_CMAKE_C_FLAGS} --sysroot=${HEXAGON_ARM_SYSROOT})
set(added_cxx_flags ${POSIX_CMAKE_CXX_FLAGS} --sysroot=${HEXAGON_ARM_SYSROOT})
list(APPEND added_exe_linker_flags
list(APPEND added_cxx_flags
${POSIX_CMAKE_CXX_FLAGS}
--sysroot=${HEXAGON_ARM_SYSROOT}
)
list(APPEND added_exe_linker_flags
-Wl,-rpath-link,${HEXAGON_ARM_SYSROOT}/usr/lib/arm-linux-gnueabihf
-Wl,-rpath-link,${HEXAGON_ARM_SYSROOT}/lib/arm-linux-gnueabihf
--sysroot=${HEXAGON_ARM_SYSROOT}
@ -237,20 +247,19 @@ elseif ("${BOARD}" STREQUAL "rpi" AND "$ENV{RPI_USE_CLANG}" STREQUAL "1")
--sysroot=${RPI_TOOLCHAIN_DIR}/gcc-linaro-arm-linux-gnueabihf-raspbian/arm-linux-gnueabihf/libc/)
set(added_c_flags ${POSIX_CMAKE_C_FLAGS} ${clang_added_flags})
set(added_cxx_flags ${POSIX_CMAKE_CXX_FLAGS} ${clang_added_flags})
set(added_exe_linker_flags ${POSIX_CMAKE_EXE_LINKER_FLAGS} ${clang_added_flags})
list(APPEND added_cxx_flags ${POSIX_CMAKE_CXX_FLAGS} ${clang_added_flags})
list(APPEND added_exe_linker_flags ${POSIX_CMAKE_EXE_LINKER_FLAGS} ${clang_added_flags})
else()
# Add the toolchain specific flags
set(added_cflags ${POSIX_CMAKE_C_FLAGS})
set(added_cxx_flags ${POSIX_CMAKE_CXX_FLAGS})
list(APPEND added_cxx_flags ${POSIX_CMAKE_CXX_FLAGS})
endif()
# output
foreach(var ${inout_vars})
string(TOLOWER ${var} lower_var)
set(${${var}} ${${${var}}} ${added_${lower_var}} PARENT_SCOPE)
#message(STATUS "posix: set(${${var}} ${${${var}}} ${added_${lower_var}} PARENT_SCOPE)")
set(${${var}} ${${${var}}} ${added_${lower_var}} PARENT_SCOPE)
endforeach()
endfunction()

View File

@ -126,6 +126,10 @@ endfunction()
# LINK_DIRS : link directories
# DEFINITIONS : definitions
#
# Note that EXE_LINKER_FLAGS is not suitable for adding libraries because
# these flags are added before any of the object files and static libraries.
# Add libraries in src/firmware/qurt/CMakeLists.txt.
#
# Example:
# px4_os_add_flags(
# C_FLAGS CMAKE_C_FLAGS