您的位置:首页 > 财经 > 金融 > CMake的使用方法

CMake的使用方法

2024/11/19 0:39:32 来源:https://blog.csdn.net/weixin_45947938/article/details/139623213  浏览:    关键词:CMake的使用方法

1 CMakeLists.txt编写

cmake_minimum_required(VERSION 3.12)project(djl_plm)set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -g")add_executable(simple simple.cpp)
add_executable(main main.cpp)include_directories(include)

相当于如下gcc命令:

g++ simple.cpp -std=c++17 -Iinclude -o simple -g

2 Cmake构建方法

根目录下创建build目录

mkdir build

进入build目录

cd build

执行cmake构建命令

cmake ..

执行make编译命令

make

注意:一旦代码被修改了,就需要重新构建

3 VScode 调试方法

task.json配置

{   "version": "2.0.0","options": {"cwd": "${workspaceFolder}/build"},"tasks": [{"type": "shell","label": "cmake","command": "cmake","args": [".."]},{"label": "make","group": {"kind": "build","isDefault": true},"command": "make","args": []},{"label": "Build","dependsOrder": "sequence", // 按列出的顺序执行任务依赖项"dependsOn":["cmake","make"]}]}

launch.json配置

{// Use IntelliSense to learn about possible attributes.// Hover to view descriptions of existing attributes.// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387"version": "0.2.0","configurations": [{"name": "C/C++: g++ build and debug active file","type": "cppdbg","request": "launch","program": "${workspaceFolder}/build/main",  //main:被调试的程序文件"args": [],"stopAtEntry": false,"cwd": "${workspaceFolder}","environment": [],"externalConsole": false,"MIMode": "gdb","miDebuggerPath": "/usr/bin/gdb","setupCommands": [{"description": "Enable pretty-printing for gdb","text": "-enable-pretty-printing","ignoreFailures": true}],"preLaunchTask": "Build"}]
}

优点:按下F5即可调试,并且每次修改代码后不再需要手动cmake编译构建。

项目目录结构如下:

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com