最后更新于2022年3月22日 11:14:31
CMakeLists.txt
#1.cmake verson,指定cmake版本
cmake_minimum_required(VERSION 3.2)
set(CMAKE_CXX_STANDARD 11)
# set(CMAKE_C_FLAGS -pthread)
# add_compile_options(-D_GNU_SOURCE -DHAVE_CONFIG_H -DNDEBUG -D__XENO__ -D__IN_XENO__ -fasynchronous-unwind-tables -pipe -fstrict-aliasing -Wall -Wstrict-prototypes -Wmissing-prototypes -Wno-long-long -Wno-unused-parameter -Wno-format-truncation -Wformat-security)
# 这里的-g很重要,没有这个就开不了断点调试!!!!
# 这里的-g很重要,没有这个就开不了断点调试!!!!
# 这里的-g很重要,没有这个就开不了断点调试!!!!
# 这里的-g很重要,没有这个就开不了断点调试!!!!
# 这里的-g很重要,没有这个就开不了断点调试!!!!
add_definitions("-Wall -g")
#2.project name,指定项目的名称,一般和项目的文件夹名称对应
PROJECT(jiayu)
find_package(catkin REQUIRED COMPONENTS
roscpp
)
catkin_package(CATKIN_DEPENDS
roscpp
)
#3.head file path,头文件目录
INCLUDE_DIRECTORIES(
${PROJECT_SOURCE_DIR}/include
${catkin_INCLUDE_DIRS}
)
link_directories(
# ${PROJECT_SOURCE_DIR}/lib
${catkin_LIB_DIRS}
)
#4.source directory,源文件目录
AUX_SOURCE_DIRECTORY(. DIR_SRCS)
#5.set environment variable,设置环境变量,编译用到的源文件全部都要放到这里,否则编译能够通过,但是执行的时候会出现各种问题,比如"symbol lookup error xxxxx , undefined symbol"
SET(JIAYU_ENV
${DIR_SRCS}
)
#6.add executable file,添加要编译的可执行文件
ADD_EXECUTABLE(${PROJECT_NAME} ${JIAYU_ENV})
#7.add link library,添加可执行文件所需要的库,比如我们用到了libm.so(命名规则:lib+name+.so),就添加该库的名称
TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${catkin_LIBRARIES})
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": "g++ - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "你的可执行文件的全路径",
"args": [],
"stopAtEntry": true,
"cwd": "你希望执行这个可执行文件时,你的工作路径(常用于代码中需要搜寻某些写死了的特定路径的情况)",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: g++ build active file",
"miDebuggerPath": "/usr/bin/gdb"
}
]
}
task.json无所谓,别让它编译或者捣乱就行,反正我们自己使用Cmake编译,不用VScode搞的这种借助task.json进行g++命令编译的方式。
问题
有个挺奇怪的问题就是,我的include目录的结构是递归的,像这样:
这种情况下会找不到子文件夹里面的头文件,于是我被迫把INCLUDE_DIRECTORIES写成了这样:
INCLUDE_DIRECTORIES(
${PROJECT_SOURCE_DIR}/include
${PROJECT_SOURCE_DIR}/include/chassis
${PROJECT_SOURCE_DIR}/include/enumerations
${PROJECT_SOURCE_DIR}/include/ros_related
${catkin_INCLUDE_DIRS}
)
编译通过了,然后我刚才写这篇文章的时候想起了这个问题,于是我就把那些子文件夹的名字都删除了,变回了原来的样子,也就是:
INCLUDE_DIRECTORIES(
${PROJECT_SOURCE_DIR}/include
${catkin_INCLUDE_DIRS}
)
按理说这样还是会找不到一些头文件,于是我删掉build文件夹,重新cmake ..
居然成功通过了?不知道为什么。ybb