我想通過實現一個包裝在C框架中使用C++庫。我發現你需要在頭文件中聲明函數爲extern「C」。但是,當我試圖鏈接我的動態庫時,名稱修改未禁用,導致未定義的符號,當我嘗試使用我的包裝。extern「C」沒有禁用名稱綁定
這裏是我的包裝的當前報頭,SbeOtfDecoder.h:
#ifndef ITF_SBEOTFDECODER_H_
#define ITF_SBEOTFDECODER_H_
// WARNING! In a C++ wrapper for C, any exception thrown by C++ code HAS TO BE CATCHED!
// Otherwise, it is "undefined behavior".
#ifdef __cplusplus
extern "C" {
#endif
///Create a new SbeOtfDecoder class instance, casted as void * for C code.
void *SbeOtfDecoder_new_with_file(char *filename);
///Destroy SbeOtfDecoder class instance.
void SbeOtfDecoder_free(void *self);
#ifdef __cpluscplus
} //extern "C"
#endif
#endif /* ITF_SBEOTFDECODER_H_ */
和對應功能在SbeOtfDecoder.cpp:
class SbeOtfDecoder
{
public:
SbeOtfDecoder(char *filename);
~SbeOtfDecoder();
};
SbeOtfDecoder::SbeOtfDecoder(char *filename)
{
}
SbeOtfDecoder::~SbeOtfDecoder()
{
}
void *SbeOtfDecoder_new_with_file(char *filename)
{
return new SbeOtfDecoder(filename);
}
void SbeOtfDecoder_free(void *self)
{
delete static_cast<SbeOtfDecoder*>(self);
}
然後,聯動發生時(在schroot):
g++ -shared ../tmpfs/v7-flux/env/squeeze-32/DEBUG/./libs/itf_sbedecoder/src/Ir.oo ../tmpfs/v7-flux/env/squeeze-32/DEBUG/./libs/itf_sbedecoder/src/Listener.oo ../tmpfs/v7-flux/env/squeeze-32/DEBUG/./libs/itf_sbedecoder/src/SbeOtfDecoder.oo -L../tmpfs/lib -lstdc++ -o ../tmpfs/lib/libitfsbedecoder.so
但是,符號仍然在輸出庫中被破壞:
nm ../tmpfs/lib/libitfsbedecoder.so | grep SbeOtf
0001f460 t _GLOBAL__I_SbeOtfDecoder.cpp
0001f3d8 T _Z18SbeOtfDecoder_freePv
0001f37f T _Z27SbeOtfDecoder_new_with_filePc
0001f36e T _ZN13SbeOtfDecoderC1EPc
0001f368 T _ZN13SbeOtfDecoderC2EPc
0001f37a T _ZN13SbeOtfDecoderD1Ev
0001f374 T _ZN13SbeOtfDecoderD2Ev
這些符號不能由C框架的其餘部分中,我目前的單元測試崩潰,出現此錯誤:
~/workspace/v7-flux/build/cunit-32: symbol lookup error: ../tmpfs/v7-flux/env/squeeze-32/DEBUG/cunit/./libs/itf_sbedecoder/cunit//libtest_SbeOtfDecoder_test.so: undefined symbol: SbeOtfDecoder_new_with_file
我不知道我做錯了。如果您需要更多關於編譯階段的信息,請隨時提問。我沒有多說,因爲幾乎所有的編譯過程都隱藏在這個框架中。
您的.cpp文件是否包含封裝器.h? –
'extern「C」'從定義中缺失。 – Jarod42
您還沒有發佈一個完整的示例來顯示行爲,所以我們在這裏猜測,但編譯器在編譯函數定義時需要了解extern「C」,因此很可能忘記了#include「SbeOtfDecoder 。H」。 –