初学c++ VS code + CMake 编译调试helloWord

来源:互联网 发布:上下课铃声软件 编辑:程序博客网 时间:2024/05/16 14:39

初学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 - failedCMake 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/buildfanbindeMBP:build fanbin$ makeScanning 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_wordhello 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 只要去掉此行了