EDIT
As pointed out in the comments, the issue is actually unrelated to OMP. The compilation failed because the -lm
argument was missing in tasks.json
. It is therefore unrelated to OMP.
Original Question
I have a *.c file (written in C
) that uses omp_get_wtime()
. Therefore, I have #include <omp.h>
in the preamble. Compiling with gcc -o test test.c -Ofast -lm -fopenmp
works. But hitting the 'Debug' button in Visual Studio Code (I'm on Linux) does not build successfully:
* Executing task: C/C : gcc build active file
Starting build...
/usr/bin/gcc -fdiagnostics-color=always -fopenmp -g
/home/[...]/test.c -o /home/[...]/test
/usr/bin/ld: /tmp/ccsQiVzo.o: in function `main':
/home/[...]/test.c:89: undefined reference to `sqrt'
collect2: error: ld returned 1 exit status
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
* Terminal will be reused by tasks, press any key to close it.
(#include <math.h>
is there, so it should know sqrt)
How do I configure VSCode such that it succesfully compiles C
code with gcc and OpenMP?
Thank you so much in advance for your help!
I specified the tasks.json
to use -fopenmp
:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C : gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-fopenmp",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
My c_cpp_properties.json
looks as follows:
{
"configurations": [
{
"name": "Linux",
"includePath": [
"${workspaceFolder}/**",
"/usr/lib/gcc/x86_64-pc-linux-gnu/12.2.0/include/"
],
"defines": [],
"compilerPath": "/usr/bin/gcc",
"cStandard": "c17",
"cppStandard": "c 14",
"intelliSenseMode": "linux-gcc-x64"
}
],
"version": 4
}
The launch.json
is empty:
{
// 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": []
}
CodePudding user response:
The error is from /usr/bin/ld
which is the linker, not the compiler. This means that 1. by including the math.h
header the compiler succeeded, but 2. the linker is missing a library with the definition of your sqrt
function.
Try adding -lm
to the link line, which supplies the math library. Quite often the compiler adds that one on its own, but apparently not in this case.