VSCode doesnt build and debug pthreads code - c

I got a few codes from https://computing.llnl.gov/tutorials/pthreads/ and I was trying to use the VSCode debugger to try to step through them but it doesnt seem to work.
Using tasks (ctrl+shift+B) I can build it just fine (I've added the -pthread flag) but when I try to debug it (F5) I get this error:
> Executing task: C/C++: gcc build active file <
Starting build...
Build finished with error:
/usr/bin/ld: /tmp/cc5vG56K.o: in function `main':
/home/xristosp59/Documents/Programming/condvar.c:98: undefined reference to `pthread_create'
/usr/bin/ld: /home/xristosp59/Documents/Programming/condvar.c:99: undefined reference to `pthread_create'
/usr/bin/ld: /home/xristosp59/Documents/Programming/condvar.c:100: undefined reference to `pthread_create'
/usr/bin/ld: /home/xristosp59/Documents/Programming/condvar.c:104: undefined reference to `pthread_join'
collect2: error: ld returned 1 exit status
The terminal process failed to launch (exit code: -1).
I've tried both -pthread and -lpthread flags in various places in my tasks.json but none seem to work, I always get this error.
Here is my current tasks.json: (this builds fine with tasks)
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"-pthread",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Generated task by Debugger"
}
],
"version": "2.0.0"
}
I'm on pop_os 20.10 if that matters.

Ok so apparently vscode, when you first try to debug a c program, it creates a launch.json and tasks.json, the launch.json has a "preLaunchTask": "C/C++: gcc build active file" option and tasks.json has a "label": "C/C++: gcc build active file" option, which match, but I guess because C/C++: gcc build active file is already a task in vscode, it doesn't use the one in tasks (please correct me if I'm wrong). I changed the labels in both and now it works.

Related

Unable to link .lib to VSCode-Undefined reference to `DAQmxCreateTask'

Building a C Code using Visual code studio having a third-party Library file from National Instruments. I have already included header file "NIDAQmx.h" to the code. The library file is still not linked with my code.
Please find my code below:
`#include<stdio.h>
#include"C:\Program Files (x86)\National
Instruments\Shared\ExternalCompilerSupport\C\include\NIDAQmx.h"
TaskHandle taskHandle=0;
int ret=0;
void main()
{
printf("Hello world");
ret=DAQmxCreateTask("task",&taskHandle);
printf("Return for creating task is %d\n",ret);
DAQmxStopTask (taskHandle);
DAQmxClearTask(taskHandle);
printf("Task closed ");
}`
tasks.json file task
given below:
`{
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"test.c",
"-o",
"test.exe",
"-LC:\\Program Files (x86)\\National Instruments\\Shared\\ExternalCompilerSupport\\C\\lib32\\msvc",
"-lNIDAQmx"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}`
When I am running the C Code, I got the Consol Output as follows:
**[Running] cd "c:\Users\User\Documents\my_c\src\" && gcc test.c -o test
&& "c:\Users\User\Documents\my_c\src\"test
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-
w64-mingw32/bin/ld.exe:
C:\Users\User\AppData\Local\Temp\ccKqDitK.o:test.c:(.text+0x82):
undefined reference to `DAQmxCreateTask'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-
w64-mingw32/bin/ld.exe:
C:\Users\User\AppData\Local\Temp\ccKqDitK.o:test.c:(.text+0xae):
undefined reference to `DAQmxStopTask'
c:/mingw/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-
w64-mingw32/bin/ld.exe:
C:\Users\User\AppData\Local\Temp\ccKqDitK.o:test.c:(.text+0xbd):
undefined reference to `DAQmxClearTask'
collect2.exe: error: ld returned 1 exit status
[Done] exited with code=1 in 0.181 seconds**
as suggested by one of the forums i tried a command(mentioned below)specifying file path in the terminal without any error:
'PS D:\VSCODES> gcc test.c -o test -L"C:\Program Files (x86)\National
Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc" -lNIDAQmx'
While debug C/C++ File, I get the following message (VSCode Terminal):
'*** Executing task: C:\MinGW\bin\gcc.exe -g test.c -o test.exe '-
LC:\Program Files (x86)\National
Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc' -lNIDAQmx
c:/mingw/bin/../lib/gcc/x86_64-w64-
mingw32/11.2.0/../../../../x86_64-
w64-mingw32/bin/ld.exe: skipping incompatible C:\Program Files
(x86)\National
Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc/NIDAQmx.lib
when searching for -lNIDAQmx
c:/mingw/bin/../lib/gcc/x86_64-w64-
mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping
incompatible C:\Program Files (x86)\National
Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc/NIDAQmx.lib
when searching for -lNIDAQmx
c:/mingw/bin/../lib/gcc/x86_64-w64-
mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping
incompatible C:\Program Files (x86)\National
Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc\NIDAQmx.lib
when searching for -lNIDAQmx
c:/mingw/bin/../lib/gcc/x86_64-w64-
mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot
find -lNIDAQmx
c:/mingw/bin/../lib/gcc/x86_64-w64-
mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: skipping
incompatible C:\Program Files (x86)\National
Instruments\Shared\ExternalCompilerSupport\C\lib32\msvc/NIDAQmx.lib
when searching for -lNIDAQmx
collect2.exe: error: ld returned 1 exit status**'
Edit the tasks,json file as follows:
{
"tasks": [
{
"type": "shell",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"test.c",
"-o",
"test.exe",
"-LC:\\Program Files (x86)\\National Instruments\\Shared\\ExternalCompilerSupport\\C\\lib64\\msvc",
"-lNIDAQmx"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}

Error linking C code when including SQLite

On Visual Studio Code, Mac OS version 10 I am trying to compile (debug C/C++ file) a C code to connect to SQLite3 database. The compile ends with error :
ld: library not found for -lsqlite clang: error: linker command failed
with exit code 1 (use -v to see invocation)
There is an SQLite3 folder under /usr/lib/ , which includes the SQLite3 library. sqlite3.h I put directly under the same source code file sqlite3test1.c. However the compile failed with an error. I link using this command on terminal :
/usr/bin/clang -lsqlite3 -fcolor-diagnostics -fansi-escape-codes -g /Users/Training/sqlite3test1.c -o /Users/Training/sqlite3test1
The command generated an executable file, run correctly, and the executable file provided the database records on the terminal window ( Success ).
How to make the link without the command?
SQLite is included in macOS and Mac OS X by default. So I think you are asking where to add your -lsqlite3 link parameter in the VSCode right? If so, there is a hidden folder .vscode, inside is a tasks.json:
Open task.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: clang build active file",
"command": "/usr/bin/clang",
"args": [
"-fcolor-diagnostics",
"-fansi-escape-codes",
"-g",
"-l",
"sqlite3",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: /usr/bin/clang"
}
],
"version": "2.0.0"
}
Did you find the sqlite3 command I added? That's it.
Now if you build and run the main.c file, VSCode will compile&link successfully, and you are ready to debug.

Getting an error "the input file is same as the output file

I am very new to C programming and I just installed mingw and started using vs code. I have installed all teh extension required in the VS code and have selected the default builder as gcc in the vs code.
here is the source code I am trying to run
main.c :
#include <stdio.h>
int main(){
int age;
printf("Hello, world!\n");
printf("Whats your age?: ");
scanf("%i", &age);
printf("You are %i years old", age);
return 0;
}
here is the tasks.json which I created:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-g",
"-fdiagnostics-color=always",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
}
]
}
But on building the file (ctrl+shift+B), I get the following error:
> Executing task: C/C++: gcc.exe build active file <
Starting build...
C:\msys64\mingw64\bin\gcc.exe -g -fdiagnostics-color=always C:\Users\user name\Desktop\username's folder laptop\College CS\C programming\main.c -o C:\Users\user name\Desktop\username's folder laptop\College CS\C programming\main.exe
gcc.exe: fatal error: input file 'C:\Users\user' is the same as output file
compilation terminated.
Build finished with error(s).
I have tried deleting and recreating the .c file.
It works fine when I type the following command in the cmd manually after going into that folder:
gcc main.c -o main.exe
So, the gcc works fine but for some reason when I mention the whole path, it goes haywire.
Please help me out as I couldn't find any solutions for this error and sorry of this doubt is a silly one.
I was expecting the build to be successful in VS code but it showed a fatal error even though the VS code is open in that particular folder.

How to set up your visual studio settings to run multiple c files

I am trying to set up Visual Studio Code to function with multiple .c and header files.
I installed the C/C++, Code Rrunner and the C/C++ Makefile Project plug in.
The code works just fine when I compile only one file.
Here are the errors it gives me:
PS C:\Users\rlung\OneDrive\Documenti\VisualStudioCode> cd "c:\Users\rlung\OneDrive\Documenti\VisualStudioCode\src\" ; if ($?) { gcc ex_02.c -o ex_02 } ; if ($?) { .\ex_02 }
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rlung\AppData\Local\Temp\cc0P4dqJ.o:ex_02.c:(.text+0xee): undefined reference to `HTcreate'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rlung\AppData\Local\Temp\cc0P4dqJ.o:ex_02.c:(.text+0x111): undefined reference to `HTinsert'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rlung\AppData\Local\Temp\cc0P4dqJ.o:ex_02.c:(.text+0x152): undefined reference to `HTprintStructure'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rlung\AppData\Local\Temp\cc0P4dqJ.o:ex_02.c:(.text+0x179): undefined reference to `HTprintStructure'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rlung\AppData\Local\Temp\cc0P4dqJ.o:ex_02.c:(.text+0x185): undefined reference to `HTdestroy'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rlung\AppData\Local\Temp\cc0P4dqJ.o:ex_02.c:(.text+0x1ad): undefined reference to `listCreate'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\rlung\AppData\Local\Temp\cc0P4dqJ.o:ex_02.c:(.text+0x2ae): undefined reference to `HTinsert'
collect2.exe: error: ld returned 1 exit status
PS C:\Users\rlung\OneDrive\Documenti\VisualStudioCode\src>
As you can see it says "undefined reference to..." for every function of the other files i use.
I found lots of answers to this problem and tried everything but I still can't manage to make it work.
I am on windows 10 using mingw64 and this are my .json files.
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe build active file",
"command": "C:\\msys64\\mingw64\\bin\\gcc.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${fileDirname}/*.c",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compiler: C:\\msys64\\mingw64\\bin\\gcc.exe"
}
],
"version": "2.0.0"
}
and launch.json
{
// 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": [
{
"name": "gcc.exe - Build and debug active file",
"type": "cppdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${fileDirname}",
"environment": [],
"externalConsole": false,
"MIMode": "gdb",
"miDebuggerPath": "C:\\msys64\\mingw64\\bin\\gdb.exe",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
},
{
"description": "Set Disassembly Flavor to Intel",
"text": "-gdb-set disassembly-flavor intel",
"ignoreFailures": true
}
],
"preLaunchTask": "C/C++: gcc.exe build active file"
}
]
}
I even try to indicate where my header files are in the c_cpp_properties.json.
This is what my Visual Studio Code project looks like:
I tried to change up a bit the tasks.json, in particular the "${file}" string after "-g", but nothing. I also tried compiling and linking everything by hand using: gcc ex_02.c -0 ex_02 -c //for every c file I have and then i linked them togheter using: gcc -o myprog ex_02.o //all object file created before, It works this way but I would like to use code runner and not worry about writing all this stuff everytime, how can i accomplish it?

Compiling in C using tasks.json and GCC?

This is my tasks.json, why is build failing when I try to execute with GCC?
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "C:\\MinGW\\bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
This is the output when I type "gcc helloworld.exe" in terminal
> C:\Users\Administrator\projects\helloworld>gcc helloworld.exe
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: DWARF error: could not find abbrev number 3874
helloworld.exe:cygming-crtbegin.c:(.text+0x290): multiple definition of `_mingw32_init_mainargs'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x290): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.text+0x2d0): multiple definition of `mainCRTStartup'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x2d0): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.text+0x2f0): multiple definition of `WinMainCRTStartup'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x2f0): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.text+0x310): multiple definition of `atexit'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x310): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.text+0x320): multiple definition of `_onexit'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.text+0x320): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.text+0x330): multiple definition of `__gcc_register_frame'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtbegin.o:cygming-crtbegin.c:(.text+0x0): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.text+0x3e0): multiple definition of `__gcc_deregister_frame'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtbegin.o:cygming-crtbegin.c:(.text+0xb0): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.bss+0x4): multiple definition of `_argc'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.bss+0x4): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.bss+0x0): multiple definition of `_argv'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../crt2.o:(.bss+0x0): first defined here
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: helloworld.exe:cygming-crtbegin.c:(.eh_frame+0xc8): multiple definition of `__EH_FRAME_BEGIN__'; c:/mingw/bin/../lib/gcc/mingw32/9.2.0/crtbegin.o:cygming-crtbegin.c:(.eh_frame+0x0): first defined here
collect2.exe: error: ld returned 1 exit status
Solution is in the comments, thank you everyone for your help!
To compile a C program, the command is
gcc -g -o helloworld.exe -Wall helloworld.c
-g Enable debugging
-o helloworld.exe Put the executable in helloworld.exe
-Wall Enable all warnings
helloworld.c Look for the source code in helloworld.c
Your command is attempting to "compile" the file which you expect the compiler to produce. I don't know if that is producing the errors you see, but it certainly is not correct.
Okay I found somewhat of a solution.
I believe I was trying to compile again after already compiling it, when my intentions were to execute the file.
So I switched the code to c and compiled in the terminal with this:
gcc -g -o helloworld.exe -Wall hiworld.c
Then to execute the new exe I used this:
.\helloworld.exe
thanks for the help!

Resources