1. 当在同一个目录下静态库和共享库同名时,共享库优先
hello.h头文件
#ifndef HELLO_H
#define HELLO_H
void print_hello();
#endif
hello.c源文件
#include "hello.h"
#include <stdio.h>
int main(int argc,char *argv[])
{
printf("hello world!");
}
测试使用源文件main.c
#include "hello.h"
int main(int argc,char *argv[])
{
printf_hello();
}
1.1 编译静态库和共享库
[test@hadoop hello]$ ls
hello.c hello.h main.c
[test@hadoop hello]$ gcc -c hello.c
[test@hadoop hello]$ ar crs libhello.a hello.o
[test@hadoop hello]$ gcc -shared -fPIC -o libhello.so hello.o
[test@hadoop hello]$ ls
hello.c hello.h hello.o libhello.a libhello.so main.c
1.2 使用hello库编译 main.c
[test@hadoop hello]$ gcc main.c -o main -L. -lhello -I.
[test@hadoop hello]$ ld
GCC编译:静态库与共享库共存策略

本文介绍了在GCC编译过程中,当静态库和共享库同名时,如何选择使用以及如何在编译程序中同时包含静态库和共享库。通过实例展示了删除静态库、保留共享库,以及编译calculate为静态库并用其与共享库libhello.so一起编译main的过程。
最低0.47元/天 解锁文章
1735

被折叠的 条评论
为什么被折叠?



