//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
gcc动态库及静态库
最新推荐文章于 2024-08-01 18:01:52 发布