静态库,动态库

来源:互联网 发布:最新病毒源码 编辑:程序博客网 时间:2024/04/29 12:18

参考

http://www.cnblogs.com/skynet/p/3372855.html

编译命令

1.静态库

源文件StaticMath.cpp

#include "StaticMath.h"
#include <iostream>
using namespace std;
void StaticMath::print()
{


        cout<<"123";
}


StaticMath.h

#pragma  once
class StaticMath
{
public:
//    StaticMath(void);
 //   ~StaticMath(void);
 
  //  static double add(double a, double b);//加法
   // static double sub(double a, double b);//减法
   // static double mul(double a, double b);//乘法
   // static double div(double a, double b);//除法
 
    void print();
};


TestStaticLibrary.cpp 

#include "StaticMath.h"
#include <iostream>
using namespace std;
 
int main(int argc, char* argv[])
{
    double a = 10;
    double b = 2;
 
//    cout << "a + b = " << StaticMath::add(a, b) << endl;
  //  cout << "a - b = " << StaticMath::sub(a, b) << endl;
   // cout << "a * b = " << StaticMath::mul(a, b) << endl;
   // cout << "a / b = " << StaticMath::div(a, b) << endl;
 
    StaticMath sm;
    sm.print();
 
//    system("pause");
    return 0;
}


(1)g++ -c StaticMath.cpp 
生成目标文件 StaticMath.o
(2)ar crv libStaticMath.a StaticMath.o 
生成静态库文件libStaticMath.a
(3)g++ TestStaticLibrary.cpp -L. -lStaticMath -o output
生成可执行文件output -L. 要链接的库文件

2.动态库
(1)g++ -c -fPIC StaticMath.cpp 
生成目标文件 StaticMath.o
(2)g++ -shared -fPIC -o libStaticMath.so StaticMath.o 
生成动态库文件libStaticMath.so
(3)g++ TestStaticLibrary.cpp -L. -lStaticMath -o output2
生成可执行文件output2 -L. 要链接的库文件

动态库需要执行以下步骤才可生效,不然找不到此动态库

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:./libStaticMath.so
export LD_LIBRARY_PATH


0 0
原创粉丝点击