-Wl选项告诉编译器将后面的参数传递给链接器。
-rpath使得execute记住链接库的位置.
-W -rpath=./调用当前文件中的.so文件
动态库
#include <stdio.h>
#include"test2.h"
int main(void)
{
func_a();
struct St *s= func_b();
printf("hello\n");
// printf("%s\n",s.name);
return 0;
}
#include <stdio.h>
#include "test2.h"
#include<malloc.h>
struct St{
int id;
char *name;
};
void func_a(){
printf("FUNC_A\n");
}
struct St *func_b(){
struct St *s = malloc(sizeof(struct St));
s->id=123;
s->name="Luck";
return s;
}
#ifndef TEST2_H
#define TEST2_H
struct St;
void func_a();
struct St * func_b();
#endif
将test2.c编译成test2.so库
gcc -shared test2.c -o test2.so
将test1进行编译
gcc test1.c test2.so -L. -o test1 -Wl,-rpath=./
运行 ./test1
当test1和test2.so不处于同一个文件夹下时报错
静态库
gcc -c test2.c
ar rcs test2.a test2.o
gcc test1.c -L ./ test2.a -o test1