## 使用静态库链接程序

-c 编译且汇编，但是不链接

-S 只编译，不汇编也不链接

-c选项产出的就是经常看到的.o文件，也是一般用来创建静态库的文件。

1. gcc -o test main.c libtest.a

2. gcc -o test main.c –ltest –L./

1. gcc –o test main.c libtest2.a libtest1.a

-l library
Search the library named library when linking.  (The second alternative with the library as a separate argument is
only for POSIX compliance and is not recommended.)

It makes a difference where in the command you write this option; the linker searches and processes libraries and
object files in the order they are specified.  Thus, foo.o -lz bar.o searches library z after file foo.o but before
bar.o.  If bar.o refers to functions in z, those functions may not be loaded.

-( archives -)
--start-group archives --end-group
The archives should be a list of archive files.  They may be either explicit file names, or -l options.

The specified archives are searched repeatedly until no new undefined references are created.  Normally, an archive is
searched  only  once  in the order that it is specified on the command line.  If a symbol in that archive is needed to
resolve an undefined symbol referred to by an object in an archive that appears later on the command line, the  linker
would not be able to resolve that reference.  By grouping the archives, they all be searched repeatedly until all pos-
sible references are resolved.

Using this option has a significant performance cost.  It is best to use it only when there are  unavoidable  circular
references between two or more archives.

posted on 2012-06-06 13:57 悟空不悟空 阅读(...) 评论(...) 编辑 收藏

• 随笔 - 25
• 文章 - 0
• 评论 - 3