园区型与C ++ 11 C链接 [英] Complex type with C linkage in C++11

查看:263
本文介绍了园区型与C ++ 11 C链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要包括C库的头在我的C ++ 11 code。现在,头提供涉及大量的的双重复杂所有的地方例程和数据结构。如,

I need to include the header of a C library into my C++11 code. Now, the header provides routines and data structures that involve plenty of double complex all over the place. E.g.,

#include <complex.h>
//..
typedef struct parameters
{
    // ...
    double complex Vud;
} parameters;
// ...
double complex polylog(int n, int m, double x);

我把这个文件到我的包在C ++ 11源的externC{#包括INCLUDE.H} (这是实际的文件名,相信它或不)。和g ++(4.7.3尝试和4.8.2),并铛(3.3)发疯,如果我有-std = C ++ 11加入。

I bring this file into my C++11 source wrapped in extern "C" { #include "include.h" } (that's the actual filename, believe it or not). And g++ (tried 4.7.3 and 4.8.2) and clang (3.3) go nuts if I have -std=c++11 added.

以百万计的G ++错误的行包括大量的:

The millions of lines of g++ errors include plenty of:

include/g++-v4/cmath:98:3: error: template with C linkage

和铛给出:

cmath:84:3: error: declaration conflicts with target of using declaration already in scope
  abs(double __x)
  ^
/usr/include/stdlib.h:773:12: note: target of using declaration
extern int abs (int __x) __THROW __attribute__ ((__const__)) __wur;
           ^
/usr/lib/gcc/x86_64-pc-linux-gnu/4.7.3/include/g++-v4/cstdlib:245:14: note: using declaration
  using std::abs;
             ^

我不知道怎么去解决这个问题。什么是做到这一点的正确方法?显然,这些必须是可互操作的,但我不知道的伎俩。

I am not sure how to get around this. What is the correct way to do this? Clearly these must be inter-operable, but I do not know the trick.

推荐答案

在100%的标准C ++,你简直不能包括在的externC块标准头。您将需要修改 INCLUDE.H 头是C ++ - 友好,首先包括的externC,并宣布了的externC里面自己的名字块。

In 100% standard C++, you just plain cannot include standard headers in an extern "C" block. You would need to modify your include.h header to be C++-friendly, by first including its required headers outside of extern "C", and declaring its own names inside an extern "C" block.

17.6.2.2标题[using.headers]

[...]

3译文单位应当包含一个头只能在外面任何外部声明或定义,并应在该翻译单元的任何在头部声明的实体的第一个引用之前词法包含头。

3 A translation unit shall include a header only outside of any external declaration or definition, and shall include the header lexically before the first reference in that translation unit to any of the entities declared in that header.

extern "C" {
#include <complex.h>
}

是无效的,因为头被遂把的声明中。这甚至适用,如果它的另一个标题,只有间接包含&LT; complex.h方式&gt;

什么可以共同实施工作,作为一种解决方法,如果修改 INCLUDE.H 是不切实际的,是先手动包括所有标题包括.H 将包括自己。假设这些报头使用适当的警卫进行第二次列入一个空操作,但事实上, INCLUDE.H 包括然后把它们不会引起任何错误。

What may work on common implementations, as a workaround if modifying include.h is impractical, is to first manually include all the headers that include.h would include itself. Assuming those headers use the appropriate guards to make a second inclusion a no-op, the fact that include.h includes them will then not cause any errors.

这篇关于园区型与C ++ 11 C链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆