Complex type with C linkage in C++11

我们两清 提交于 2019-12-05 09:42:49

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.

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