有没有办法(ab)使用C预处理器来模拟C中的命名空间?
我正在考虑以下几点:
#define NAMESPACE name_of_ns
some_function() {
some_other_function();
}
Run Code Online (Sandbox Code Playgroud)
这将被翻译为:
name_of_ns_some_function() {
name_of_ns_some_other_function();
}
Run Code Online (Sandbox Code Playgroud) 我是C和C++的新手.但我有一些C++函数,我需要从C调用它们.我做了一个我需要做的例子
main.c:
#include "example.h"
#include <stdio.h>
int main(){
helloWorld();
return 0;
}
Run Code Online (Sandbox Code Playgroud)
example.h:
#ifndef HEADER_FILE
#define HEADER_FILE
#ifdef __cplusplus
extern "C" {
#endif
void helloWorld();
#ifdef __cplusplus
}
#endif
#endif
Run Code Online (Sandbox Code Playgroud)
example.cpp:
#include <iostream.h>
void helloWorld(){
printf("hello from CPP");
}
Run Code Online (Sandbox Code Playgroud)
它只是不起作用.我仍然收到未定义参考错误_helloWorld在我main.c.这个问题在哪里?
我正在使用 Keil uVision 并且不断收到此错误:
C:\Keil_v5\ARM\ARMCC\bin\..\include\rw/_defs.h(781): error: #20:
identifier "namespace" is undefined
Run Code Online (Sandbox Code Playgroud)
什么可能导致此错误?命名空间不是自动定义的吗?