centos5.8,g++4.1.2
最近操作比较多,小小笔记下
静态库,动态库编译和链接
//1.DynamicMath.h
class DynamicMath
{
public:
static double add(double a, double b);
static double sub(double a, double b);
void print();
};
//2.DynamicMath.cpp
#include "DynamicMath.h"
#include <iostream>
using std::cout;
using std::endl;
double DynamicMath::add(double a, double b)
{
return a+b;
}
double DynamicMath::sub(double a, double b)
{
return a-b;
}
void DynamicMath::print()
{
cout<<"DynamicMath"<<endl;
}
//3.main.cpp
#include <iostream>
using namespace std;
#include "DynamicMath.h"
int main()
{
double a = 10;
double b = 2;
cout<<"a + b= "<<DynamicMath::add(a, b)<<endl;
cout<<"a - b= "<<DynamicMath::sub(a, b)<<endl;
DynamicMath dy;
dy.print();
return 0;
}
//4.makefile
all: main smath smain dmath dmain
main:
g++ -o main main.cpp DynamicMath.cpp
smath:
g++ -c DynamicMath.cpp
ar -rsv libsmath.a DynamicMath.o
smain:
g++ -o smain main.cpp -L./ -lsmath
dmath:
g++ -fPIC -shared -o libdmath.so DynamicMath.cpp
dmain:
g++ -o dmain -L./ -ldmath main.cpp -Wl,-rpath ./
clean:
rm -f *.o
rm -f *.a
rm -f *.so
rm -f main
rm -f smain
rm -f dmain