我在 Windows 10 下链接 CMake 可执行项目时遇到问题,该项目是共享库主项目的子项目。
所以,我的主要项目是一个共享库。然后我有一个子文件夹,其中有一个可执行项目,它链接到我的库。但我无法成功地将它们联系起来。
如果我将其构建为静态,一切正常。
如何将子文件夹上的可执行子项目链接到主项目上的主共享库?
CMakeLists.txt 的main project
# CMakeList.txt : CMake project for Project
cmake_minimum_required (VERSION 3.8)
set(This Project) # Creates a variable to reference the project's name
project(
${This}
VERSION 0.1.0
LANGUAGES CXX
) # Sets the properties of the project
set(CMAKE_CXX_STANDARD 17) # This project will be build using the C++ 17 standard
set(CMAKE_CXX_EXTENSIONS OFF) # Let's ensure -std=c++xx instead of -std=g++xx
# Directories for headers and source code
file(
GLOB_RECURSE
Headers
./include/Project/header.hpp
Sources
src/source.cpp
)
# Add source to this project's executable.
add_library (${This} STATIC ${Headers} ${Sources})
target_include_directories(${This} PUBLIC "include")
# Target properties
SET_TARGET_PROPERTIES(${This} PROPERTIES CMAKE_BUILD_TYPE Debug)
# Compiler options to enable the inclusion of reflection capabilities into C++
# thanks to the .NET System library of the Microsoft's COMMON LANGUAGE RUNTIME SUPPORT
SET_TARGET_PROPERTIES(${PROJECT_NAME} PROPERTIES COMMON_LANGUAGE_RUNTIME "")#
SET_TARGET_PROPERTIES(${This} PROPERTIES COMPILE_FLAGS "/clr")
STRING(REPLACE "/EHsc" "/EHa" CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
STRING(REPLACE "/RTC1" "" CMAKE_CXX_FLAGS_DEBUG ${CMAKE_CXX_FLAGS_DEBUG})
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /clr")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /c")
enable_testing()
add_subdirectory(subproject)
CMakeLists.txt 的subproject
cmake_minimum_required(
VERSION 3.8
)
set(This Subproject)
project(${This} CXX)
file(
GLOB_RECURSE
Sources
src/subsource.cpp
)
add_executable(${This} ${Sources})
target_compile_features(${This} cxx_std_17)
target_link_libraries(
${This}
PRIVATE
Project
)
编辑:我有这个:
SET(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS YES CACHE BOOL "Export all symbols")
用于导出我的代码上的符号。由于 CRT 库,我有 50 个链接错误,就像这个:
[build] exports.def : warning LNK4002: "[T2M] public: static void __cdecl <CrtImplementationDetails>::AtExitLock::Exit(void)" (__t2m@?Exit@AtExitLock@<CrtImplementationDetails>@@SAXXZ) defined in d:\a01\_work\10\s\Intermediate\vctools\msvcmrt.nativeproj__939063769\objd\x86\msilexit.obj