2017-09-04 12 views
-1

私はいくつかのAPI(cppH.hから)を公開するC++ライブラリを持っています。&静的ライブラリ(* .lib)です。 これをCコードで使用したいので、以下のようにC Wrapperを記述しました。しかし、以下のようなビルドエラーが発生しています。この点で、私は何が欠けているかを知るために親切に私を助けてください。C++クラスのCラッパーが動作しない

#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++ライブラリのヘッダファイル

class Abc 
{ 
    int ma, mb; 
public: 
    void pass(int a,int b); 
    int sum(); 
}; 

#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 

を - 私はhere

cppH.hから参照さ

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 ========== 
+0

コンパイラエラーログに 'cwarppers.cpp'とは何ですか?そのようなファイルは表示されていません(間違っているようですが)。 – tambre

+0

'extern" C "'は、それがC ABIと命名規則を使用していることを意味するのではなく、Cコードです。関数本体はまだC++です。 – Olaf

答えて

2

タイプclass Abcstruct Abc_Cは完全に無関係です。 Cヘッダーのtypedefが間違っています。定義されていないタイプのエイリアスです。したがって、new Abc_C();は不完全な型のオブジェクトを作成しようとしています。

簡単な修正は次のようにエイリアスを変更することです:

typedef struct Abc Abc_C; 

今エイリアスが正しいタイプの名前です。