I'm trying to debug C using visual studio code on a Mint VM, code is as follows:
#include <stdio.h>
int main(int numargs, char* argvector[])
{
printf("test\n");
return(0);
}
Compiled with:
gcc test.c -g -o test
based on the output of ls -l, I can verify that gcc is adding symbols.
When I attempt to debug this program using vs-code using the C/C++ extension, I receive the following error:
Warning: Debuggee TargetArchitecture not detected, assuming x86_64.
=cmd-param-changed,param="pagination",value="off"
Stopped due to shared library event (no libraries added or removed)
Loaded '/lib64/ld-linux-x86-64.so.2'. Symbols loaded.
Breakpoint 1, main (numargs=1, argvector=0x7fffffffdd18) at test.c:5
5 printf("test\n");
[Inferior 1 (process 8322) exited normally]
The program '/home/ccsd/test/test' has exited with code 0 (0x00000000).
gcc version: 5.4.0 20160609
vs-c version: 1.24.1
my launch.json file is as follows:
{
"version": "0.2.0",
"configurations": [
{
"name": "(gdb) Launch",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/grow",
"processName": "grow",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": true,
"MIMode": "gdb",
"setupCommands": [
{
"description": "Enable pretty-printing for gdb",
"text": "-enable-pretty-printing",
"ignoreFailures": true
}
]
}
]
}
You may note that this is not a duplicate of Stopped due to shared library event - Visual Studio Code because I am already using the -g switch.
I would like to know how I can fix this. Thank you in advance.
Assuming the use of "additionalSOLibSearchPath" option of launch.json did not help, the following setting might add a shared library into gdb's consideration:
"setupCommands":[
{
"description": "Additional libs for gdb",
"text": "set solib-search-path sharedLibraryPath/lib"
}
]
PS: gdb may still raise Stopped due to shared library event (no libraries added or removed) warning, nevertheless.
Related
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.
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?
So I'm brand new to the C language and have started taking a look at CSFML and have run into an issue when trying to compile in VS Code this is my tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/Fe:",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"${file}",
"-I${fileDirname}\\CSFML-2.5-windows-64-bit\\include",
"-L${fileDirname}\\CSFML-2.5-windows-64-bit\\lib\\msvc"
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
this is my 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": "cl.exe - Build and debug active file",
"type": "cppvsdbg",
"request": "launch",
"program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"preLaunchTask": "C/C++: cl.exe build active file"
}
]
}
and here is some simple code I'm trying to run to just make a window:
#include <stdio.h>
#include <SFML/Graphics.h>
int main(int argc, char const *argv[])
{
/*24 bit colour depth at 800 by 600 resolution*/
sfVideoMode mode = {800, 600, 24};
sfRenderWindow *window;
/*This will be used in the main loop*/
sfEvent event;
/*Start creating the window*/
window = sfRenderWindow_create(mode, "SFML Test", sfResize | sfClose, NULL);
if (!window)
{
puts("Unable to create window, aborting.");
return 1;
}
/*Main loop*/
while (sfRenderWindow_isOpen(window))
{
/*This processes the event que*/
while (sfRenderWindow_pollEvent(window, &event))
{
if (event.type == sfEvtClosed)
{
puts("Closing render window.");
sfRenderWindow_close(window);
}
}
}
/*Cleanup*/
sfRenderWindow_destroy(window);
return 0;
}
}
but if attempt to run the program I get the following:
> Executing task: C/C++: cl.exe build active file <
Starting build...
Build finished with errors(s):
main.c
Microsoft (R) Incremental Linker Version 14.28.29333.0
Copyright (C) Microsoft Corporation. All rights reserved.
/debug
"/out:C:\Users\Student\OneDrive - Liverpool Hope University\Year 1\Core 2\Guide to Assessments and Policies\Coursework\4-in-a-row\main.exe"
main.obj
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_create referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_destroy referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_close referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_isOpen referenced in function _main
main.obj : error LNK2019: unresolved external symbol __imp__sfRenderWindow_pollEvent referenced in function _main
C:\Users\Student\OneDrive - Liverpool Hope University\Year 1\Core 2\Guide to Assessments and Policies\Coursework\4-in-a-row\main.exe : fatal error LNK1120: 5 unresolved externals
The terminal process terminated with exit code: -1.
Terminal will be reused by tasks, press any key to close it.
As I said I'm very new to C so I'm not really sure what's going on my guess is that the dll's in the CSFML folder need to also be loaded some how but I really don't know
So after many hours of trying to work it out and googling and stackoverflow and discord servers I finally managed to figure it out...
I found the correct commandline args and order but kept having issues as it wouldn't compile for 64 bit but would for x86/32 bit
Since I'm using MSVC the you have to launch VS Code using "Developer Command Prompt for VS 2019" and well turns out that that defaults to an x86 build environment which isn't said in any of the documentation but if u open "x64 Native Tools Command Prompt for VS 2019" instead and open code using that it works using the following tasks.json:
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: cl.exe build active file",
"command": "cl.exe",
"args": [
"/Zi",
"/EHsc",
"/I${fileDirname}/CSFML-2.5-windows-64-bit/include",
"${fileDirname}\\CSFML-2.5-windows-64-bit\\lib\\msvc\\csfml-graphics.lib",
"${file}",
"/link",
"/MACHINE:x64",
],
"options": {
"cwd": "${workspaceFolder}"
},
"problemMatcher": [
"$msCompile"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
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.
I am unable to run my c program in visual studio code! I compiles correctly since the executable is in the same directory as the main.c file I am trying to run. My code is:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
and my tasks.json that did not generate automatically but I had to manually add it via cmd + shift + p and search for tasks
{
// See https://go.microsoft.com/fwlink/?LinkId=733558
// for the documentation about the tasks.json format
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "/usr/bin"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
}
}
]
}
It is the same output from building the program with gcc and clang
> Executing task: /usr/bin/gcc -g '/Users/usrname/Documents/C Programming/main.c' -o '/Users/usrname/Documents/C Programming/main' <
Terminal will be reused by tasks, press any key to close it.
I have installed xcode, both the IDE and the other thingy people recommend installing from terminal "xcode --install" or something like that. I can compile code easily in CLion and from terminal as well as run it, so compiling is obviously not an issue.
The output is blank.
Again this is on mac Mojave latest version running latest version of visual studio code. And yes I have installed the c/c++ extension from the addons page inside the app.