C呼叫C 鏈結庫

2021-09-30 15:06:52 字數 1698 閱讀 4996

c呼叫c++鏈結庫:

1.編寫c++**,編寫函式的時候,需要加入對c的介面,也就是extern 「c"

2.由於c不能直接用"class.function」的形式呼叫函式,所以c++中需要為c寫乙個介面函式。例如本來要呼叫student類的talk函式,就另外寫乙個cfun(),專門建乙個student類,並呼叫talk函式。而cfun()要有extern宣告

3.我在練習中就使用在c++標頭檔案中加extern 」c」的方法。而c檔案要只需要加入對cpp.h的引用

4.詳細見如下**:

student是乙個類,裡邊有talk函式,就輸出一句話而已

cpp.cpp與cpp.h是兩個c++**,包含對c的介面

最後用c**:helloc.c來測試結果

student.cpp

[cpp]view plain

copy

print?

#include

using

namespace

std;

#include"student.h"

void

student::talk()

student.h:

[cpp]view plain

copy

print?

#ifndef_student_

#define_student_

class

student;

#endif

cpp.h:

[cpp]view plain

copy

print?

#ifndef_cpp_

#define_cpp_

#include"student.h"

#ifdef__cplusplus

extern

"c"#endif

#endif

cpp.cpp:

[cpp]view plain

copy

print?

#include

using

namespace

std;

#include"cpp.h"

studentstu;

void

hellocplusplus()

void

hellocplusplus2()

helloc.c:

[cpp]view plain

copy

print?

#include

#include"cpp.h"

intmain()

我這次練習是直接在xp環境下,在cmd命令列方式用gcc,g++來編譯的。

1.編譯c++**,成為鏈結庫

g++ -shared -o libccall.so cpp.cpp student.cpp (libccall.so為庫名)

2.編譯c**:g++ helloc.c ./libccall.so。這裡一定要用g++,如果用gcc會出錯,因為gcc編譯c++檔案才會自動呼叫g++,但如果物件直接就是c檔案就不會呼叫g++了。

C 呼叫C鏈結庫

c 呼叫c鏈結庫 c 呼叫c語言的鏈結庫,其實相對c呼叫c 簡單。因為c 本來就向下相容c吧 個人見解 簡單說來原因就是未經處理的c 編譯後函式名可能變為 helloc 之類的,而c 編譯後函式名卻不是這樣,所以就對不上。詳細解釋見 需要在include c的檔案的時候加上extern c c.h ...

C 呼叫C 動態鏈結庫dll

在過程中發現兩種方法解決問題 一種是非託管c 建立的dll庫,需要用靜態方法呼叫。這種方法無法在c 的reference中直接引用,而是要用靜態呼叫的方法,其他部落格已經介紹的很詳盡,唯一需要補充的是,c 檔案需要先 usingsystem.runtime.interopservices 之後才可以...

Python呼叫C 動態鏈結庫

1 安裝vs2010,或者以上的版本 2 建立win32應用程式,應用程式型別選擇dll。由於python是64位的,因此需要將win32程式設定為x64,即64位的。3 編寫程式add.cpp 4 編寫標頭檔案add.h。由於 比較少,也可以申明直接放在add.cpp中,此處為了規範,還是分開寫的...