2、vscode c++ 项目配置调试及运行

文章目录

接上一篇文章,vscode和cmake的c++环境配置好以后,我们要写项目,再写对应的CMakeLists.txt

1、项目布局

shell 复制代码
.
├── bin
├── build
├── CMakeLists.txt
├── pp
├── pp_onnx

详细的如下:

shell 复制代码
.
├── bin
│   ├── pp
│   └── pp_onnx
├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.21.0
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   └── TargetDirectories.txt
│   ├── cmake_install.cmake
│   ├── compile_commands.json
│   ├── Makefile
│   ├── pp
│   │   ├── CMakeFiles
│   │   ├── cmake_install.cmake
│   │   └── Makefile
│   └── pp_onnx
│       ├── CMakeFiles
│       ├── cmake_install.cmake
│       └── Makefile
├── CMakeLists.txt
├── pp
│   ├── CMakeLists.txt
│   └── src
│       ├── main.cpp
│       ├── video.cpp
│       └── video.h
├── pp_onnx
    ├── CMakeLists.txt
    ├── lib
    │   ├── onnxruntime-linux-x64-1.10.0
    │   └── onnxruntime-linux-x64-gpu-1.10.0
    └── src
        ├── main.cpp
        └── img.cpp

2、多项目管理

这里cmake管理了两个项目,有了这个经验,向下变成一个项目或向上变成更多项目也是没问题的。这里是两个项目,pp 和pp_onnx,pp负责读视频,图片前后后理,pp_onnx负责图片推理并返回结果.

2.1 先是一个总的CMakeLists.txt

shell 复制代码
cmake_minimum_required(VERSION 3.5.0)
project(pinpang VERSION 0.1.0)
#c++ std 17 
set(CMAKE_CXX_STANDARD 17) 
message(STATUS "src path " ${CMAKE_SOURCE_DIR})
message(STATUS "binary path " ${CMAKE_BINARY_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY  ${CMAKE_SOURCE_DIR}/bin)

message(STATUS "build pp ")
add_subdirectory(pp)


set(BUILD_PP_ONNX true)
if (BUILD_PP_ONNX)
message(STATUS "build pp_onnx")
add_subdirectory(pp_onnx)
endif()

内容可以看到,可以通过设置参数来选择是否编译,

set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) 这一句设置最终的可执行文件保存位置,

这种设置会把下边管的所有项目的可执行文件都放到这个路径下。

2.2 每个项目

每个项目要有自己的单独的CMakeLists.txt src文件文源文件(.c .cpp .h等),要用到第三方库,还要来个lib.比如pp_onnx

下面是,pp CMakeLIsts.txt内容:

shell 复制代码
# project(pp)
set(CMAKE_CXX_STANDARD 17)
#find required opencv
set(OpenCV_DIR /home/tl/opencv/build/) 
find_package(OpenCV REQUIRED)
message(STATUS "    pp use OpenCV version  is " ${OpenCV_VERSION})
#find_package(OpenCV REQUIRED)
aux_source_directory(src/ SRC_LIST) #找src下边所有的源文件
add_executable(pp  ${SRC_LIST})
#directory of opencv librarys
include_directories(${Opencv_INCLUDE_DIRS})
#opencv librarys
target_link_libraries(pp ${OpenCV_LIBS})
target_link_libraries(pp stdc++fs)
# SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/pp/bin") #可以单独指定
# message(STATUS "beginafdsfasfd src path " ${OpenCV_LIBS})
# set_target_properties(pp PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/pp/bin")

没啥,直接上里边的注释吧

pp_onnx的内容:

shell 复制代码
# project(pp_onnx)
set(CMAKE_CXX_STANDARD 17)
#find required opencv
set(OpenCV_DIR /home/tl/opencv/build/) # this can change which opencv you use
find_package(OpenCV REQUIRED)
message(STATUS "    pp_onnx use OpenCV version  is " ${OpenCV_VERSION})
#onnxruntime cpu or gpu
set(GPU_PP_ONNX true)
if (GPU_PP_ONNX)
message(STATUS "    pp_onnx use gpu.")
set(ONNXRUNTIME_ROOT_PATH ${CMAKE_SOURCE_DIR}/pp_onnx/lib/onnxruntime-linux-x64-gpu-1.10.0)
else()
message(STATUS "    pp_onnx use cpu")
set(ONNXRUNTIME_ROOT_PATH ${CMAKE_SOURCE_DIR}/pp_onnx/lib/onnxruntime-linux-x64-1.10.0)
endif()
set(ONNXRUNTIME_INCLUDE_DIRS ${ONNXRUNTIME_ROOT_PATH}/include/)
set(ONNXRUNTIME_LIB ${ONNXRUNTIME_ROOT_PATH}/lib/libonnxruntime.so)
#set(ONNXRUNTIME_LIB ${ONNXRUNTIME_ROOT_PATH}/lib/)

aux_source_directory(src/ SRC_LIST)

add_executable(pp_onnx  ${SRC_LIST})
#directory of opencv librarys
include_directories(${Opencv_INCLUDE_DIRS})
#opencv librarys
target_link_libraries(pp_onnx ${OpenCV_LIBS})

include_directories(${ONNXRUNTIME_INCLUDE_DIRS})
target_link_libraries(pp_onnx ${ONNXRUNTIME_LIB})
# SET(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/pp_onnx/bin")
# set_target_properties(pp_onnx PROPERTIES RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/pp_onnx/bin")

这个里边多了一个是否使用gpu onnxruntim,这个要看具体的运行环境,来手动设定,当然,main.cpp中也要有对应的配置。

2.3 多版本OPENCV

安装opencv可以多个版本,从源码安装,可以找找我的博客https://blog.csdn.net/u011119817/article/details/100125018。

对于这种,这装时,cmake 配置 -DCMAKE_INSTALL_PREFIX=/path/yourpath,对应的cmake 改一行就行:set(OpenCV_DIR /path/yourpath) ,就行,也就是说这个项目就可以使用你安装的opencv

3、调试和运行

ctrl+shift+p,输入cmake

点击快速入门或生成,就可以自动完成项目配置。

在vscode最下方会生成:

点击 板手图标,会自动显示所有系统自带和自己安装的编译器:

这里有我自己安装的高版本gcc12.3,鼠标点击进行选择。

接着点击生成,生成齿轮和后边中括号里的是一套,可以选择,弹出的选项如图:

这时,会弹出要生成那个项目,如下图:

对于调试小虫子和三角形运行标志,这两后边的中括号,也是选择项目的:

关于调试和运行,我们都使用这里的小虫子和三角,不用vscode自带的,效果是一样的,而且这样不用写配置文件。

点击调试,会在.vscode文件夹中生成setting.json.内容是:

shell 复制代码
{
    "cmake.debugConfig": {
        "args":["jsonfile=/home/tl/ping_pong_referee/pypinpang/data/test/pp_label.json","mode=match", "videofile=/home/tl/ping_pong_referee/pypinpang/data/test/192.168.1.13_20230728161749_CH01_round1_240_320.avi", "outdir=json_result","trajdir=trajectory_result","--log_level","debug"]
        //"args":["jsonfile=/home/tl/ping_pong_referee/pypinpang/data/test/pp_label.json","mode=match", "videofile=/home/tl/ping_pong_referee/pypinpang/data/test/192.168.1.13_20230728161749_CH01_round1_240_320.avi", "outdir=json_result","trajdir=trajectory_result","--log_level","debug","-dv"]
        //"args":["jsonfile=/home/tl/ping_pong_referee/pypinpang/data/test1/pp_label.json","mode=match", "videofile=/home/tl/ping_pong_referee/pypinpang/data/test1/40_2_20230523233534_CH01_729_829.avi", "outdir=json_result","trajdir=trajectory_result","--log_level","debug","-dv"]

        //"args:":["/home/tl/ping_pong_referee/pypinpang/onnx_models/pp_v3_20230620.onnx","0.5","0.5","yellow_testimg.jpg"]
    },

    
    "files.associations": {
        "array": "cpp",
        "atomic": "cpp",
        "bit": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "chrono": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "codecvt": "cpp",
        "complex": "cpp",
        "condition_variable": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "forward_list": "cpp",
        "list": "cpp",
        "map": "cpp",
        "set": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "numeric": "cpp",
        "optional": "cpp",
        "random": "cpp",
        "ratio": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iomanip": "cpp",
        "iosfwd": "cpp",
        "iostream": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "mutex": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "thread": "cpp",
        "cinttypes": "cpp",
        "typeinfo": "cpp",
        "valarray": "cpp",
        "filesystem": "cpp",
        "bitset": "cpp",
        "compare": "cpp",
        "concepts": "cpp",
        "unordered_set": "cpp",
        "numbers": "cpp",
        "semaphore": "cpp",
        "stop_token": "cpp",
        "typeindex": "cpp",
        "*.ipp": "cpp"
    },
    "cmake.sourceDirectory": "/home/tl/ping_pong_referee"
}

cmake.debugConfig 里边是配置程序运行的参数,如果没有参数,那就直接执行了。

"cmake.sourceDirectory" 这个就是项目的根目录。调试,如果main.cpp有断点会停止。

调试时务必要把cmake:后改成debug,运行则无所谓,都可以。点击有四个选项,我只用前两个。

最终的调试为:

相关推荐
21岁被迫秃头1 年前
vscode 调试使用 make 编译的项目
ide·编辑器·makefile·vscode调试