Unable to run C program in visual studio code on mac - c

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.

Related

Error no such file or directory in VScode

I'm trying to include header files from the atmel avr folder to work with arduino. Despite trying to include the directory of the files, it still prompts me with "No such file or directory" when compyling. The files are located inside "C:\repositories\arduino_testing\include\avr"
What am I doing wrong?
main.c
#include <stdio.h>
#include "avr\io.h"
int main(){
printf("This is a C code");
return 0;
}
tasks.json
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: g++.exe build active file",
"command": "C:\\MinGW\\bin\\g++.exe",
"args": [
"-I C:\\repositories\\arduino_testing\\include",
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
},
{
"type": "cppbuild",
"label": "C/C++: cpp.exe build active file",
"command": "C:\\MinGW\\bin\\cpp.exe",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
c_cpp_properties
{
"configurations": [
{
"name": "Win32",
"includePath": [
//"${workspaceFolder}/**",
//"C:\\repositories\\arduino_testing\\avr",
"C:\\repositories\\arduino_testing\\include"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE"
],
"windowsSdkVersion": "10.0.18362.0",
"compilerPath": "C:\\MinGW\\bin\\gcc.exe",
"cStandard": "c17",
"cppStandard": "c++17",
"intelliSenseMode": "windows-gcc-x64"
}
],
"version": 4
}
In your c_cpp_properties.json file, you need to add the path to the avr folder in the includePath array.
"includePath": [
//"${workspaceFolder}/**",
"C:\\repositories\\arduino_testing\\include\\avr"
],
#include "avr\io.h"
#include always uses forward slashes / as path separator, no matter what host system you are using.
That system header is part of the GNU tools for AVR and provided by AVR-LibC, hence:
#include <avr/io.h>
Also notice that this is a sytem header, thus enclose in < and >, not in "'s.
There is no need for -isystem <path> (or -I <path> for that matter) as far as the GNU tools are concerned. You might need to provide the headers to IDEs that need their contents to display locations of macro definitions, though.
If you can only compile with -isystem <path> (or -I <path> for that matter) your toolchain installation is broken, and you should get a working toolchain. Same if it doesn't work with -isystem (or -I): toolchain is broken.
So show the include paths, add -v to the compiler options and inspect its output:
> avr-gcc -v ...
[snip many lines]
#include "..." search starts here:
#include <...> search starts here:
$install/bin/../lib/gcc/avr/8.5.0/include
$install/bin/../lib/gcc/avr/8.5.0/include-fixed
$install/bin/../lib/gcc/avr/8.5.0/../../../../avr/include
End of search list.
...
where the last line belongs to the AVR-LibC system includes. Resolving all these ..'s, the path is $install/avr/include, and it should contain a folder avr which contains io.h etc.

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