Search code examples
cgccstatic-libraries

How to compile a static library in Linux?


I have a question: How to compile a static library in Linux with gcc, i.e. I need to compile my source code into a file named out.a. Is it sufficient to simply compile with the command gcc -o out.a out.c? I'm not quite familiar with gcc, hope anyone can give me a hand.


Solution

  • See Creating a shared and static library with the gnu compiler [gcc]

    gcc -c -o out.o out.c
    

    -c means to create an intermediary object file, rather than an executable.

    ar rcs libout.a out.o
     
    

    This creates the static library. r means to insert with replacement, c means to create a new archive, and s means to write an index. As always, see the man page for more info.