2013-04-17 4 views
6

私は専門家ではなく、最近C++でトリックを行っているので、以下の問題を引き起こしています。Cソースファイルに名前空間を持つC++ヘッダーファイルを組み込むとコンパイルエラーが発生する

私の仕事の目的:特定の非システムスレッド(実際に協調スレッド)セーフモジュールは、システム内のさまざまなニーズをサポートするためのシステムスレッドセーフバージョンを作成するために複製されます。しかし、互換性を維持するためにsys_XXX関数を作成する代わりに、C++ヘッダーファイルでシステムスレッドのバージョンを保護する名前空間を作成することにしました。私は実際にこれをCPPファイルに含めることができ、うまく動作しますが、CPPファイルコントロールに到達する前にfuncInit呼び出しが呼び出されていないことがわかりました。残念ながら、この協調スレッド版のinitはCファイルにあります。今私は、同じ場所から私のシステムスレッドの安全なバージョンを初期化する必要がありますが、私の知識から解決することは難しいコンパイルエラーによってブロックされている。

コンパイルエラーログ - FYI

In file included from sysinit.c:87: 
sys_unicode.h:39: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'SystemThreadUtils' <== corresponds to line [namespace SystemThreadUtils {] 
sysinit.c:88: 
sysinit.c:512: error: expected identifier or '(' before string constant <== corresponds to line [extern "C" bool SystemThreadUtils::funcInit(void);] 
sysinit.c:513: error: expected identifier or '(' before string constant <== corresponds to line [extern "C" bool SystemThreadUtils::funcTerm(void);] 
sysinit.c: In function 'SysInit': 
sysinit.c:817: error: 'SystemThreadUtils' undeclared (first use in this function) <= corresponds to line [SystemThreadUtils::funcInit();] 
sysinit.c:817: error: (Each undeclared identifier is reported only once 
sysinit.c:817: error: for each function it appears in.) 
sysinit.c:817: error: expected ')' before ':' token 
sysinit.c: In function 'SysTerm': 
sysinit.c:2737: error: expected expression before ':' token <== corresponds to line [SystemThreadUtils::funcTerm();] 
sysinit.c:2737: warning: label 'SystemThreadUtils' defined but not used 

ソースとヘッダスニペット: -

Cヘッダファイル(unicode.h):

// all functions called from the C source file 
funcInit(); 
funcA(); 
funcB(); 
funcTerm(); 

Cヘッダファイル(unicode.c ):

// all functions called from the C source file 
funcInit() { 
} 
funcA() { 
} 
funcB() { 
} 
funcTerm() { 
} 

C++ he ADERファイル(sys_unicode.h):

#include "unicode.h" 
namespace SystemThreadUtils { 

    // below functions called from the C source file 
    extern "C" funcInit(); 
    extern "C" funcTerm(); 

    // below functions called from the CPP source file 
    funcA(); 
    funcB(); 
} 

C++ソース定義(sys_unicode.cpp):

#include "sys_unicode.h" 

namespace SystemThreadUtils { 

    // below functions are called from C source 
    funcInit() { 
    } 
    funcTerm() { 
    } 

    // below methods are called from CPP source 
    funcA() { 
    } 
    funcB() { 
    } 
} 

CPPソース#1(utils.cpp):

#include "sys_unicode.h" 

using namespace SystemThreadUtils; 

utils::utils_init() 
{ 
    funcA(); 
    funcB(); 
} 

Cソース#2(sysinit.c):

#include "sys_unicode.h" 

extern "C" bool SystemThreadUtils::funcInit(void); 
extern "C" bool SystemThreadUtils::funcTerm(void); 

SysInit() 
{ 
    funcInit(); // non system thread safe version 
    SystemThreadUtils::funcInit(); // system thread safe version 
} 
SysTerm() 
{ 
    funcTerm(); // non system thread safe version 
    SystemThreadUtils::funcTerm(); // system thread safe version 
} 
+0

http://stackoverflow.com/questions/389827/namespaces-in-c – Najzero

+0

@Najzero SOリンクをありがとう、しかしそれは私の問題を解決しません。私はCPPヘッダファイルに名前空間を持っており、同じ名前のC関数との混乱を避けるためにこれを使う必要があります。 unicode.cとunicode.cppには同じ名前のx関数が含まれています。 – rajeshk

+0

sys_unicode.h:39。私は39行を見ることができません、それは全体のファイルですか? –

答えて

19

名前空間は使用できませんCでは、extern "C"もC言語では許可されていません。解決策は何ですか?C++モジュールは、Cコードで呼び出される必要があるネームスペースSystemThreadUtilsに一連の関数を定義します。あなたが直接それを行うことはできませんので、あなたはそれらの周りにC準拠のラッパーを記述する必要があります。

//C/C++ - header "sys_unicode_for_c.h" 

#ifdef __cplusplus 
extern "C" { 
#endif 

    void STU_funcInit(); 
    void STU_funcTerm(); 

#ifdef __cplusplus 
} //end extern "C" 
#endif 

//C++-source: sys_unicode_for_c.hpp 
#include "sys_unicode.h" 

extern "C" { 
    void STU_funcInit() { 
    SystemThreadUtils::funcInit(); 
    } 
    void STU_funcTerm() { 
    SystemThreadUtils::funcTerm(); 
    } 
} 

意味:あなたは、有効なCのコードであるヘッダを必要とするために委譲関数を宣言各C++関数をCから呼び出す必要があります。ソースはC++であり、呼び出しを行うだけです。

Calling C++ functions from C fileも参照してください。

+0

私の問題を解決したあなたの助けに感謝します。 – rajeshk

関連する問題