2013-03-13 94 views
0

從我的C代碼調用C++方法時遇到問題。我需要在C++代碼中調用的方法不在類中。我想建立一個簡單的例子,我有以下文件:編譯和鏈接調用C++函數的C代碼

//header.h 
#ifdef __cplusplus 
#include <iostream> 
extern "C" { 
#endif 
int print(int i, double d); 
#ifdef __cplusplus 
} 
#endif 

//ccode.c 
#include "header.h" 

main() { 
     printf("hello"); 
     print(2,2.3); 
} 

//cppcode.cc 
#include "header.h" 
using namespace std; 
int print(int i, double d) 
{ 
    cout << "i = " << i << ", d = " << d; 
} 

也許我的錯誤是在我試圖編譯和鏈接這種方式。我正在做以下事情:

g++ -c cppcode.cc -o cppcode.o 

這很好。

gcc ccode.c cppcode.o -o ccode 

在這裏,我得到了以下錯誤:

cppcode.o: In function `print': 
cppcode.cc:(.text+0x16): undefined reference to `std::cout' 
cppcode.cc:(.text+0x1b): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' 
cppcode.cc:(.text+0x28): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(int)' 
cppcode.cc:(.text+0x35): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)' 
cppcode.cc:(.text+0x42): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(double)' 
cppcode.o: In function `__static_initialization_and_destruction_0(int, int)': 
cppcode.cc:(.text+0x6b): undefined reference to `std::ios_base::Init::Init()' 
cppcode.cc:(.text+0x70): undefined reference to `std::ios_base::Init::~Init()' 
collect2: ld returned 1 exit status 

我以爲這是因爲我使用的是C編譯器。編譯和鏈接這個小例子的正確方法是什麼? 這個想法是,我有C代碼運行,只需調用C++函數,而不必在C中重寫它們。在此先感謝您的幫助!

我使用Ubuntu 12.04,gcc版本4.6.3

+0

你有一個c + +依賴;你應該使用'gcc'來構建'ccode.o',然後使用'g ++'將.o文件鏈接在一起。 – Joe 2013-03-13 17:49:08

+0

請勿在其他標題內包含標題,甚至標準標題。除非他們明確定義了頭文件中需要的類型/常量*。 – StoryTeller 2013-03-13 17:51:38

+0

@StoryTeller:謝謝你的提示,我會修復這個問題 – natascha 2013-03-13 18:05:04

回答

2

你需要C++運行時庫鏈接。

gcc ccode.c cppcode.o -o ccode -lstdc++ 
+0

完美的,現在它工作。謝謝 – natascha 2013-03-13 18:02:52

0

g ++編譯器自動將您的程序與標準cpp庫鏈接。 使用gcc進行編譯時,鏈接器可以找到對該文件的引用。 你有兩種選擇。 一個是用g ++編譯c文件。其次是強制標準cpp庫的鏈接。

這裏有什麼寫在GCC指南: -static-的libstdC++

When the g++ program is used to link a C++ program, it normally automatically links against libstdc++. If libstdc++ is available as a shared library, and the -static option is not used, then this links against the shared version of libstdc++. That is normally fine. However, it is sometimes useful to freeze the version of libstdc++ used by the program without going all the way to a fully static link. The -static-libstdc++ option directs the g++ driver to link libstdc++ statically, without necessarily linking other libraries statically. 

運行以下命令:

gcc ccode.c cppcode.o -o ccode -lstdc++ 
+1

再看一遍,他用gcc編譯c文件 – stdcall 2013-03-13 17:52:28

+0

是的,你是對的。抱歉。 – nneonneo 2013-03-13 17:54:19

1

你應該單獨編譯和鏈接。使用g++進行鏈接以獲得正確的標準庫。

g++ -c cppcode.cc -o cppcode.o 
gcc -c ccode.c -o ccode.o 
g++ ccode.o cppcode.o -o ccode 
+0

感謝@nneonneo爲您的答案。由於我在ccode.c中有一個主函數,所以現在可以工作了。如果沒有main,它將如何編譯?我只需要調用函數(來自其他程序,特別是我的Lua代碼)?我設法分別構建了兩個代碼(.cc和.c),但我目前無法正確鏈接它們。 – natascha 2013-03-14 12:48:36

+0

您將構建一個*共享庫*並以某種方式從您的Lua代碼加載庫。 – nneonneo 2013-03-14 16:12:53