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

标签 c++ c compilation header namespaces

我不是专业的 C++ 程序员,我最近在 C++ 中做了一个技巧,导致我出现以下问题。

我的任务目标:复制特定的非系统线程(实际上是协作线程)安全模块以创建系统线程安全版本以支持系统中的不同需求。但是我们没有创建 sys_XXX 函数来保持兼容性,而是决定在 C++ 头文件中创建一个命名空间来保护系统线程版本。我实际上可以将其包含在 CPP 文件中并愉快地工作,但我刚刚意识到我的 funcInit 调用在到达 CPP 文件控件之前未被调用。不幸的是,协作线程版本的这个初始化在 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

源代码和标题片段仅供引用:-

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 中使用命名空间,extern "C" 在 C 中也是不允许的。那么可能的解决方案是:您的 C++ 模块在命名空间 SystemThreadUtils< 中定义了一组函数 需要在 C 代码中调用。由于您不能直接这样做,因此您必须围绕它们编写符合 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++ 函数声明一个委托(delegate)函数。源代码在 C++ 中,只是执行调用。

参见 Calling C++ functions from C file

关于c++ - 在 C 源文件中包含带有命名空间的 C++ 头文件导致编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16058245/

相关文章:

c++ - 如何链接我自己的 .so 文件而不是操作系统包 .so 文件?

c++ - 使用 cin 的良好输入验证循环 - C++

c++ - 将类对象传递给内核

c++ - 在运行时以编程方式检测 CPU 架构

c - C 中具有可变二维的二维数组

c++ - 循环遍历两个不同长度的 vector 时的优化

java - Eclipse 不编译 java 文件(已经尝试了可能重复的答案)

c++ - 警告 C4099 : type name first seen using 'class' now seen using 'struct' (MS VS 2k8)

c++ - 永不结束的 war 游戏

c - 在 C 中读取和存储字符串列表的最节省内存的方法