3

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.

Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
Massimo Manca
  • 385
  • 1
  • 2
  • 15
  • You mean you trying to compile for Micro-controller ? if so you need to have supported toolchain package. May be you can you cygwin or mingw to cross compile for micro-controller – ntshetty Jan 15 '18 at 02:18
  • I already can compile for the microcontroller but it is more time consuming. I found a solution adding -nostdlib -lgcc switches to the linker but after that I am not able to debug using gdb, seems it does not stop on any breakpoint. – Massimo Manca Jan 15 '18 at 03:16
  • you mean run time debug with controller ? – ntshetty Jan 15 '18 at 03:22
  • 1
    Possible duplicate of [Compiling without libc](https://stackoverflow.com/questions/2548486/compiling-without-libc) – gj13 Jan 15 '18 at 08:13
  • Maybe [this](https://stackoverflow.com/a/7948630/3545273) can help... – Serge Ballesta Jan 15 '18 at 08:50
  • May [help](https://blogs.oracle.com/ksplice/hello-from-a-libc-free-world-part-1) – Mosaaleb Jan 16 '18 at 00:10

1 Answers1

1

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.

Massimo Manca
  • 385
  • 1
  • 2
  • 15