Inclusion of a C ++ header file with a namespace in the C source file leads to a compilation error

I'm not a C ++ programming specialist, and I recently did a trick in C ++, which causes me the problem below.

The goal of my task: a specific non-system thread (shared thread actually) a safe module is duplicated to create a safe version of the system thread to support various system requirements. But instead of creating sys_XXX functions for compatibility, we decided to create a namespace to protect the system thread version in the C ++ header file. I can actually include this in a CPP file and work with pleasure, but I just realized that my funcInit call is not called before it reaches the CPP file control. Unfortunately, this init for the shared streaming version is in file C. Now I need to initialize a safe version of the system thread from the same place, but im is blocked by a compilation error, which is difficult to solve from my knowledge.

Compilation Error Log: -

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 

Source and header FYI fragments: -

C header file (unicode.h):

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

C header file (unicode.c):

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

C ++ header file (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 ++ source definition (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 Source # 1 (utils.cpp):

 #include "sys_unicode.h" using namespace SystemThreadUtils; utils::utils_init() { funcA(); funcB(); } 

Source 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 } 
+6
source share
1 answer

You cannot use namespaces in C, extern "C" also not allowed in C. So what could be the solution: your C ++ module defines a set of functions in the SystemThreadUtils namespace that you need to call in C code. Since you cannot do this directly, you will need to write a wrapper with a C conformation:

 //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(); } } 

Meaning: you need a header that is valid C code and declares a delegation function for each C ++ function that you must call from C. The source is in C ++ and just makes the call.

See Calling C ++ Functions from a C File .

+19
source

All Articles