初学c++ VS code + CMake 编译调试helloWord,cmakehelloword
分享于 点击 15508 次 点评:268
初学c++ VS code + CMake 编译调试helloWord,cmakehelloword
初学c++ VS code + CMake 编译调试helloWord
准备工作
- 安装 VS code
- 安装CMake
- 给VS code安装插件
- c/c++
- c/c++ clang command adapter
- c++ intellisense
- CMake
- CMake Tools
以上插件有的可能是多余 本人也只是在尝试中
设置CMake
首先安装CMake
打开CMake -> tools -> how to install for command line use
这些显示了三种安装方式 我选择第二种
运行 sudo “/Applications/CMake.app/Contents/bin/cmake-gui” –install
配置VS code CMake
进入首选项 -> 设置 添加CMake 路径
“cmake.cmakePath”: “自己CMake的路径”
我的是: “cmake.cmakePath”: “/Applications/CMake.app/Contents/bin/cmake”
编写程序
- 创建项目文件夹
- 创建main.cpp
- 创建CMakeLists.txt
- 创建build文件夹
main.cpp
#include <iostream>
using namespace std;
int main(int parameter_size, char **parameter)
{
int a = 1 + 3;
int b = a + 3;
cout << "hello word " << parameter_size << " " << endl;
return 0;
}
CMakeLists.txt
#项目名称
project(hello_word)
#代码路径
aux_source_directory(. DIR_TOOT_SRCS)
#dubug 模式
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
#生成可执行的文件
add_executable(hello_word ${DIR_TOOT_SRCS})
在终端进入build文件夹下运行
cmake ../
make
结果如下
fanbindeMBP:build fanbin$ cmake ../
-- The C compiler identification is AppleClang 9.0.0.9000037
-- The CXX compiler identification is AppleClang 9.0.0.9000037
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - failed
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - failed
CMake Warning (dev) in CMakeLists.txt:
No cmake_minimum_required command is present. A line of code such as
cmake_minimum_required(VERSION 3.10)
should be added at the top of the file. The version specified may be lower
if you wish to support older CMake versions for this project. For more
information run "cmake --help-policy CMP0000".
This warning is for project developers. Use -Wno-dev to suppress it.
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/fanbin/Documents/c:c++/helloword/build
fanbindeMBP:build fanbin$ make
Scanning dependencies of target hello_word
[ 50%] Building CXX object CMakeFiles/hello_word.dir/main.o
[100%] Linking CXX executable hello_word
这时build目录下会生成:
hello_word 文件就是可执行文件了
运行
fanbindeMBP:build fanbin$ ./hello_word
hello word 1
以上我们就成功的编译和运行了
断点调试
点击调试
在调试那里 添加配置
会生成launch.json
按照以下修改 其他可以不变
launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "(lldb) hello_word", // 项目名字
"type": "cppdbg",
"request": "launch",
"program": "${workspaceRoot}/helloword/build/hello_word", //修改这里路径指向刚才生成的可执行程序
"args": [],
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceRoot}",
"environment": [],
"externalConsole": true,
"MIMode": "lldb"
}
]
}
运行调试 如图
注意 需要在CMakeLists.txt 里加
set (CMAKE_CXX_FLAGS “${CMAKE_CXX_FLAGS} -g”)
开启debug 不然断点调试是无效的
如需去除dubug 只要去掉此行了
相关文章
- 暂无相关文章
用户点评