Skip to content

Commit

Permalink
Add an option to control building static library with PIC or not
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
lancechentw committed May 31, 2016
1 parent 7ee997e commit aa360e4
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -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})

Expand All @@ -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})

Expand Down

0 comments on commit aa360e4

Please sign in to comment.