文件结构如图所示。
bin:可执行文件 (这里删掉,可执行文件直接放在build内,2019.10.17)
--------hello1
build:cmake出的文件+可执行文件(hello1) (加入了可执行文件,2019.10.17)
include:头文件
--------add.hpp
--------hello.hpp
lib:生成的库
--------libhellolib.a
src:源文件 source
--------add.cpp
--------CMakeLists.txt
--------hello.cpp
CMakeLists.txt
main.cpp
//main.cpp
#include "include/hello.hpp"
#include "include/add.hpp"
int main(){
helloFun();
addFun(145,6);
return 0;
}
//add.hpp
#ifndef ADD_H
#define ADD_H
#include
void addFun(int x, int y);
#endif
//add.cpp
#include "add.hpp"
void addFun(int x, int y){
printf("%d + %d = %d \n",x,y,x+y);
}
//hello.hpp
#ifndef HELLO_H
#define HELLO_H
#include
void helloFun();
#endif
//hello.cpp
#include "hello.hpp"
void helloFun(){
printf("hello world\n");
}
根目录下的CMakeLists.txt
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
#项目名称
PROJECT(HELLO)
set(CMAKE_C_COMPILER "gcc")
set(CMAKE_CXX_COMPILER "g++")
set(CMAKE_CXX_STANDARD 11)
# 根目录/代码路径
aux_source_directory(. DIR_MAIN)
#dubug 模式------------------这个非常重要,否则无法进入断点
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g")
# 添加子目录
include_directories("${PROJECT_SOURCE_DIR}/include")
add_subdirectory(src)
# 编译成可执行文件
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/build) ###### bin,改成build文件,2019.10.17
add_executable(hello1 ${DIR_MAIN})
# 链接hellolib库,注意下面子目录src的CMakeLists
link_directories("${PROJECT_SOURCE_DIR}/lib")
target_link_libraries(hello1 hellolib)
src下的CMakeLists.txt,生成hellolib库
CMAKE_MINIMUM_REQUIRED(VERSION 2.6)
# generate lib
SET(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib) ######### 增加此行,2019.10.17
aux_source_directory(. DIR_SRC)
add_library(hellolib ${DIR_SRC})
两个 task,第一个 cmake 为使用 cmake 生成指定的 makefile,每次当文件目录结构有更改时需要执行一遍 cmake;第二个 task 为 make。group设置为 true,标识其为一个编译构建任务。在两个task外还需要设置一个options将路径切换至当前工作空间堆build目录下,以在此目录中生成make文件以及编译后的结果,可以将此目录添加到gitignore中。
{
"version": "2.0.0",
"options": {
"cwd": "${workspaceRoot}/build"
},/////////////////////////////////////////////////这个要加上,否则编译出问题
"tasks": [
{
"label": "cmake",
"type": "shell",
"command": "cmake",
"args": [
"-G",
"Unix Makefiles",
"-DCMAKE_BUILD_TYPE=Debug",
".."
]
},
{
"label": "make",/////////////////////// 下面的make就是这里
"group": {
"kind": "build",
"isDefault": true
},
"type": "shell",
"command": "make",
"args": []
}
]
}
program为刚刚在 CMakeLists 中设置的编译生成的程序文件,位置要指定正确;preLaunchTask是每次执行 debug 前要执行的任务,也就是每次debug之前要make一下
{
// 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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/bin/hello1",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
],
"preLaunchTask": "make" //////////////////////// 这个重要,task中的make
}
]
}
断点调试:断点 - F5(启动调试)- F10(逐步调试)
(在F5之前,要提前用命令行cmake make一下)####### 这里有更新,2019.10.17
参考(感谢!):https://ruofeng.me/2018/02/11/debug-c-cpp-in-linux-with-vscode/
https://blog.csdn.net/u014265289/article/details/78213643