CSFML with VS Code and MSVC cl.exe LNK2019 error - c

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"
}

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?

Visual Studio Code ignores debug symbols

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.

Resources