-1
我有一個C++庫,它公開了一些API(通過cppH.h)&它是一個靜態庫(* .lib)。 我想在C代碼中使用它,因此寫了如下的C Wrapper。但是,我收到如下構建錯誤。請在這方面幫助我知道我失去了什麼。我從hereC封裝的C++類不工作
cppH.h refered - 在C++的頭文件庫
class Abc
{
int ma, mb;
public:
void pass(int a,int b);
int sum();
};
CWrapper.h
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Abc_C Abc_C;
Abc_C* New_Abc();
void pass_in_C(Abc_C* cobj, int a, int b);
int sum_in_C(Abc_C* cobj);
#ifdef __cplusplus
}
#endif
CWrapper.cpp
#include "CWrapper.h"
#include "cppH.h"
extern "C" {
Abc_C* New_Abc()
{
return new Abc_C();
}
void pass_in_C(Abc_C* cobj, int a, int b)
{
cobj->pass(a, b);
}
int sum_in_C(Abc_C* cobj)
{
cobj->sum();
}
}
CWrapper.cpp & CWrapper.h靜態鏈接到C++庫cppH.lib & cppH.h.
編譯錯誤:
1>------ Rebuild All started: Project: CApp, Configuration: Debug Win32 ------
1> CS.c
1> CWarpperS.cpp
1>c:\users\user1\documents\ccg\vsprojects\expapp\capp\cwarppers.cpp(7): error C2512: 'Abc_C' : no appropriate default constructor available
1>c:\users\user1\documents\ccg\vsprojects\expapp\capp\cwarppers.cpp(12): error C2027: use of undefined type 'Abc_C'
1> c:\users\user1\documents\ccg\vsprojects\expapp\capp\cwrapper.h(6) : see declaration of 'Abc_C'
1>c:\users\user1\documents\ccg\vsprojects\expapp\capp\cwarppers.cpp(12): error C2227: left of '->pass' must point to class/struct/union/generic type
1>c:\users\user1\documents\ccg\vsprojects\expapp\capp\cwarppers.cpp(17): error C2027: use of undefined type 'Abc_C'
1> c:\users\user1\documents\ccg\vsprojects\expapp\capp\cwrapper.h(6) : see declaration of 'Abc_C'
1>c:\users\user1\documents\ccg\vsprojects\expapp\capp\cwarppers.cpp(17): error C2227: left of '->sum' must point to class/struct/union/generic type
========== Rebuild All: 0 succeeded, 1 failed, 0 skipped ==========
什麼是編譯器錯誤日誌中的'cwarppers.cpp'?你沒有顯示這樣的文件(雖然它似乎拼錯了)。 – tambre
'extern「C」'並不意味着它是C代碼,只是它使用C ABI和命名約定。函數體仍然是C++。 – Olaf