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.
相关问题
- Error building gcc 4.8.3 from source: libstdc++.so
- Aggregate static libraries
- What are the recommended GNU linker options to spe
- What is the right order of linker flags in gcc?
- Why doesn't g++ -Wconversion warn about conver
相关文章
- gcc/g++ gives me error “CreateProcess: No such fil
- Calls that precede a function's definition can
- How can I use gcc's -I command to add recursiv
- How do I know if std::map insert succeeded or fail
- How to specify gcc flags (CXXFLAGS) particularly f
- How to generate assembly code with gcc that can be
- Weakly link static library via -weak_library
- Embedding a program's source code into its bin
Generate the object files with gcc, then use
ar
to bundle them into a static library.See Creating a shared and static library with the gnu compiler [gcc]
-c
means to create an intermediary object file, rather than an executable.This creates the static library.
r
means to insert with replacement,c
means to create a new archive, ands
means to write an index. As always, see the man page for more info.Here a full makefile example:
makefile
explaining the makefile:
target: prerequisites
- the rule head$@
- means the target$^
- means all prerequisites$<
- means just the first prerequisitear
- a Linux tool to create, modify, and extract from archives see the man pages for further information. The options in this case mean:r
- replace files existing inside the archivec
- create a archive if not already existents
- create an object-file index into the archiveTo conclude: The static library under Linux is nothing more than a archive of object files.
main.c using the lib
lib.h the libs main header
lib1.c first lib source
lib1.h the corresponding header
lib2.c second lib source
lib2.h the corresponding header