Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC: compiling an application without linking any library

Tags:

c

windows

gcc

libc

I know how to compile a C application without linking any library using GCC in bare metal embedded application just setting up the startup function(s) and eventually the assembly startup.s file.

Instead, I am not able to do the same thing in Windows (I am using MINGW32 GCC). Seems that linking with -nostdlib removes also everything needed to be executed before main, so I should write a specific startup but I did not find any doc about that.

The reason because I need to compile without C std lib is that I am writing a rduced C std lib for little 32 bits microcontrollers and I would like to test and unit test this library using GCC under Windows. So, if there is an alternative simplest way it is OK for me.

Thanks.

like image 556
Massimo Manca Avatar asked Oct 15 '25 17:10

Massimo Manca


1 Answers

I found the solution adding -nostdlib and -lgcc together to ld (or gcc used as linker). In this way the C standard lib is not automatically linked to the application but everything needed to startup the application is linked.

I found also that the order of these switches matters, it may not work at all, signal missing at_exit() function or work without any error/warning depending by the order and position of the options.

I discovered another little complication using Eclipse based IDEs because there are some different approaches in the Settings menu so to write the options in the right order I needed to set them in different places.

After that I had a new problem: I did not think that unit test libraries require at least a function able to write to stdout or to a file.

I found that using "" and <> forces the compiler and linker to use the library modules I want by my library and the C standard library.

So, for instance:

#include "string.h" // points to my library include
#include <stdio.h>  // points to C stdlib include

permits me to test all my library string functions using the C stdlib stdout functions. It works both using GCC and GCC Cross Compilers.

like image 135
Massimo Manca Avatar answered Oct 18 '25 08:10

Massimo Manca



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!