VSCODE+CMAKE+Libtorch environment configuration to realize one-key compilation and one-key DEBUG

VSCODE+CMAKE+Libtorch environment configuration to realize one-key compilation and one-key DEBUG

reference:https://blog.csdn.net/ha_ku/article/details/102625837

 

Article Directory


Record the process of configuring vscode+camke and introducing the third-party deep learning library libtorch to achieve one-click compilation and one-click cmake. First, vscode and cmake, libtorch as an example how to reference third-party libraries. Mainly refer to the official documentation of vscode. If you don’t need to configure cmake but directly call g++, you can directly read the official documentation  C/C++ for Visual Studio Code , but it seems that it is in English. If you find it troublesome, you can see me. The process is the same, but the cmake commands are replaced with g++ commands.

 

Configuring the C++ environment of vscode is mainly to configure three json files, c_cpp_properties.json, tasks.json, and launch.json. The three files control C++ features, compilation options, and DEBUG options. Below we will configure these three files in order, but first create a new hello world.cpp to facilitate our configuration.

1. Create new helloworld.cpp

Open a folder with vscode and create a helloworld.cpp file. This step is mainly to facilitate subsequent configuration files. The beginning of the dream can be output in .cpp:

#include<iostream>

void main()
{
    std::cout << "hello, world!" << std::endl;
}

 

2. Configure the c_cpp_properties.json file

This step is to set various properties of the C++ project in vscode, such as header file search path, pre-definition, etc. Here we mainly refer to the official document C/C++ for Visual Studio Code of vsvode . Here we can use the UI interface or directly modify the json file to configure.

F1 or ctrl+shift+p (mac user is cmd+shift+p ) open the command panel, and enter edit as follows and select C/C++: Edit Configurations (UI) or C/C++: Edit Configurations (JSON)

Insert picture description here

If you choose C/C++: Edit Configurations (JSON), you can skip these two steps and modify your own json file directly according to the final json file. Here is to select **C/C++: Edit Configurations(UI)** as an example, vscode will open the following configuration interface, here we mainly modify the compiler to g++ through the drop-down menu of the compiler path

Insert picture description here

Here we continue to modify the IntelliSense mode to the corresponding platform or what you want to use. The include path introduces all the header files in the workspace by default. Here we can import our third-party library libtorch, such as my libtorch in /home Under /kb457/, kb457 is my user name, I can add a line /home/kb457/libtorch/**. The Defines below is to put some pre-defined values, do not need to fill in here.

Insert picture description here

After the configuration is completed, closing this interface will generate a c_cpp_properties.json in the .vscode folder of the workspace. If you select the **C/C++:Edit Configurations(JSON)** file in the previous step, you can directly modify it according to the following figure That's it.

Insert picture description here

{
    "configurations": [
        {
            "name": "Linux",
            "includePath": [
                "${workspaceFolder}/**",
                "/home/kb457/libtorch/**"
            ],
            "defines": [],
            "compilerPath": "/usr/bin/g++",
            "cStandard": "c11",
            "cppStandard": "c++17",
            "intelliSenseMode": "gcc-x64"
        }
    ],
    "version": 4
}

 

3. Configure the tasks.json file

This file mainly tells vscode what you should do when you press Generate ( ctrl+shift+b / cmd+shift+b ). First, we select and open the helloworld.cpp file we just created. It must be selected here, otherwise vscode cannot know that you need to make more changes to tasks.json if you want to generate cpp. Then we still open the command panel ( F1 / ctrl+shift+p / cmd+shift+p ), enter task , and select Tasks: Configure Default Build Task .

this is important!!!!!!!!!!!!!!!!!!!!!!!!!1111

Insert picture description here

Then select C/C++: g++ build activate file , and vscode will automatically generate tasks.json file using g++ compiled file in the .vscode folder.

Insert picture description here

The generated tasks.json file is as follows, where label can be modified to any name, command is the command to be called, args is the parameter immediately after the command when called, and isDefault in the group is set to true to indicate that it is the default generated task.

Insert picture description here

If you only use g++, you don’t need to modify the file. Because we use cmake, the file is further modified, mainly by modifying the command and args as follows, cmake .. && make -jand at the same time we also need to set the working directory for command execution, modify the cwd to "${workspaceFolder}/build", and at the same time The new build folder in the workspace directory is used to store the generated files, and finally the label is modified to cmake buildmake it easier for us to call the launch.json file later. In this step, we have completed the debugging of the build task. If you don’t need to debug, press Generate vscode and call the cmake command in the build directory for production. However, due to the lack of CMakeLists.txt, the correct file will not be generated. Regarding CMakeLists.txt How to write will be placed at the end, let's continue to talk about how to write the launch.json file.

Insert picture description here

{
// 有关 tasks.json 格式的文档,请参见
    // https://go.microsoft.com/fwlink/?LinkId=733558
    "version": "2.0.0",
    "tasks": [
        {
            "type": "shell",
            "label": "cmake build",
            "command": "cmake",
            "args": [
                "..",
                "&&",
                "make",
                "-j"
            ],
            "options": {
                "cwd": "${workspaceFolder}/build"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            }
        }
    ]
}

 

4. Configure the launch.json file

Also open the command panel, enter debug and select DEBUG:Open launch.json : then select C++ (GDB/LLDB)

Insert picture description here

After selecting, open the launch.json file as follows:

Insert picture description here

Here we mainly modify the file generated by program for cmake. Here we assume it is an example, and add two configurations , preLaunchTask and miDebuggerPath, to tell vscode to call the generation task and the location of the debugger before starting debugging. If you do not add the preLaunchTask configuration, the latest program will not be automatically generated every time we press F5 to debug, and it needs to be generated manually. The modified configuration is as follows:

Insert picture description here

{
    // 使用 IntelliSense 了解相关属性。 
    // 悬停以查看现有属性的描述。
    // 欲了解更多信息,请访问: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [
        {
            "name": "g++ build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${workspaceFolder}/build/stereo",
            "args": ["--dataset=sceneflow"],
            "stopAtEntry": true,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "cmake build",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

 

5. 编写CMakeLists.txt

First give me the CMakeLists.txt I used.

cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(stereo)


# SET(CMAKE_BUILD_TYPE "Debug")
SET(CMAKE_BUILD_TYPE "Release")
SET(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g2 -ggdb")
SET(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

set(CMAKE_PREFIX_PATH ${CMAKE_PREFIX_PATH} "/home/kb457/libtorch")


find_package(Torch REQUIRED)
find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS filesystem system)
# find_package(PythonLibs REQUIRED)

if(NOT Boost_FOUND)
    message("Not found Boost")
endif()


include_directories(${Boost_INCLUDE_DIRS})

add_executable(example helloworld.cpp)



target_link_libraries(stereo "${TORCH_LIBRARIES}" "${OpenCV_LIBS}" "${Boost_LIBRARIES}")
set_property(TARGET stereo PROPERTY CXX_STANDARD 11)

 

Visual Studio Code (VS Code) configuration C/C++ environment common problems and solutions

https://blog.csdn.net/Goo_12138/article/details/83380859

 

Organize: Visual Studio Code (vscode) configure C, C++ environment/write and run C, C++ (mainly Windows, brief Linux)

https://blog.csdn.net/bat67/article/details/76095813

 

In the Visual Studio code configuration C++ operating environment, launch: program'-path-'does not exist

https://blog.csdn.net/YoungXinLer/article/details/81393343?utm_source=blogxgwz8#commentBox 

Guess you like

Origin blog.csdn.net/qq_27009517/article/details/112180982