我试图使用供应商的库与我的C++应用程序结合.库主要基于C,这通常不是extern "C"
个选项的问题,但我遇到了C++编译器不接受的问题.
我将代码简化为以下示例文件.头球.h代表suppier库main中的标题.c/cpp是我自己的文件.我的实际应用程序是一个C++应用程序,所以我想让它与主程序一起工作.cpp.
头球.h(注意第u64 u64;
行):
#ifndef HEADER_H
#define HEADER_H
#include <stdint.h>
typedef uint64_t u64;
union teststruct {
u64 u64;
struct {
u64 x:32;
u64 y:32;
} s;
};
#endif
main.c:
#include <stdio.h>
#include "header.h"
int main() {
union teststruct a;
a.u64=5;
printf("%x\n", a.u64);
return 0;
}
主要的cpp(与main.c相同,但有额外的extern "C"
条语句):
#include <stdio.h>
extern "C" {
#include "header.h"
}
int main() {
union teststruct a;
a.u64=5;
printf("%x\n", a.u64);
return 0;
}
使用以下行编译main.c
gcc -o test main.c
编译没有问题.但是,使用G+编译器使用命令编译C++版本
g++ -o test main.cpp
提供以下编译器错误:
In file included from main.cpp:12:0:
header.h:11:9: error: ‘u64’ does not name a type
u64 x:32;
^
header.h:12:9: error: ‘u64’ does not name a type
u64 y:32;
^
问题是,供应商对类型和变量名使用了相同的名称(u64),这似乎是一个坏主意,但gcc显然接受它.我不想更改库(即header.h),因为它非常大,这在代码中经常发生,我偶尔会得到更新.有没有办法让g++接受这种组合,或者有没有办法修改main.cpp使其编译without个更改头.H