2012-11-02 58 views
1

我有一个程序,我不得不将C和C++代码融合在一起。我已经在C中定义了一些全局变量,我需要在C和C++文件中访问它们,但无法解决它。下面是我有什么,这在C文件的工作,但不是在每次通话费用:从CPP文件中的C文件访问变量

#ifdef __cplusplus 
extern "C" { 
#endif 

#ifndef _COMMON_H_ 
#define _COMMON_H_ 

extern char test[100]; 

#ifdef __cplusplus 
} 
#endif 

抄送

#include <windows.h> 
#include <stdio.h> 
#include "C.h" 

char test[100] = "value"; 

CPlusPlus.cpp

#include "C.h" 

int TestFunction() { 
    // I need to access variable test here 
} 

谢谢, 奔

+0

那么,什么不起作用?编译器错误?链接?运行? –

+0

问题是什么? – Nawaz

+2

你错过了'C.h'中的'#endif'。 –

回答

3

假设的问题是“我怎么访问变量test在这里吗?”

像这样:

#include "C.h" 
#include <cstring> 
int TestFunction() { 
    return strcmp(test, "volvo"); 
} 
+0

这真棒! :) –

+0

这就是我正在做的。我不知道为什么它会返回一个空白字符串,但干净和重建使它开始工作。很奇怪。 – Fmstrat