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?

1 Answer 1

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:

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

2 Comments

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.
You might wish to compile a static libc. You might also look at ELF Stratifier

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.