Linux下c++經過動態連接庫調用類

http://hi.baidu.com/ablenavy/item/b498901c6826bbf587ad4e33php

Linux下的動態連接庫叫so,即Shared Object,共享對象。一些函數就不說了,網上多的是。把我遇到的問題寫下來吧linux

提示錯誤 undefined reference to `dlopen' 編譯時增長「-ldl」選項便可解決。ios

提示錯誤 cannot open shared object file: No such file or directory 將當前目錄的絕對路徑添加到LD_LIBRARY_PATH便可 export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/zhj/test_so/web

提示錯誤 undefined reference to 檢查了一下,原來在makefile裏少包含了一個文件。 這一類錯誤通常都是缺乏相應的類文件所致。ide

例子來源: http://www.linuxsir.org/bbs/printthread.php?t=266890函數

這個擴展名爲hpp的文件,我分解了一下,下面就上代碼吧spa

polygon.h //---------- //polygon.h: //---------- #ifndef POLYGON_H #define POLYGON_H #include <stdio.h>orm

class polygon { protected: double side_length_;對象

public: polygon(); virtual ~polygon();ci

void set_side_length(double side_length);

virtual double area() const; };

// the types of the class factories typedef polygon* create_t(); typedef void destroy_t(polygon*);

#endif

polygon.cpp //---------- //polygon.cpp: //---------- #include "polygon.h"

polygon::polygon() { //set_side_length(0); }

polygon::~polygon() { }

void polygon::set_side_length(double side_length) { printf("polygon side_length: %f\n\n",side_length); side_length_ = side_length; printf("polygon side_length_: %f\n\n",side_length_); }

double polygon::area() const { return 0; }

triangle.cpp //---------- //triangle.cpp: //---------- #include "polygon.h" #include <cmath>

class triangle : public polygon { public: virtual double area() const { printf("triangle side_length_: %f\n\n",side_length_); return side_length_ * side_length_ * sqrt(3) / 2; } };

// the class factories extern "C" polygon* create() { return new triangle; }

extern "C" void destroy(polygon* p) { delete p; }

main.cpp //---------- //main.cpp: //---------- #include "polygon.h" #include <iostream> #include <dlfcn.h>

int main() { using std::cout; using std::cerr;

// load the triangle library void* triangle = dlopen("./triangle.so", RTLD_LAZY); if (!triangle) { cerr << "Cannot load library: " << dlerror() << '\n'; return 1; }

// reset errors dlerror();

// load the symbols create_t* create_triangle = (create_t*) dlsym(triangle, "create"); const char* dlsym_error = dlerror(); if (dlsym_error) { cerr << "Cannot load symbol create: " << dlsym_error << '\n'; return 1; }

destroy_t* destroy_triangle = (destroy_t*) dlsym(triangle, "destroy"); dlsym_error = dlerror(); if (dlsym_error) { cerr << "Cannot load symbol destroy: " << dlsym_error << '\n'; return 1; }

// create an instance of the class polygon* poly = create_triangle();

// use the class poly->set_side_length(7); cout << "The area is: " << poly->area() << '\n';

// destroy the class destroy_triangle(poly);

// unload the triangle library dlclose(triangle); }

makefile

objects = main.o polygon.o main: $(objects) g++ -g -rdynamic -o $@ $(objects) -ldl main.o: main.cpp g++ -g -c main.cpp polygon.o: polygon.cpp polygon.h g++ -g -c polygon.cpp

.PHONY : clean clean : -rm main $(objects)

用下面的命令編譯,生成libtriangle.so g++ -g -fpic -shared -o libtriangle.so triangle.cpp polygon.cpp

而後make一下,運行main試試吧!

相關文章
相關標籤/搜索