目录
2.在c_cpp_properties.json文件中"includePath"里添加头文件路径
另:如果找不到c_cpp_properties.json文件
3.检查CMakeLists.txt文件中是否添加了头文件路径
基本配置文件
点击左侧选项中的 运行和调试,会弹出编译选项,选择g++
根据提示创建以下两个文件
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": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/${fileBasenameNoExtension}.out",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"preLaunchTask": "build",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
tasks.json文件
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0", //该tasks.json文件采用的版本号,目前默认为2.0.0版本
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-std=c++11",
"-o",
"${fileBasenameNoExtension}.out"
],
"problemMatcher": [
"$gcc"
]
}
]
}
CMakeLists.txt文件编写
1.创建build文件夹和CMakeLists.txt文件
2.在CMakeLists.txt文件中写入内容
add_executable里.cpp文件路径一定要注意
include_directories下添加头文件路径
#CMake 要求的至少版本
cmake_minimum_required(VERSION 3.10)
# set the project name and version
project(main VERSION 1.0)
add_definitions(-std=c++17)
#构建的输出类型,executable是app,library是动态库,最后的.cpp 是被构建的文件名
add_executable(Localization src/main.cpp src/preprocessing.cpp)
#add_library(Tu Tutorial.cpp)
#头文件路径
include_directories(
include
${catkin_INCLUDE_DIRS}
)
3.在build文件夹下cmake
打开终端输入如下指令
cd Project/build/
cmake ..
make
头文件路径添加
未添加任何头文件配置时,编译会报如下错误
解决如下:
1.在tasks.json文件中"args"下添加
"-I",
"/home/tzq/Project/include/"
"-I"后面跟的是头文件路径
修改后的tasks.json文件内容如下:
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0", //该tasks.json文件采用的版本号,目前默认为2.0.0版本
"tasks": [
{
"label": "build",
"type": "shell",
"command": "g++",
"args": [
"-g",
"${file}",
"-std=c++11",
"-o",
"${fileBasenameNoExtension}.out",
"-I",
"/home/tzq/Project/include/"
],
"problemMatcher": [
"$gcc"
]
}
]
}
2.在c_cpp_properties.json文件中"includePath"里添加头文件路径
"/home/tzq/Project/include/",
注意如果不是最后一行,要在末尾加逗号
修改后的c_cpp_properties.json文件内容如下:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/include/**",
"/home/tzq/Project/include/",
"/home/tzq/Project/src/"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c11",
"cppStandard": "gnu++14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
另:如果找不到c_cpp_properties.json文件
"Ctrl+shift+p",在命令栏中输入Edit Configurations选择C/C++编辑配置(JSON)选项就可以出现