我有一个导入的库,我试图使用CMake链接。但是,库始终显示为空(或未找到)。我知道导入的库具有与标准库不同的可见性规则(according to this thread),但无论我尝试什么,库总是最终丢失。首先,这是我的文件夹结构:
Root
├── build
├── cmake
├── src
│ ├── external
│ │ ├── openvdb
│ │ │ ├── openvdb
│ │ │ ├── cmake
│ │ │ ├── CMakeLists.txt
│ │ ├── tbb
│ │ ├── zlib
│ │ ├── CMakeLists.txt
│ ├── CMakeLists.txt
├── CMakeLists.txt
在我的 Root CMakeLists.txt 文件中,我添加了名为TBB_LIBS和TBBMALLOC_LIBS的库,如下所示:
cmake_minimum_required(VERSION 3.2.2 FATAL_ERROR)
include(ExternalProject)
project(openVDBBuild)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if (WIN32)
set(CMAKE_CXX_FLAGS "-bigobj /EHsc")
endif ()
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib")
if(NOT CMAKE_DEBUG_POSTFIX)
set(CMAKE_DEBUG_POSTFIX debug)
endif()
if(NOT CMAKE_RELEASE_POSTFIX)
set(CMAKE_RELEASE_POSTFIX release)
endif()
set(BUILD_VARIANTS "release debug")
set(TBB_PREFIX "tbb")
set(TBB_LIBS_DIR "${CMAKE_SOURCE_DIR}/src/external/tbb/build/${TBB_PREFIX}_")
set(TBB_ROOT ${CMAKE_SOURCE_DIR}/src/external/tbb)
set(TBB_INCLUDE_DIR ${TBB_ROOT}/include)
#********** Here I create the library and add the Global Flag ***********
add_library(TBBMALLOC_LIBS SHARED IMPORTED GLOBAL)
add_library(TBB_LIBS SHARED IMPORTED GLOBAL)
if (WIN32)
#********** Here I set different paths for debug and release versions of the library ***********
set_property(TARGET TBBMALLOC_LIBS PROPERTY IMPORTED_LOCATION_RELEASE ${CMAKE_BINARY_DIR}/lib/tbbmalloc.dll )
set_property(TARGET TBBMALLOC_LIBS PROPERTY IMPORTED_LOCATION_DEBUG ${CMAKE_BINARY_DIR}/lib/tbbmalloc_${CMAKE_DEBUG_POSTFIX}.dll )
set_property(TARGET TBB_LIBS PROPERTY IMPORTED_LOCATION_RELEASE ${CMAKE_BINARY_DIR}/lib/tbb.dll )
set_property(TARGET TBB_LIBS PROPERTY IMPORTED_LOCATION_DEBUG ${CMAKE_BINARY_DIR}/lib/tbb_${CMAKE_DEBUG_POSTFIX}.dll )
add_custom_command(
TARGET ProjectTbbExternal
POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy "$<$<CONFIG:debug>:${TBB_LIBS_DIR}${CMAKE_DEBUG_POSTFIX}/tbb_${CMAKE_DEBUG_POSTFIX}.dll>$<$<CONFIG:release>:${TBB_LIBS_DIR}${CMAKE_RELEASE_POSTFIX}/tbb.dll>" "${CMAKE_BINARY_DIR}/lib"
COMMAND ${CMAKE_COMMAND} -E copy "$<$<CONFIG:debug>:${TBB_LIBS_DIR}${CMAKE_DEBUG_POSTFIX}/tbbmalloc_${CMAKE_DEBUG_POSTFIX}.dll>$<$<CONFIG:release>:${TBB_LIBS_DIR}${CMAKE_RELEASE_POSTFIX}/tbbmalloc.dll>" "${CMAKE_BINARY_DIR}/lib"
COMMAND ${CMAKE_COMMAND} -E copy "$<$<CONFIG:debug>:${TBB_LIBS_DIR}${CMAKE_DEBUG_POSTFIX}/tbbmalloc_proxy_${CMAKE_DEBUG_POSTFIX}.dll>$<$<CONFIG:release>:${TBB_LIBS_DIR}${CMAKE_RELEASE_POSTFIX}/tbbmalloc_proxy.dll>" "${CMAKE_BINARY_DIR}/lib"
)
endif ()
add_subdirectory("src")
现在,在src / external / openvdb 目录中的 CMakeLists.txt内部,我尝试链接这样的库:
project(OpenVDBCore)
#define OPENVDB_LIBRARY_MAJOR_VERSION_NUMBER 4
#define OPENVDB_LIBRARY_MINOR_VERSION_NUMBER 0
#define OPENVDB_LIBRARY_PATCH_VERSION_NUMBER 0
#*********** I'm going to omit some lines here since **********
#*********** they're standard for the OpenVDB build ***********
SET_SOURCE_FILES_PROPERTIES ( ${OPENVDB_LIBRARY_SOURCE_FILES}
PROPERTIES
COMPILE_FLAGS "-DOPENVDB_PRIVATE -DOPENVDB_USE_BLOSC ${OPENVDB_USE_GLFW_FLAG}"
)
ADD_LIBRARY ( openvdb_static STATIC ${OPENVDB_LIBRARY_SOURCE_FILES} )
ADD_LIBRARY ( openvdb_shared SHARED ${OPENVDB_LIBRARY_SOURCE_FILES} )
TARGET_LINK_LIBRARIES ( openvdb_static
zlib
TBB_LIBS
TBBMALLOC_LIBS
)
TARGET_LINK_LIBRARIES ( openvdb_shared
zlib
TBB_LIBS
TBBMALLOC_LIBS
)
MESSAGE(STATUS "TBB_LIBS library location is ${TBB_LIBS}")
MESSAGE(STATUS "TBBMALLOC_LIBS library location is ${TBBMALLOC_LIBS}")
您可以看到我在CMake中放置状态消息,以查看$ {TBB_LIBS}和$ {TBBMALLOC_LIBS}变量是什么......但该部分始终为空。因此,由CMake生成的openvdb_shared和openvdb_static项目文件也不能正确链接库(因为这些库是未知的)。有谁知道为什么会发生这种情况(以及如何解决)?