简体   繁体   English

如何使用 CMake 一次性创建静态和共享库?

[英]How to create both static and shared libraries at one go using CMake?

In my project, static library is created using cmake.在我的项目中,静态库是使用 cmake 创建的。 But now i wanted to change it to create and support both static and shared libraries.但现在我想改变它以创建和支持静态和共享库。

Could someone please help me how to proceed with that?有人可以帮助我如何进行吗?

Below is my existing script for the static library:以下是我现有的静态库脚本:

include(bundle_static_library.cmake)

set(BINARY camsdk)

set(CMAKE_VERBOSE_MAKEFILE ON)

include_directories("../../OpenSource/boost_1_75_0")
if(CMAKE_HOST_WIN32)
add_definitions(-DBOOST_DATE_TIME_NO_LIB)
endif()
include_directories(Include)
include_directories("../Dependencies/CamSupport/h")

file(GLOB_RECURSE SOURCES LIST_DIRECTORIES true *.h *.cpp)
set(SOURCES 
    ${SOURCES}
    ${CMAKE_CURRENT_LIST_DIR}/../CamVisionLibrary/Source/VisionClient.cpp)

if(CMAKE_HOST_UNIX)
    add_compile_options(-fpic -Wall -Werror)
    add_definitions(-DBOOST_UUID_RANDOM_PROVIDER_FORCE_POSIX)
elseif(CMAKE_HOST_WIN32)
    # all warnings as errors
    add_compile_options(/W4 /WX)
    add_definitions(-D_UNICODE -DUNICODE)
endif ()

if(${CMAKE_BUILD_TYPE} STREQUAL "Debug")
    set(BUILD_TYPE "debug")
else()
    set(BUILD_TYPE "release")
endif() 

if(${CMAKE_SYSTEM_NAME} STREQUAL "Darwin")
    set(PLATFORM "macos")
elseif(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    set(PLATFORM "windows")
else()
    set(PLATFORM "linux")
endif()

# We depend on another static library EMR
add_library(EMR STATIC IMPORTED)
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
    if(NOT CMAKE_CL_64)
        message(STATUS "Architecture: x86")
        set_property(TARGET EMR PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/Library/emr/lib/${PLATFORM}-x86-${BUILD_TYPE}-static/testemr-cc.lib)
    else()
        message(STATUS "Architecture: x64")
        set_property(TARGET EMR PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/Library/emr/lib/${PLATFORM}-x64-${BUILD_TYPE}-static/testemr-cc.lib)
    endif()
else()
    set_property(TARGET EMR PROPERTY IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/Library/emr/lib/${PLATFORM}-x64-${BUILD_TYPE}-static/libtestemr-cc.a)
endif()

# First build a temporary library
set(TMP_BINARY camsdk-tmp)
add_library(${TMP_BINARY} STATIC ${SOURCES})
target_link_libraries(${TMP_BINARY} PUBLIC EMR)

bundle_static_library(${TMP_BINARY} ${BINARY})

As I recall, the best way to handle this is with CMake Object libraries.我记得,处理这个问题的最好方法是使用 CMake 对象库。 An object library contains the compiled objects, which you can then wrap with shared and static versions.对象库包含已编译的对象,然后您可以使用共享和静态版本对其进行包装。 This avoids the need to compile twice.这避免了编译两次的需要。 For example:例如:

add_library(something OBJECT lib/something.cpp)
target_include_directories(something PUBLIC lib)

add_library(something_shared SHARED)
target_link_libraries(something_shared PUBLIC something)

add_library(something_static STATIC)
target_link_libraries(something_static PUBLIC something)

In this example, you compile the .cpp once, then you wrap that library with a shared and static version that you can link to.在此示例中,您编译.cpp一次,然后使用可以链接到的共享和静态版本包装该库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM