Eliya
Eliya

Reputation: 165

Compiling cpp Project with command line But debugging in Vscode

I compile my code with the command line. here is an example of how I compile a program named "out" with 2 libraries a and b

g++   -c -O3 -I../../include/boost_1_61_0 -std=c++14  -MMD  -MP -MF "a.o.d" -o a.o ../../my_Library/lib_a.cpp
g++  -c -O3  -I../../include/boost_1_61_0 -std=c++14  -MMD   -MP -MF  "b.o.d" -o b.o ../../my_Library/lib_b.cpp
g++ -lcurl -g -std=c++11 -lpthread    -o out a.o b.o -L../../include/shared_libraries -no-pie -lsqlite3 -lrt -lpthread -lcurl -fopenmp -lboost_serialization -lconfig++ -lpq -lstdc++ -lz -lboost_thread -lboost_system -lboost_program_options

In the end, I have that files (.o and .od) - a, b, out I want to debug a cpp file and b cpp file with Vscod how can I do this?

I know that I need to edit the tasks.json and launch.json But I don't know-how. I use ubuntu. thanks

I use ubuntu.

thanks

################# EDIT ###################

I did what Krzysztof Mochocki said. and after some minor changes, it works.

First, I remove the optimization flag from compiling line (-O3)

Second, I add -g (debug flag) to the compiling line.

g++   -c -g  -I../../include/boost_1_61_0 -std=c++14  -MMD -MP -MF "a.o.d"  -o a.o my_Librar/a.cpp

The last change is in launch.json in the field "program" I write there the program path, not the .o path

"program": "my/path/a",

And now I can debugging with Vscode.

thank you Krzysztof Mochocki

Upvotes: 0

Views: 185

Answers (1)

Krzysztof Mochocki
Krzysztof Mochocki

Reputation: 188

Here is example of configuration:

{
    "name": "(gdb) Launch my super program with 3 args",
    "type": "cppdbg",
    "request": "launch",
    "program": "/home/user/build/my_program.o",
    "args": ["arg1", "--arg-2", "arg_3"],
    "stopAtEntry": false,
    "cwd": "/home/user/build/workspace",
    "environment": [],
    "externalConsole": false,
    "MIMode": "gdb",
    "setupCommands": [
        {
            "text": "-enable-pretty-printing",
            "ignoreFailures": true
        }
    ]
}

As I see on Your compile commands i am afraid it's not optimal. Try using some cmake, as you will add more files it will be much harder to maintain your compile commands.

Moreover your code is not debuggable, because you didn't add any debug info parameters. If you use cmake, it will add it automaticly after specyfing one parameter: CMAKE_BUILD_TYPE=Debug, so your example cmake for release would look like:

cmake ../src

and for debug:

cmake -DCMAKE_BUILD_TYPE=Debug ../src

Upvotes: 1

Related Questions