C语言 如何在 Linux 中编译静态库?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2734719/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to compile a static library in Linux?
提问by Summer_More_More_Tea
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.
我有一个问题:如何在 Linux 中使用 编译静态库gcc,即我需要将我的源代码编译成一个名为 out.a 的文件。简单地用命令编译就足够了gcc -o out.a out.c吗?不是很熟悉gcc,希望大家帮帮我。
回答by Matthew Flaschen
See Creating a shared and static library with the gnu compiler [gcc]
gcc -c -o out.o out.c
-cmeans to create an intermediary object file, rather than an executable.
-c意味着创建一个中间目标文件,而不是一个可执行文件。
ar rcs libout.a out.o
This creates the static library. rmeans to insert with replacement, cmeans to create a new archive, and smeans to write an index. As always, see the man pagefor more info.
这将创建静态库。 r意味着插入替换,c意味着创建一个新的存档,并s意味着写入索引。与往常一样,请参阅手册页以获取更多信息。
回答by Alex44
Here a full makefile example:
这是一个完整的 makefile 示例:
makefile
生成文件
TARGET = prog
$(TARGET): main.o lib.a
gcc $^ -o $@
main.o: main.c
gcc -c $< -o $@
lib.a: lib1.o lib2.o
ar rcs $@ $^
lib1.o: lib1.c lib1.h
gcc -c -o $@ $<
lib2.o: lib2.c lib2.h
gcc -c -o $@ $<
clean:
rm -f *.o *.a $(TARGET)
explaining the makefile:
解释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 archive
target: prerequisites- 规则头$@- 表示目标$^- 表示所有先决条件$<- 意味着只是第一个先决条件ar- 用于创建、修改和从档案中提取的 Linux 工具,请参阅手册页了解更多信息。这种情况下的选项意味着:r- 替换存档中存在的文件c- 如果不存在,则创建一个存档s- 在档案中创建一个目标文件索引
To conclude: The static library under Linux is nothing more than a archive of object files.
总结:Linux 下的静态库只不过是目标文件的存档。
main.cusing the lib
main.c使用 lib
#include <stdio.h>
#include "lib.h"
int main ( void )
{
fun1(10);
fun2(10);
return 0;
}
lib.hthe libs main header
lib.h libs 主头文件
#ifndef LIB_H_INCLUDED
#define LIB_H_INCLUDED
#include "lib1.h"
#include "lib2.h"
#endif
lib1.cfirst lib source
lib1.c第一个lib源码
#include "lib1.h"
#include <stdio.h>
void fun1 ( int x )
{
printf("%i\n",x);
}
lib1.hthe corresponding header
lib1.h对应的头文件
#ifndef LIB1_H_INCLUDED
#define LIB1_H_INCLUDED
#ifdef __cplusplus
extern “C” {
#endif
void fun1 ( int x );
#ifdef __cplusplus
}
#endif
#endif /* LIB1_H_INCLUDED */
lib2.csecond lib source
lib2.c第二个lib源码
#include "lib2.h"
#include <stdio.h>
void fun2 ( int x )
{
printf("%i\n",2*x);
}
lib2.hthe corresponding header
lib2.h对应的头文件
#ifndef LIB2_H_INCLUDED
#define LIB2_H_INCLUDED
#ifdef __cplusplus
extern “C” {
#endif
void fun2 ( int x );
#ifdef __cplusplus
}
#endif
#endif /* LIB2_H_INCLUDED */
回答by Ignacio Vazquez-Abrams
Generate the object files with gcc, then use arto bundle them into a static library.
使用 gcc 生成目标文件,然后使用ar将它们捆绑到静态库中。

