Compiling in C using tasks.json and GCC? - c

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!

Related

why vscode debug doesn't recognize local #include?

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

VSCode compilerPath issue with STM32

So, VSCode was working happily for writing code for my STM microcontroller (STM32WB). I would write the code in VSCode and compile and run in STM32CubeIDE.
Lately, however, VSCode's IntelliSense is acting up. It detects problems such as
identifier "uint8_t" is undefined. Furthermore, the 'Output' tab of VSCode gives the following error:
Unable to resolve configuration with compilerPath: "C:/ST/STM32CubeIDE_1.8.0/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127/tools/bin/arm-none-eabi-gcc.exe"
The path itself appears to be fine. If I issue that command from command prompt, I get
arm-none-eabi-gcc.exe: fatal error: no input files
compilation terminated.
which is the expected response.
Any suggestions on what I can try next? I feel like I'm missing something small!
More info:
I've tried extensively playing around in the c_cpp_properties.json
file.
I've tried downloading another toolchain through STM32CubeIDE and using that path instead.
I've tried adding the path to the Windows environment path.
Here is my c_cpp_properties.json file (most of the settings are copy/paste from what STM32CubeIDE sets up by default:
{
"configurations": [
{
"name": "STM32",
"includePath": [
"${workspaceFolder}/**",
"C:\\ST\\STM32CubeIDE_1.8.0\\STM32CubeIDE\\plugins\\com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127\\tools\\**",
"${workspaceFolder}/Drivers/STM32WBxx_HAL_Driver/Inc",
"${workspaceFolder}/Drivers/CMSIS/Device/ST/STM32WBxx/Include",
"${workspaceFolder}/Drivers/CMSIS/Include",
"${workspaceFolder}/Core/Inc",
"${workspaceFolder}/STM32CubeIDE/Application/Core",
"${workspaceFolder}/STM32CubeIDE/Drivers/STM32WBxx_HAL_Driver",
"${workspaceFolder}/Middlewares/ST/STM32_WPAN/interface/patterns/ble_thread",
"${workspaceFolder}/Middlewares/ST/STM32_WPAN/ble/core",
"${workspaceFolder}/Middlewares/ST/STM32_WPAN/utilities",
"${workspaceFolder}/Middlewares/ST/STM32_WPAN/ble",
"${workspaceFolder}/STM32_WPAN/App",
"${workspaceFolder}/Middlewares/ST/STM32_WPAN/ble/core/template",
"${workspaceFolder}/Middlewares/ST/STM32_WPAN",
"${workspaceFolder}/Utilities/sequencer"
],
"defines": [
"_DEBUG",
"UNICODE",
"_UNICODE",
"USE_HAL_DRIVER",
"DEBUG",
"STM32WB55xx"
],
"compilerPath": "C:/ST/STM32CubeIDE_1.8.0/STM32CubeIDE/plugins/com.st.stm32cube.ide.mcu.externaltools.gnu-tools-for-stm32.10.3-2021.10.win32_1.0.0.202111181127/tools/bin/arm-none-eabi-gcc.exe",
"compilerArgs": [
"-mcpu=cortex-m4 -std=gnu11 -g3 -DUSE_HAL_DRIVER -DUSE_STM32WBXX_NUCLEO -DCORE_CM4 -DSTM32WB55xx -c -I../../Core/Inc -I../../Utilities/lpm/tiny_lpm -I../../Middlewares/ST/STM32_WPAN/interface/patterns/ble_thread/shci -I../../Utilities/sequencer -I../../Drivers/CMSIS/Device/ST/STM32WBxx/Include -I../../Middlewares/ST/STM32_WPAN/interface/patterns/ble_thread -I../../Middlewares/ST/STM32_WPAN/interface/patterns/ble_thread/tl -I../../STM32_WPAN/App -I../../Middlewares/ST/STM32_WPAN/ble/core -I../../Middlewares/ST/STM32_WPAN -I../../Middlewares/ST/STM32_WPAN/ble/core/template -I../../Drivers/BSP/P-NUCLEO-WB55.Nucleo -I../../Drivers/STM32WBxx_HAL_Driver/Inc -I../../Middlewares/ST/STM32_WPAN/ble/core/auto -I../../Middlewares/ST/STM32_WPAN/utilities -I../../Middlewares/ST/STM32_WPAN/ble -I../../Drivers/CMSIS/Include -Os -ffunction-sections -fdata-sections -Wall -fstack-usage --specs=nano.specs -mfpu=fpv4-sp-d16 -mfloat-abi=hard -mthumb"
],
"cStandard": "gnu11",
"cppStandard": "gnu++11",
"intelliSenseMode": "windows-gcc-arm",
}
],
"version": 4
}
The c_cpp_properties.json reference indicates that some fields are optional. Indeed, simply commenting out the optional compilerArgs parameter resolves the issue. At least to a point that IntelliSense is working correctly again. Note that some of the -DXXXX parameters had to be moved to defines (e.g. -DUSE_STM32WBXX_NUCLEO).
It does make some sense, since you are already defining a lot of the compilerArgs elsewhere in the .json file (like defines and includePath).

Why is this hello world program failing to link?

#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).

Compiling in terminal and in VS code with multiple .c files (and including header files correctly). Am I doing this correctly?

I'd like to have some clarifications about compiling and running programs in C. Until now I have mostly used single .c files so now I'm trying to run programs with multiple .c files. I'm on Windows with gcc (mingw).
Let's say that inside the same program folder there are:
file.c (functions' definitions)
header.h (function's prototypes)
main.c
Inside both .c files I have included the header with #include "header.h" .
In the terminal I tried to compile some programs made of multiple .c files like stated above with something like:
gcc file.c main.c -o main
and it created the main.exe file and the program ran correctly but then I tried with another program with the same "structure" and I got error messages that indicated the header file couldn't be found. So I added the header path with the -I command at the end of the command line and again it worked fine. But I don't know why in this case I needed to specific the header path.
Inside VS code I modified the task.json file to compile all the .c files in my workspace (${workspaceFolder}\\*.c) and also added the header path (-I${workspaceFolder}) and it works fine. So the task.json looks like this: (there are some italian phrases and I'm using gcc installed inside CodeBlocks's folder btw).
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc.exe compila il file attivo",
"command": "D:\\CodeBlocks\\MinGW\\bin\\gcc.exe",
"args": [
"-g",
"${workspaceFolder}\\*.c",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-I${workspaceFolder}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": "build",
"detail": "compilatore: D:\\CodeBlocks\\MinGW\\bin\\gcc.exe"
}
]
}
Basically I'd like to know if I am doing this correctly or if there are other/easier ways to do it.
Yes actually, if you have a lot of file, instead manually type them with gcc, you can do a Makefile.
https://github.com/kayofeld/makefile-gen : this is a plugin to generate automatically your Makefile with all the rules and your files in your directory.
But, if you want to learn how to do one by yourself, you can check this site: https://www.cs.swarthmore.edu/~newhall/unixhelp/howto_makefiles.html

VSCode doesnt build and debug pthreads code

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.

Resources