我已经看过以下关于同一主题的两篇文章: Post1 Post2 。我有类似的问题,但不一样(我猜)。所以把它贴在这里。抱歉,如果它仍然是重复的。
我有一个 C 静态库(libComm.a),其中包含以下内容的实现..
comm.h:
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <math.h>
.....
typedef struct _CDef {} CommDef;
我收到了这个文件。无论如何我都无法改变它。除此之外,我还有另一个 Cpp 库 (libfw.a):
fw.h:
namespace fw { namespace ipc {
class A { ...... };
} }
我的目标是在我的 C++ 应用程序中使用这两个库:
myCppApp.cpp
#include "fw.h"
extern "C" {
#include "comm.h"
}
namespace chat { namespace comm {
class CppApplication
{
private:
CommDef def;
fw::ipc::A ipc;
};
}
}
当我想编译它时,编译器找不到“fw::ipc::A”。但如果我不使用 C header 和相应的数据类型,一切都会正常。
我意识到这是因为我包含的头文件包含标准 C 包含文件。所以,我的问题是如何解决这个编译问题并最终成功链接到库,条件如下:
- 不更改提供的来源
- 由于第 1 点,未使用 C++11 包含相应的 CPP 定义
非常感谢您抽出时间。
最佳答案
问题在于 C 头文件用 #define
污染了预处理器。一种可能性是事后使用 #undef
进行清理:
extern "C" {
#include "comm.h"
}
#undef ipc
// ...
另一个选项是在包含 C 头文件之前为无法访问的名称添加别名:
#include "fw.h"
typedef fw::ipc::A fw_ipc_A;
extern "C" {
#include "comm.h"
}
// ...
fw_ipc_A ipc_;
关于c++ - 包括包含标准包含的 C 头文件(不含 C++11),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35293782/