Complex type with C linkage in C++11

╄→гoц情女王★ 提交于 2019-12-07 06:11:45

问题


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);

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.

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

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

And clang gives:

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.


回答1:


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 Headers [using.headers]

[...]

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.

Doing

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

is invalid, because the header is then included inside of a declaration. This applies even if it's another header that only indirectly includes <complex.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.



来源:https://stackoverflow.com/questions/22115996/complex-type-with-c-linkage-in-c11

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!