From aa360e402958ef4eaf0bdc76334196befa3d8ad7 Mon Sep 17 00:00:00 2001 From: Lance Chen Date: Tue, 31 May 2016 14:06:37 +0800 Subject: [PATCH] Add an option to control building static library with PIC or not The option WITH_PIC is default to OFF. By default, the static library is built from the source code directly. If WITH_PIC is enabled, the static library is built with the same set of object libraries that the shared library uses. To link Mosquitto static library into a shared library, one must enable WITH_PIC to make the piece of code locatable. Signed-off-by: Lance Chen --- lib/CMakeLists.txt | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 809d9996..1290f998 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,4 +1,5 @@ option(BUILD_STATIC_LIBRARY "Build the static library?" ON) +option(WITH_PIC "Build the static library with PIC(Position Independent Code) enabled archives?" OFF) add_subdirectory(cpp) option(WITH_THREADING "Include client library threading support?" ON) @@ -80,7 +81,12 @@ if (${WITH_SRV} STREQUAL ON) endif (ARES_HEADER) endif (${WITH_SRV} STREQUAL ON) -add_library(libmosquitto SHARED ${C_SRC} ) +add_library(libmosquitto_obj OBJECT ${C_SRC}) +set_target_properties(libmosquitto_obj PROPERTIES + POSITION_INDEPENDENT_CODE 1 +) + +add_library(libmosquitto SHARED $) target_link_libraries(libmosquitto ${LIBRARIES}) @@ -93,8 +99,11 @@ set_target_properties(libmosquitto PROPERTIES install(TARGETS libmosquitto RUNTIME DESTINATION ${BINDIR} LIBRARY DESTINATION ${LIBDIR}) if (${BUILD_STATIC_LIBRARY} STREQUAL ON) - #target for building static version of library - add_library(libmosquitto_static STATIC ${C_SRC}) + if (${WITH_PIC} STREQUAL OFF) + add_library(libmosquitto_static STATIC ${C_SRC}) + else (${WITH_PIC} STREQUAL OFF) + add_library(libmosquitto_static STATIC $) + endif (${WITH_PIC} STREQUAL OFF) target_link_libraries(libmosquitto_static ${LIBRARIES})