- 创建目录结构
![image](https://img2023.cnblogs.com/blog/2850002/202302/2850002-20230223151705011-1070928764.png)
- 撰写CMakeLists.txt
点击查看代码
#cmake需求的最小版本,2和3不兼容
#cmake_minimum_required(VERSION 3.1)
# 置工程名称
project(HELLO)
#设置变量 SOURCE 添加 main.cpp到变量
#相对路径
set(SOURCE main.cpp)
#设置编译执行程序,名称位工程名,源代码变量 SOURCE
add_executable(${PROJECT_NAME} ${SOURCE})
#设置编译选项
# -Wall -g 编译调试版本
add_definitions(-std=c++11 -o2 -D__LINUX__ -Wall -g)
#通过 CMAKE_CXX_FLAGS 变量 来设置
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pthread -std=c++11")
message("[LINE:${CMAKE_CURRENT_LIST_LINE}]" ${CMAKE_CXX_FLAGS})
message("[LINE:${CMAKE_CURRENT_LIST_LINE}]" ${PROJECT_NAME})
message("[LINE:${CMAKE_CURRENT_LIST_LINE}]" ${SOURCE})
message("[LINE:${CMAKE_CURRENT_LIST_LINE}]" ${PROJECT_SOURCE_DIR})
message("[LINE:${CMAKE_CURRENT_LIST_LINE}]" ${PROJECT_BINARY_DIR})
message("============include dir=====================")
#绝对路径
include_directories(
${CMAKE_SOURCE_DIR}/core
${CMAKE_SOURCE_DIR}/common
../Library_Source/include
../Library_Source/libSTDF
./include
/opt/gss/poco_1_6/include
)
message("[LINE:${CMAKE_CURRENT_LIST_LINE}]" ${CMAKE_CURRENT_SOURCE_DIR})
link_directories(
${CMAKE_SOURCE_DIR}/libs
/opt/
/proj/
)
get_property(dirs DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY INCLUDE_DIRECTORIES)
foreach(dir ${dirs})
message("[LINE:${CMAKE_CURRENT_LIST_LINE}]" "dir='${dir}'")
endforeach()
add_executable(test main.cpp ${SRC_LIST1} ${SRC_LIST2} ${SRC_LIST3})
target_link_libraries(lib_gzip PocoUtil PocoJSON PocoFoundation PocoXML pthread)
- 下载json.hpp、spd_logger.h、configgr.h
- .vscode添加launch.json
点击查看代码
{
"configurations": [
{
"name": "(gdb) 启动",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/HELLO",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "为 gdb 启用整齐打印",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "将反汇编风格设置为 Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
]
}
]
}
- main.cpp 编码
点击查看代码
#include <iostream>
#include <string>
#include <vector>
#include "spd_logger.h"
int main()
{
std::vector<int> v{10, 10, 10};
std::cout << "hello world" << std::endl;
TRACE(json(v).dump());
return 0;
}
- 编译
cd build;make clean;make
- 执行
![image](https://img2023.cnblogs.com/blog/2850002/202302/2850002-20230223152350383-1295180352.png)