vscode:CmakeList编写+编译调试

CMakelist基本写法

# 1.前提
cmake_minimum_required(VERSION 3.2) #最低camke版本需求
PROJECT(test) #创建工程,example为工程名

SET(CMAKE_BUILD_TYPE Debug) #debug模式
#SET(CMAKE_BUILD_TYPE Release) #Release模式

# 2.需要的依赖库
find_package(OpenCV 4.2)
find_package(Eigen3 3.1.0 REQUIRED)
find_package(Pangolin REQUIRED)
find_package(realsense2)

# 3.设置可执行文件与链接库保存的路径
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
set(LIBRARY_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/lib)

# 4.设置头文件目录使得系统可以找到对应的头文件
include_directories(
${PROJECT_SOURCE_DIR}/include  #自己目录的头文件
${EIGEN3_INCLUDE_DIR}          #其他系统目录的头文件 也可以直接写路径/usr/include/eigen3
${Pangolin_INCLUDE_DIRS}
)

# 5.编译动态库并链接库文件
add_library(${PROJECT_NAME} SHARED
src/IMUPreIntergral.cpp
include/IMUPreIntergral.h
)
target_link_libraries(${PROJECT_NAME}
${OpenCV_LIBS}
${EIGEN3_LIBS}
${Pangolin_LIBRARIES}
-lboost_serialization
-lcrypto
)

# 6.生成可执行文件
#选择需要编译的源文件,有几个文件写几个
add_executable(IMUPreIntergral src/IMUPreIntergral.cpp)
target_link_libraries(IMUPreIntergral ${PROJECT_NAME})

#add_executable(IMUPreIntergral src/IMUPreIntergral.cpp)
#target_link_libraries(IMUPreIntergral ${PROJECT_NAME})

编译

命令行编译

mkdir build
cd build
cmake ..
make

图形化编译
上面第二个图标,build all projects

vscode:CmakeList编写+编译调试_第1张图片

调试

修改launch.json文件,修改相应的"program"“args”:部分,理论上每个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}/bin/IMUPreIntergral",//可执行文件的位置
            "args": [],//命令行带的参数
            "stopAtEntry": false,
            "cwd": "${fileDirname}",
            "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
                }
            ]
        }

    ]
}

调试成功!
vscode:CmakeList编写+编译调试_第2张图片

你可能感兴趣的:(vscode,opencv,ide)