cmake+vscode编译多个子目录c++文件的源代码
在
1、
2、
的基础上,建立include和src目录:
每个目录的内容如下:
文件内容与:
内容相同,对
CMakeLists.txt进行重构:
cmake_minimum_required ( VERSION 3.17 )
project ( multifiles CXX )
set ( PRJ_SRC_LIST )
set ( PRJ_HEADER_LIST )
set ( PRJ_LIBRARIES )
set ( PRJ_INCLUDE_DIRS )
set ( prj_inc_dir ${CMAKE_CURRENT_SOURCE_DIR}/include )
set ( prj_src_dir ${CMAKE_CURRENT_SOURCE_DIR}/src )
file ( GLOB root_header_files "${prj_inc_dir}/*.h" )
file ( GLOB root_src_files "${prj_src_dir}/*.cpp" )
message ( STATUS " root_header_files = ${root_header_files}")
message ( STATUS " root_src_files = ${root_src_files}")
list ( APPEND PRJ_HEADER_LIST ${root_header_files} )
list ( APPEND PRJ_SRC_LIST ${root_src_files} )
list ( APPEND PRJ_INCLUDE_DIRS ${prj_inc_dir} )
add_executable ( ${PROJECT_NAME} ${PRJ_SRC_LIST} ${PRJ_HEADER_LIST} )
target_include_directories ( ${PROJECT_NAME}
PRIVATE
${PRJ_INCLUDE_DIRS}
target_compile_features ( ${PROJECT_NAME}
PUBLIC
cxx_std_14
target_link_libraries ( ${PROJECT_NAME}
PRIVATE
${PRJ_LIBRARIES}
)
打开vscode,有:
此时vscode自动构建build目录,并配置(configure,generate):
[variant] Loaded new set of variants
[kit] Successfully loaded 14 kits from C:\Users\eric\AppData\Local\CMakeTools\cmake-tools-kits.json
[proc] Executing command: d:\software\mingw64\bin\x86_64-w64-mingw32-gcc.exe -v
[main] Configuring folder: multidirs
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --no-warn-unused-cli -DCMAKE_EXPORT_COMPILE_COMMANDS:BOOL=TRUE -DCMAKE_BUILD_TYPE:STRING=Debug -DCMAKE_C_COMPILER:FILEPATH=d:\software\mingw64\bin\x86_64-w64-mingw32-gcc.exe -DCMAKE_CXX_COMPILER:FILEPATH=d:\software\mingw64\bin\x86_64-w64-mingw32-g++.exe -Hd:/work/cmake_work/multidirs -Bd:/work/cmake_work/multidirs/build -G "MinGW Makefiles"
[cmake] Not searching for unused variables given on the command line.
[cmake] -- The CXX compiler identification is GNU 11.2.0
[cmake] -- Detecting CXX compiler ABI info
[cmake] -- Detecting CXX compiler ABI info - done
[cmake] -- Check for working CXX compiler: d:/software/mingw64/bin/x86_64-w64-mingw32-g++.exe - skipped
[cmake] -- Detecting CXX compile features
[cmake] -- Detecting CXX compile features - done
[cmake] -- root_header_files = D:/work/cmake_work/multidirs/include/foo1.h;D:/work/cmake_work/multidirs/include/foo2.h
[cmake] -- root_src_files = D:/work/cmake_work/multidirs/src/foo1.cpp;D:/work/cmake_work/multidirs/src/foo2.cpp;D:/work/cmake_work/multidirs/src/main.cpp
[cmake] -- Configuring done
[cmake] -- Generating done
[cmake] -- Build files have been written to: D:/work/cmake_work/multidirs/build
build:
[main] Building folder: multidirs
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build d:/work/cmake_work/multidirs/build --config Debug --target all -j 14 --
[build] [ 25%] Building CXX object CMakeFiles/multifiles.dir/src/foo1.cpp.obj
[build] [ 50%] Building CXX object CMakeFiles/multifiles.dir/src/foo2.cpp.obj
[build] [ 75%] Building CXX object CMakeFiles/multifiles.dir/src/main.cpp.obj
[build] [100%] Linking CXX executable multifiles.exe
[build] [100%] Built target multifiles
[build] Build finished with exit code 0
运行:
Microsoft Windows [版本 10.0.19042.985]
(c) Microsoft Corporation。保留所有权利。