gcc动态库及静态库

213 篇文章 0 订阅
//libt.h
#ifdef __cplusplus
extern "C" {
#endif

void PrintMsg(const char* msg);

#ifdef __cplusplus
}
#endif


//libt.cpp
#include"libt.h"
#include<iostream>

using namespace std;

void PrintMsg(const char* msg)
{
	cout << "PrintMsg : " << msg << endl;
}


//libt2.cpp
#include<iostream>

using namespace std;

void PrintMsg(const char* msg)
{
	cout << "PrintMsg : " << msg << endl;
}


//test.c
#include"libt.h"
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main()
{
	PrintMsg("Hello World!");
	printf("sinx(x)=%lf\n",sin(3.1415926/3));
	return 0;
}

//makefile
ALL:
	g++ -shared -o libt.so libt.cpp
	g++ -shared -o libt2.so libt2.cpp
	nm libt.so | grep PrintMsg
	nm libt2.so | grep PrintMsg
	gcc -c test.c
	gcc -o t1 test.o -L$(PWD) -lt -lm
	ldd t1
	g++ -c libt.cpp
	ar rvs t.a libt.o
	g++ -o t2 test.o -L$(PWD) -l:t.a -lm
	ldd t2
	g++ -o t3 test.o libt.o -lm
	g++ -o t4 test.c libt.cpp -lm
	./t1
	./t2
	./t3
	./t4	
clean:
	rm *.o *.so *.a  t1 t2 t3 t4


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值