[C++] Avoid multiple compilations of same source files
@@ -57,8 +57,12 @@ if (WIN32)
string(APPEND LIB_NAME_SHARED dll)
endif()
+# this is the "object library" target: compiles the sources only once
+add_library(PULSAR_OBJECT_LIB OBJECT ${PULSAR_SOURCES})
+set_property(TARGET PULSAR_OBJECT_LIB PROPERTY POSITION_INDEPENDENT_CODE 1)
+
if (BUILD_DYNAMIC_LIB)
- add_library(pulsarShared SHARED ${PULSAR_SOURCES})
+ add_library(pulsarShared SHARED $<TARGET_OBJECTS:PULSAR_OBJECT_LIB>)
set_property(TARGET pulsarShared PROPERTY OUTPUT_NAME ${LIB_NAME_SHARED})
set_property(TARGET pulsarShared PROPERTY VERSION ${LIBRARY_VERSION})
target_link_libraries(pulsarShared ${COMMON_LIBS} ${CMAKE_DL_LIBS})
@@ -78,14 +82,14 @@ if (NOT ${RECORD_OPENSSL_CRYPTO_LIBRARY} MATCHES ".+\\.a$")
endif ()
if (BUILD_DYNAMIC_LIB AND LINK_STATIC)
- add_library(pulsarSharedNossl SHARED ${PULSAR_SOURCES})
+ add_library(pulsarSharedNossl SHARED $<TARGET_OBJECTS:PULSAR_OBJECT_LIB>)
set_property(TARGET pulsarSharedNossl PROPERTY OUTPUT_NAME ${LIB_NAME_SHARED}nossl)
set_property(TARGET pulsarSharedNossl PROPERTY VERSION ${LIBRARY_VERSION})
target_link_libraries(pulsarSharedNossl ${COMMON_LIBS_NOSSL} ${CMAKE_DL_LIBS})
endif()
if (BUILD_STATIC_LIB)
- add_library(pulsarStatic STATIC ${PULSAR_SOURCES})
+ add_library(pulsarStatic STATIC $<TARGET_OBJECTS:PULSAR_OBJECT_LIB>)
set_property(TARGET pulsarStatic PROPERTY OUTPUT_NAME ${LIB_NAME})
set_property(TARGET pulsarStatic PROPERTY VERSION ${LIBRARY_VERSION})
target_compile_definitions(pulsarStatic PRIVATE PULSAR_STATIC)
