包括C ++与C源文件的命名空间的头文件导致编译错误 [英] Including C++ header file with namespace in C source file causes compilation error

查看:389
本文介绍了包括C ++与C源文件的命名空间的头文件导致编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是一个专家C ++程序员,和我最近一直在做C ++中,这是造成我的问题,下面一招的事情。

我的任务目标:具体的非系统线程(协作线程实际上)安全模块复制创建系统线程安全的版本,以支持系统的不同需求。但是,而不是创造sys_XXX功能,以保持兼容性,我们决定创建一个命名空间,以保护系统线程版本的C ++头文件。其实我可以在CPP文件中包含这一点,并愉快地工作,但我只是意识到它达到CPP文件控制之前我funcInit电话不叫。不幸的是这个初始化为协作线程版本的C文件。现在我需要来自同一个地方的init我的系统线程安全的版本,但即时通讯已经封锁的编译错误,这是很难从我的知识来解决。

编译错误日志: -

87:

 在文件从sysinit.c包括:
sys_uni code.h:39:错误:预期'=',',',',','ASM'或前SystemThreadUtils'&LT'__attribute__'; ==相当于线路[命名空间SystemThreadUtils {]
sysinit.c:88:
sysinit.c:512:错误:预期的标识符或'('前字符串常量< ==相当于线路[外部的C布尔SystemThreadUtils :: funcInit(无效);]
sysinit.c:513:错误:预期的标识符或'('前字符串常量< ==相当于线路[外部的C布尔SystemThreadUtils :: funcTerm(无效);]
sysinit.c:在函数'SYSINIT:
sysinit.c:817:错误:'SystemThreadUtils'未申报(在一次使用此功能)LT =相当于线路[SystemThreadUtils :: funcInit();]
sysinit.c:817:错误:(每个未声明的标识符报道只有一次
sysinit.c:817:错误:它出现在每个功能)
sysinit.c:817:错误:之前的预期')'':'令牌
sysinit.c:在函数'SYSTERM:
sysinit.c:2737:错误:之前的预期前pression':'令牌LT; ==相当于线路[SystemThreadUtils :: funcTerm();]
sysinit.c:2737:警告:标签SystemThreadUtils定义但未使用

源文件和头片段供参考: -

C头文件(UNI code.h):

  //都是从C源文件调用的函数
funcInit();
FuncA的行();
funcB();
funcTerm();

C头文件(UNI code.C):

  //都是从C源文件调用的函数
funcInit(){
}
FuncA的行(){
}
funcB(){
}
funcTerm(){
}

C ++头文件(sys_uni code.h):

 的#includeUNI code.h
命名空间SystemThreadUtils {    //下面从C源文件调用的函数
    为externCfuncInit();
    为externCfuncTerm();    //下面从CPP源文件调用的函数
    FuncA的行();
    funcB();
}

C ++源定义(sys_uni code.cpp):

 的#includesys_uni code.h命名空间SystemThreadUtils {    //下面的函数从C源称为
    funcInit(){
    }
    funcTerm(){
    }    //下面的方法是从源头CPP叫
    FuncA的行(){
    }
    funcB(){
    }
}

CPP源#1(utils.cpp):

 的#includesys_uni code.h使用命名空间SystemThreadUtils;utils的:: utils_init()
{
   FuncA的行();
   funcB();
}

C源#2(sysinit.c):

 的#includesys_uni code.h为externC布尔SystemThreadUtils :: funcInit(无效);
为externC布尔SystemThreadUtils :: funcTerm(无效);SYSINIT()
{
   funcInit(); //非系统线程安全版本
   SystemThreadUtils :: funcInit(); //系统线程安全版本
}
SYSTERM()
{
   funcTerm(); //非系统线程安全版本
   SystemThreadUtils :: funcTerm(); //系统线程安全版本
}


解决方案

您也不会在C使用的命名空间,的externC也不允许C.那么什么可能是一个解决办法:你的C ++模块定义了一组命名空间功能 SystemThreadUtils 需要被调用的C- code。既然你不能直接这样做,你将不得不写他们围绕C-一致性包装:

  // C / C ++  - 头sys_uni code_for_c.h#IFDEF __cplusplus
为externC{
#万一  无效STU_funcInit();
  无效STU_funcTerm();#IFDEF __cplusplus
} //结束的externC
#万一


  // C ++  - 来源:sys_uni code_for_c.hpp
#包括sys_uni code.h为externC{
  无效STU_funcInit(){
    SystemThreadUtils :: funcInit();
  }
  无效STU_funcTerm(){
    SystemThreadUtils :: funcTerm();
  }
}

含义:你需要一个头是有效的C code和地宣告++函数需要从C调用的源C ++和少了点通话℃的委托功能

请参阅调用C ++从C文件功能为好。

I'm not an expert C++ programmer, and i have been recently doing a trick thing in C++ which is causing me the below issue.

Objective of my task: Specific non system thread (cooperative threading actually) safe module is duplicated to create a system thread safe version to support different needs in the system. But instead of creating sys_XXX functions to keep the compatibility, we have decided to create a namespace to protect the system thread version in a C++ header file. I can actually include this in the CPP file and work happily but i just realised my funcInit call is not called before it reaches the CPP file control. Unfortunately this init for the cooperative threading version is in a C file. Now i need to init my system thread safe version from the same place, but im have been blocked by the compilation error which are hard 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 snippets FYI :-

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

C source #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
}

解决方案

You cannot use namespaces in C, extern "C" also is not allowed in C. So what could be a solution: Your C++ module defines a set of functions in namespace SystemThreadUtils that need to get called in C-Code. Since you cannot do that directly, you will have to write a C-conformant wrapper around them:

//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 delegating function for each C++ function you need to call from C. The source is in C++ and just does the call.

See Calling C++ functions from C file as well.

这篇关于包括C ++与C源文件的命名空间的头文件导致编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆