linux下C语言编程动态库so的编写及调用

时间:2021-01-19 09:26:27
//test_so.h
#include <stdio.h>
void test_a();
void test_b();
//test_a.c
#include "so_test.h"

void test_a()
{
	printf("this is in test_a...\n");
}

//test_b.c
#include "so_test.h"

void test_b()
{
	printf("this is in test_b...\n");
}

//test.c
#include "so_test.h"

int main()
{
	test_a();
	test_b();
	return 0;
}


编译步骤

gcc test_a.c test_b.c -fPIC -shared -o libtest.so

gcc test.c -L. -ltest -o test


./test

解决链接库问题:

#vim /etc/profile

LD_LIBRARY_PATH=/mnt/hgfs/Ubuntu_shared/so:$LD_LIBRARY_PATH

export LD_LIBRARY_PATH

# source /etc/profile