c++ - 从 CPP 文件中的 C 文件访问变量

标签 c++ c

我有一个程序,我被迫将一些 C 和 C++ 代码融合在一起。我在 C 中定义了一些全局变量,我需要在 C 和 C++ 文件中访问它们,但无法弄清楚。这是我所拥有的,它适用于 C 文件,但不适用于 CPP:

C.h

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

谢谢, 本

最佳答案

Assuming the question is ‟how do I access the variable test here?”

像这样:

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

关于c++ - 从 CPP 文件中的 C 文件访问变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13198689/

相关文章:

c - 我想过滤数字流以获取某个范围内的值

c - 我无法理解教授关于 malloc 的代码

c - 每次调用 strtok_r() 时都会出现段错误,即使在简单的情况下也是如此

c++ - 从 C/C++ 代码中删除注释

c++ - 基于 OpenGL 的 UI 的几何表示

c - libmemcached 问题

c++ - 创建大图像

c++ - template <pointer> 获取指向对象的类型

c++ - 当 QTableWidget 中的单元格已更新时,如何更新 csv 文件?

c++ - 数组作为函数参数查找数组长度时出错