在C源文件中包含带有命名空间的C ++头文件会导致编译错误

我不是一个专业的C ++程序员,我最近在C ++中做了一个技巧,这引起了我下面的问题。

我的任务目标:复制特定的非系统线程(实际上是协作线程)安全模块,以创建系统线程安全版本,以支持系统中的不同需求。 但是我们决定创建一个名称空间来保护C ++头文件中的系统线程版本,而不是创建sys_XXX函数来保持兼容性。 我实际上可以将它包含在CPP文件中并愉快地工作但我刚刚意识到我的funcInit调用在它到达CPP文件控件之前没有被调用。 不幸的是,协作线程版本的这个init是在C文件中。 现在我需要从同一个地方初始化我的系统线程安全版本,但我已被编译错误阻止,这是我所知道的难以解决的。

编译错误日志: –

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 

来源和标题摘要FYI: –

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 ++头文件(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 } 

你不能在C中使用名称空间,在C中也不允许使用extern "C" 。所以可能是一个解决方案:你的C ++模块在名称空间SystemThreadUtils中定义了一组需要在C-Code中调用的函数。 既然你不能直接这样做,你将不得不围绕它们编写一个符合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 ++中,只是执行调用。

请参阅从C文件调用C ++函数 。