ライブラリの作り方、使い方

g++ -c mymath.cpp -fPIC

g++ -shared -Wl,-soname,libmymath.so.1 -o libmymath.so.1.0.0 mymath.o

sudo install libhello.so.1.0.0 /usr/local/lib/

sudo ldconfig

sudo ln -sf /usr/local/lib/libmymath.so.1.0.0 /usr/local/lib/libmymath.so

sudo cp mymath.h /usr/local/include/

提供する側(mymath.cpp)

#include "mymath.h"

mymath::mymath()
{
};

mymath::~mymath()
{
};

int mymath::add(int a, int b)
{
	return a + b;
};

int mymath::sub(int a, int b)
{
	return a - b;
};

int mymath::mul(int a, int b)
{
	return a * b;
};

double mymath::div(double a, double b)
{
	return a / b;
};
  
提供する側(mymath.h)

class mymath
{
	public:
		mymath();
		~mymath();

		int add(int a, int b);
		int sub(int a, int b);
		int mul(int a, int b);
		double div(double a, double b);
};
  
使う側(main.cpp)

#include <iostream>
#include <mymath.h>

using namespace std;

int main(void)
{
	auto math = new mymath();

	auto a = math->add(100,200);

	cout << a << endl;

	return 0;
}