4

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.

7
  • 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 Commented Jan 15, 2018 at 2: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. Commented Jan 15, 2018 at 3:16
  • you mean run time debug with controller ? Commented Jan 15, 2018 at 3:22
  • 1
    Possible duplicate of Compiling without libc Commented Jan 15, 2018 at 8:13
  • Maybe this can help... Commented Jan 15, 2018 at 8:50

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

0

simple answer of how to compile a c program without linking in linux (gcc/debian) is using command "gcc -c $CFILE"

in case you don't understand this , execute this script

#!/usr/bin/bash gcc -c $CFILE

then Export $CFILE to your c filename example EXPORT

1 Comment

Not what he asked. Compiling a file without linking does not result in an executable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.