目录
开发工具:VS2019、clion、cmake
平台: win
多编译系统,即多个CMakeList.txt,非顶级的构建都属于子构建系统。
- add_subdirectory(source_dir [binary_dir]
- [EXCLUDE_FROM_ALL])
参数详解:
CMakeLists.txt
文件和代码文件。CMakeLists.txt
不会构建子目录的目标文件,必须在子目录下显式去构建。注意:当父目录的目标依赖于子目录的目标,则子目录的目标仍然会被构建出来以满足依赖关系(例如使用了target_link_libraries)例外
。例如:我用 vs 2019 创建的项目
新建项目时选择将解决方案和项目放在同一目录时。
addSubdirectoryTest.h
- #ifndef ADD_SUBDIRECOTRY_TEST_H_
- #define ADD_SUBDIRECOTRY_TEST_H_
-
- void test();
- #endif
-
addSubdirectoryTest.cpp
- #include
- #include "addSubdirectoryTest.h"
-
- void test() {
- std::cout << "子项目测试" << std::endl;
- }
CMakeLists.txt 文件内容:
cmake_minimum_required (VERSION 3.8)
project(addSubdircotryTest)
aux_source_directory(. DIR_MY_LIB_TEST)
add_library(addSubdircotryTest ${DIR_MY_LIB_TEST})
#add_library(addSubdircotryTest addSubdirectoryTest.cpp)
主项目CmakeList.txt 文件:
全部代码:
- # CMakeList.txt: cPulsPlusTestProject 的 CMake 项目,在此处包括源代码并定义
- # 项目特定的逻辑。
- #
- #注意:cmake的语法支持大小、小写和大小写混合上边的代码中我们使用的cmake语法是小写的.
- #project(cPlusPlusTestProject1)
- cmake_minimum_required (VERSION 3.8)
-
- # 设置系统自带变量
- set(cmake_cxx_standard 11)
- #SET( CMAKE_CXX_FLAGS "-std=c++11 -O3")
- if(CAMKE_SYSTEM_NAME STREQUAL "Linux")
- set(CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS} "-std=c++11 -pthread")
- elseif(CMAKE_SYSTEM_NAME MATCHES "Windows")
- #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")
- endif()
- #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -fexec-charset=GBK")
-
- #include_directories(./Header)
- #include_directories(./Header/ThreadTest ./Header/OpteratorOverloadTest)
- #设置头文件搜索路径
- set(myHeadPath "${CMAKE_CURRENT_SOURCE_DIR}/Header")
- include_directories(${myHeadPath})
-
-
- set (cPlusPlusTest_version_major 1.9)
- set (cPlusPlusTest_version_minor 1.5)
- configure_file(
- "${PROJECT_SOURCE_DIR}/cPlusPlusTestConfig.h.in"
- "${PROJECT_BINARY_DIR}/cPlusPlusTestConfig.h"
- )
-
- add_definitions(-DMyDebug)
- include_directories(${PROJECT_BINARY_DIR})
-
- include_directories(${PROJECT_SOURCE_DIR}/3dPart/3dInclude)
-
- link_directories(${PROJECT_SOURCE_DIR}/3dPart/lib)
- link_directories(${PROJECT_SOURCE_DIR}/output)
- #link_libraries()
- #link_directories(../3dPart/lib)
- #link_libraries(${PROJECT_SOURCE_DIR}/3dPart/lib/MyMathFunctions.lib)
- # 将源代码添加到此项目的可执行文件。
-
- # 查找当前目录下的所有源文件
- # 并将名称保存到 DIR_SRCS 变量
- add_subdirectory(subProjectTest ${PROJECT_SOURCE_DIR}/output/lib)
-
- add_executable (cPlusPlusTestProject "Source/cPlusPlusTestProject.cpp" "Header/cPlusPlusTestProject.h" "Header/ThreadTest/multipleThreadTest.h" "Source/ThreadTest/multipleThreadTest.cpp" "Header/ThreadTest/calculateTest.h" "Source/ThreadTest/calculateTest.cpp" "Header/ThreadTest/calculateTest2.h" "Source/ThreadTest/calculateTest2.cpp" "Header/OpteratorOverloadTest/OverloadOperatorTest.h" Source/PerformanceTest/RelevantSizeFuncTest.cpp Header/PerformanceTest/RelevantSizeFuncTest.h)
-
-
- #target_link_directories(cPlusPlusTestProject PUBLIC ${PROJECT_SOURCE_DIR}/3dPart/lib)
- #target_link_libraries(cPlusPlusTestProject ${PROJECT_SOURCE_DIR}/3dPart/lib/libMyMathFunctions.a)
- #target_link_libraries(cPlusPlusTestProject ${PROJECT_SOURCE_DIR}/3dPart/lib/LibTest.lib)
- target_link_libraries(cPlusPlusTestProject MyMathFunctions)
- target_link_libraries(cPlusPlusTestProject addSubdircotryTest)
- #target_link_libraries(cPlusPlusTestProject ${PROJECT_SOURCE_DIR}/3dPart/lib/libMyMathFunctions.dll.a)
- #target_link_libraries(cPlusPlusTestProject ${PROJECT_SOURCE_DIR}/3dPart/lib/libMyMathFunctions.dll)
- # TODO: 如有需要,请添加测试并安装目标。
引入头文件:
运行结果: