为不同文件夹中的源创建静态库 .lib
Create a static library .lib for sources in different folders
VS2015解决方案结构如下:
vs2015解决方案-
-Project-A (googletest source code)
-Project-B
-Folder-1 (with source and header files)
-Folder-2 (with source and header files in 2 sub-folders)
-Folder-3 (with source and header files)
-src (with source files)
-include (with header files)
-项目-C(单元测试)
我需要从 Project-B
和 link 中的所有源构建一个静态库到 Project-C
。我尝试仅针对 Project-B
使用 CMake,但无法正常工作。此外,这会在主 *.sln.
中创建一个 projectB.sln
处理此问题的最佳方法是什么,并确保在我向主解决方案添加新项目时可以稍后扩展? (我坚持使用这种代码设置,因为以前的开发人员以这种方式构建它。)
为什么不对整个项目使用 CMake?假设您的文件结构与 Visual Studio 解决方案资源管理器中显示的类似,它可能看起来像这样:
顶级CMakeLists.txt:
cmake_minimum_required (VERSION 3.16)
# Name your VS solution.
project(MyProject)
enable_testing()
# Add the CMake file in the googletest repo
add_subdirectory(Project-A)
# Add the CMake file that configures your static library.
add_subdirectory(Project-B)
# Add the CMake file that configures your unit tests.
add_subdirectory(Project-C)
CMakeLists.txt Project-B
文件夹中的文件:
# Add the VS project for the static library.
project(MyLibProj)
add_library(MyLib STATIC)
# Add the public/private sources for the static library.
target_sources(MyLib
PUBLIC
Folder-1/MyClass1.cpp
Folder-1/MyClass2.cpp
...
Folder-2/SubFolder1/UtilityClass1.cpp
Folder-2/SubFolder1/UtilityClass2.cpp
...
Folder-2/SubFolder2/HelperClass1.cpp
...
Folder-3/Circle.cpp
Folder-3/Square.cpp
PRIVATE
src/ImplClass1.cpp
src/ImplClass2.cpp
...
)
# Add the include directories for the library.
target_include_directories(MyLib
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/Folder-1
${CMAKE_CURRENT_LIST_DIR}/Folder-2/SubFolder1
${CMAKE_CURRENT_LIST_DIR}/Folder-2/SubFolder2
${CMAKE_CURRENT_LIST_DIR}/Folder-3
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/include
)
CMakeLists.txt Project-C
文件夹中的文件:
# Add the VS project for the executable.
project(MyExeProj)
# Create the test executable, and link the static library to it.
add_executable(MyTestExe main.cpp)
target_link_libraries(MyTestExe PRIVATE MyLib)
add_test(NAME MyTest COMMAND MyTestExe)
VS2015解决方案结构如下:
vs2015解决方案-
-Project-A (googletest source code)
-Project-B-Folder-1 (with source and header files)
-Folder-2 (with source and header files in 2 sub-folders)
-Folder-3 (with source and header files)
-src (with source files)
-include (with header files)-项目-C(单元测试)
我需要从 Project-B
和 link 中的所有源构建一个静态库到 Project-C
。我尝试仅针对 Project-B
使用 CMake,但无法正常工作。此外,这会在主 *.sln.
projectB.sln
处理此问题的最佳方法是什么,并确保在我向主解决方案添加新项目时可以稍后扩展? (我坚持使用这种代码设置,因为以前的开发人员以这种方式构建它。)
为什么不对整个项目使用 CMake?假设您的文件结构与 Visual Studio 解决方案资源管理器中显示的类似,它可能看起来像这样:
顶级CMakeLists.txt:
cmake_minimum_required (VERSION 3.16)
# Name your VS solution.
project(MyProject)
enable_testing()
# Add the CMake file in the googletest repo
add_subdirectory(Project-A)
# Add the CMake file that configures your static library.
add_subdirectory(Project-B)
# Add the CMake file that configures your unit tests.
add_subdirectory(Project-C)
CMakeLists.txt Project-B
文件夹中的文件:
# Add the VS project for the static library.
project(MyLibProj)
add_library(MyLib STATIC)
# Add the public/private sources for the static library.
target_sources(MyLib
PUBLIC
Folder-1/MyClass1.cpp
Folder-1/MyClass2.cpp
...
Folder-2/SubFolder1/UtilityClass1.cpp
Folder-2/SubFolder1/UtilityClass2.cpp
...
Folder-2/SubFolder2/HelperClass1.cpp
...
Folder-3/Circle.cpp
Folder-3/Square.cpp
PRIVATE
src/ImplClass1.cpp
src/ImplClass2.cpp
...
)
# Add the include directories for the library.
target_include_directories(MyLib
PUBLIC
${CMAKE_CURRENT_LIST_DIR}/Folder-1
${CMAKE_CURRENT_LIST_DIR}/Folder-2/SubFolder1
${CMAKE_CURRENT_LIST_DIR}/Folder-2/SubFolder2
${CMAKE_CURRENT_LIST_DIR}/Folder-3
PRIVATE
${CMAKE_CURRENT_LIST_DIR}/include
)
CMakeLists.txt Project-C
文件夹中的文件:
# Add the VS project for the executable.
project(MyExeProj)
# Create the test executable, and link the static library to it.
add_executable(MyTestExe main.cpp)
target_link_libraries(MyTestExe PRIVATE MyLib)
add_test(NAME MyTest COMMAND MyTestExe)