g++ 动态库的编译及使用

#ifndef __HELLO_H_
#define __HELLO_H_

void print();

#endif
#include "hello.h"
#include <iostream>
using namespace std;

void print() {
  cout << "hello world" << endl;
}

将上面的 cpp 文件编译为动态库:

 

g++ hello.cpp -fPIC ishared -o libhello.so

 

#include "hello.h"
#include <iostream>
using namespace std;

int main() {
  print();
  cout << "hello" << endl;
  return 0;
}

将 main.cpp 与 libhello.so 链接成一个可执行文件

g++ main.cpp -L. -lhello -o main

 

posted @ 2014-12-20 19:50  leesea  阅读(2272)  评论(0)    收藏  举报