#include <stdio.h>
int main() {
printf("Hello, World!");
return 0;
}
I installed VScode and gcc on Mac Silicon, tried running the above hello world program in C, but I get the following error:
Undefined symbols for architecture arm64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
When I run the following commands on the terminal I get this:
which gcc
/usr/bin/gcc
which gcc-11
/opt/homebrew/bin/gcc-11
I'm not sure if I installed the compiler incorrectly(first time trying C).
For just starting out, I recommend you just have a simple task like this in tasks.json (Terminal -> Configure Tasks):
{
"tasks": [
{
"label": "Run my C",
"type": "shell",
"command": "gcc -o myoutput ${file} && ./myoutput",
"group": {
"kind": "build",
"isDefault": true
},
}
}
],
"version": "2.0.0"
}
This will compile it and immediately run ./myoutput (inside vscode's terminal). isDefault means that it is the default build task so you can run it by Ctrl+Shift+B (also under Terminal -> Run Build Task).
Related
I have been stuck on this bug for a few hours now.
I wrote some code in C that uses functions and structs from another C file that I wrote. when I run the code manually (with the makefile I wrote) it runs.
the makefile:
exec: ../LineParser.c main_c_file.c
gcc -g -m32 -Wall -c ../LineParser.c -o LineParser.o
gcc -g -m32 -Wall -c main_c_file.c -o main_c_file.o
gcc -g -m32 -Wall LineParser.o main_c_file.o -o main_c_file
rm LineParser.o main_c_file.o
but when I am trying to debug it I get the error:
/dir1/dir2/task2/main_c_file.c:123: undefined reference to imported_function'
collect2: error: ld returned 1 exit status
Build finished with error(s).
* The terminal process failed to launch (exit code: -1).
I tried
adding -g to the make file.
to first compile the code
and then add its full path to the program tag in the debug configuration file
{
linkid=830387
"version": "0.2.0",
"configurations": [
{
"type": "by-gdb",
"request": "launch",
"name": "Launch(gdb)",
"program": "/dir1/dir2/main_c_file",
"cwd": "${workspaceRoot}"
}
]
}
by the way I am runing this code in Linux
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.
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.
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!
I have this simple program called main.c:
#include <stdio.h>
int main(){
puts("hi");
puts("bye");
return 0;
}
and this makefile:
run: main.o
gcc main.o -o run
main.o: main.c
gcc -c -g $< -o $#
clean:
rm -f run main.o
Now I want to use it in VS 2017. So I opened the program folder in VS (main.c and the makefile are located there). Then I configured these tasks for it:
{
"version": "0.2.1",
"tasks": [
{
"taskName": "BUILD",
"appliesTo": "/",
"workingDirectory": "${workspaceRoot}",
"command": "make",
"type": "default"
},
{
"taskName": "RUN",
"appliesTo": "/",
"workingDirectory": "${workspaceRoot}",
"command": "run",
"type": "default"
}
]
}
What I tried to do next, is to put break point in the main function and then use the RUN task. I thought VS will stop the running on the break point but that didn't happen and the program run till the end of it.
How can I use the task configuration, so that when I click on RUN - it will stop on the break points that I put?
It's important for me to emphasize that I want to debug the program using the task configuration only and not via other ways that VS have.