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 <cyen0312@gmail.com>
pull/179/head
Lance Chen 9 years ago
parent 7ee997e6cc
commit aa360e4029
No known key found for this signature in database
GPG Key ID: 2B9B29056A82FEB1

@ -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_OBJECTS:libmosquitto_obj>)
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 $<TARGET_OBJECTS:libmosquitto_obj>)
endif (${WITH_PIC} STREQUAL OFF)
target_link_libraries(libmosquitto_static ${LIBRARIES})

Loading…
Cancel
Save