0

I'm trying to link the math library to the C debugger than comes with visual studio code, GCC. I'm want to mimic what the argument -lm does when I use a makefile. To my understanding I can do this using the launch.json file shown below.

{
    "version": "0.2.0",
    "configurations": [

        {
            "name": "gcc - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}/${fileBasenameNoExtension}",
            "args": ["nums"],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: gcc build active file",
            "miDebuggerPath": "/usr/bin/gdb"
        }
    ]
}

I looked through the documentation https://code.visualstudio.com/docs/cpp/launch-json-reference. The closest thing I could find was "miDebuggerArgs". I tried adding the line "miDebuggerArgs": "-lm" to the "configurations" list, but it didn't work. Any advice?

vs629
  • 1
  • 1
  • 1
    As far as I understand, the above configuration is only for launching a debugger. If you want to build your executable before launching it, you must set the "preLaunchTask" to a task which actually builds it. This might be of help: https://code.visualstudio.com/docs/cpp/config-linux – vmt Dec 02 '20 at 07:30
  • [How do I build and run C files that use math.h functions in VSCode?](https://stackoverflow.com/a/60594654/3422102)?? -- just one more reason I find it easier to add the `/path/to/MinGW/bin` to my user path so I can just open a command prompt and do `gcc -Wall -Wextra -O2 -o my_exe_name my_source.c -lm` (no jacking with `.json` file just to compile C...) – David C. Rankin Dec 02 '20 at 07:31

1 Answers1

1

You posted the launch.json file. Go to the tasks.json and add -lm to the arguments.

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "C/C++: gcc build active file",
            "command": "/usr/bin/gcc",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/bin/${fileBasenameNoExtension}",
                "-lm" 
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": {
                "isDefault": true,
                "kind": "build"
            },
            "detail": "compiler: /usr/bin/gcc"
        }
    ]
}
8thChakra
  • 11
  • 1
  • Thanks @8thChakra , worked like a charm. I was trying to link pthread lib so added "-lpthread" in the args section of tasks.json. – Sudhanshu Gupta Aug 20 '23 at 02:47