linux c 動態庫里寫 類及呼叫類

2021-06-19 09:37:00 字數 1627 閱讀 9406

怎樣在linux c++ 程式中呼叫.so動態庫中的類

2008-03-25 13:01

bdqwe123

|分類:c/c++

|瀏覽3335次

|該問題已經合併到》

1.linux環境下,編寫乙個類庫, 有乙個類叫class1, 編譯成lib1.so

2.編寫乙個測試程式, test.cpp, 如果在test.cpp中呼叫class1?

3.makefile如何寫,test.cpp中如何開啟lib1.so,如何宣告class1?

提問者採納

在test.cpp中引用class1(如通過標頭檔案)

makefile

cc = gcc

cflags = -wall -os

ldflags = -llib1.so

all: test

test: test.cpp

$(cc) $(cflags) $(ldflags) $< -o $@

clean:

rm test

ldd test就可以看到有沒有連線成功

例子如下:
//----------

//----------

#ifndef polygon_h

#define polygon_h

#include

class polygon ;

// the types of the class factories

typedef polygon* create_t();

typedef void destroy_t(polygon*);

#endif

//----------

//----------

#include "polygon.h"

polygon::polygon()

polygon::~polygon()

void polygon::set_side_length(double side_length)

double polygon::area() const

//makefile

cc = g++

cflags = -wall -os

#ldflags = -l./usr/local/lib

libs= -l********

all: main

main: main.cpp

$(cc) $(cflags)  $< -o $@  $(libs)

clean:

rm main

//----------

://----------

#include "polygon.h"

//#include #include

int main() {

polygon testpolygon;

用下面的命令編譯,生成lib********.so

g++ -g -fpic -shared -o lib********.so polygon.cpp

然後make一下,執行main試試吧!

C 呼叫類庫里的事件

首先在類庫中定義事件 定義委託 public delegate void delweituo string name 定義事件 public event delweituo eventweituo 定義事件觸發方法 定義事件觸發方法 public void fun string name 滿足情況觸...

動態呼叫類和方法

舉乙個很簡單的例子 某公司的有1000名員工,每個員工的工資都不一樣.發工資的時候,這要是人工去發,耗費的時間和精力是非常大的.所以財務會打乙個 給銀行,委託銀行轉賬.站在銀行的角度,如果有1000個公司,委託銀行轉賬發工資.它應該怎麼做呢?它需要通過電子轉賬系統,輸入公司名字,每個員工的工資數,就...

Delphi動態呼叫C 寫的DLL

c dll 檔案,建議用最簡單的c 編輯工具。不會加入很多無關的dll檔案。本人用codeblocks mingw。不像 vs2010,dll編譯成功,呼叫的時候會提示缺其他dll。系統生成的main.h和main.cpp ifndef main h define main h include to...