1

When I try to compile static, I am getting the following error:

gcc  defrag.c -o abc.exe --static
/usr/bin/ld: cannot find -lc
collect2: error: ld returned 1 exit status

However, the same thing compiles fine without static:

gcc  defrag.c -o abc.exe 

Question: Why did the compilation failed when static is specified?

Jonathan Leffler
  • 698,132
  • 130
  • 858
  • 1,229
anon
  • 65
  • 5

1 Answers1

3

The error is occurring becuase "--static" says that all subsequent libraries in your link command must be static ... but you only have a dynamic libc on your system.

Recommended solution:

gcc defrag.c -o abc -lc --static -lmystaticlib

If you're just trying to create a static exe for the sake of having a static exe - I'd recommend "don't". Shared libraries are Good. For many different reasons.

Here's a good link:

Community
  • 1
  • 1
paulsm4
  • 107,438
  • 16
  • 129
  • 179
  • Thanks paul. I am building for embedded environment. To save myself from the trouble of having to move all the libraries along with the app, I decided to go with static. – anon Oct 28 '12 at 05:28
  • You might wish to compile a static libc. You might also look at [ELF Stratifier](http://archive09.linux.com/feature/150677) – paulsm4 Oct 28 '12 at 05:40