Linux CMake工程目录结构

主文件目录

1
2
3
4
./lib/
./src/
./build
./CMakeLists.txt

./CMakeLists.txt

1
2
3
4
5
6
7
8
CMAKE_MINIMUM_REQUIRED( VERSION 3.10 )

PROJECT(MAIN)

ADD_SUBDIRECTORY(./lib )

ADD_SUBDIRECTORY(./src)

./lib/CMakeLists.txt

1
2
3
4
5
AUX_SOURCE_DIRECTORY(. DIR_LIB_SRCS)

SET(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

ADD_LIBRARY(LIB STATIC ${DIR_LIB_SRCS})

./src/CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
INCLUDE_DIRECTORIES(${PROJECT_SOURCE_DIR}/lib)

SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

AUX_SOURCE_DIRECTORY(./ DIR_SRCS)

ADD_EXECUTABLE(MAIN ${DIR_SRCS})

TARGET_LINK_LIBRARIES(MAIN LIB)


vscode调试

./CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
CMAKE_MINIMUM_REQUIRED( VERSION 3.10 )

PROJECT(MAIN)

add_definitions(-std=c++14)
SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_CXX_FLAG_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
SET(CMAKE_CXX_FLAG_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

ADD_SUBDIRECTORY(./lib )

ADD_SUBDIRECTORY(./src)

修改launch.json

1
"program": "${workspaceFolder}/build/bin/MAIN"