c ++和在单独的文件中包含(c++ and with in separate files)

编程入门 行业动态 更新时间:2024-10-25 00:33:59
c ++和在单独的文件中包含(c++ and with in separate files)

笔记:

我正在使用Apple LLVM 6.0版(clang-600.0.56)编译OSX(基于LLVM 3.5svn)

具体来说,我正在尝试从LibIIR编译一个单片源, 这是Laurence Withers 在这里维护的过滤器库。

我已经在这里看到了关于在同一个文件同时使用<complex>和<complex.h>答案。


建立:

我有一个像iir.h这样的文件:

#include <complex.h> #ifdef __cplusplus extern "C" { #endif ...

我有C ++源代码和头文件libiir++.cpp和iir++.h喜欢这样:

/*** libiir++.cpp ***/ // we need to include "iir.h" first, as it pulls in <complex.h>, which we need // to take effect before "iir++.h" pulls in <complex> #include "iir.h" // now remove the preprocessor definition of complex to _Complex, which is fine // for the C header but not good for the C++ header #undef complex #include "iir++.h" namespace IIR { ...

-

/*** iir++.h ***/ #include <complex> namespace IIR { ...

问题:

clang在编译时给出了以下错误:

./iir.h:570:15: error: expected ';' after top level declarator double complex iir_response_c(const struct iir_coeff_t* coeff, double freq); ^ ;

显然,新的<complex>导入没有发生 - 或#undef complex再次发生 - 但我不知道如何。 关于可能出现的问题或检查内容的任何建议?

Notes:

I am compiling on OSX using Apple LLVM version 6.0 (clang-600.0.56) (based on LLVM 3.5svn)

Specifically, I am trying to compile a monolithic source from LibIIR, a filter library maintained here by Laurence Withers.

I've already looked at this answer here about using both <complex> and <complex.h> in the same file.


Setup:

I have a file iir.h like so:

#include <complex.h> #ifdef __cplusplus extern "C" { #endif ...

I have C++ source and header files libiir++.cpp and iir++.h like so:

/*** libiir++.cpp ***/ // we need to include "iir.h" first, as it pulls in <complex.h>, which we need // to take effect before "iir++.h" pulls in <complex> #include "iir.h" // now remove the preprocessor definition of complex to _Complex, which is fine // for the C header but not good for the C++ header #undef complex #include "iir++.h" namespace IIR { ...

-

/*** iir++.h ***/ #include <complex> namespace IIR { ...

Problem:

clang gives me the following error when compiling:

./iir.h:570:15: error: expected ';' after top level declarator double complex iir_response_c(const struct iir_coeff_t* coeff, double freq); ^ ;

Evidently, the new <complex> import is not happening--or #undef complex is happening again--but I don't see how. Any advice on what might be going wrong, or what to check?

最满意答案

<complex.h>是一个C头,它与C ++不兼容。

C ++通过模式<c***>指定的标头定义C库兼容性。 因此, <complex.h>的C ++对应名为<ccomplex> 。 以下是C ++标准对此的评价:

标题<ccomplex>

标题的行为就像它只包含标题<complex> 。

如果您尝试使用C复数库,则只需获取C ++编号。

结论:您无法通过C ++编译器运行C复杂数学运算。 充其量,您可以使用预处理器根据__cplusplus生成等效程序。

例如,

#if __cplusplus # include <complex> typedef std::complex< double > cdouble; #else # include <complex.h> typedef double complex cdouble; #endif

注意, std::complex< double >和double complex是按照C ++ 14 [complex.numbers]§26.4/ 4和C11§6.2.5/ 13进行布局兼容的。 意图似乎是你可以将cdouble用于跨语言函数原型,尽管严格来说它取决于ABI。


顺便提一下,C ++标准确实定义了#include <complex.h>时会发生什么,但它没有任何意义:

每个C头,每个头都有一个名称为name.h行为,就好像每个由相应的cname头放在标准库命名空间中的名称放在全局命名空间范围内。 未指定是在名称空间std的名称空间作用域(3.3.6)中首先声明或定义这些名称,然后通过显式using-declarations (7.3.3)将这些名称注入到全局名称空间作用域中。

所以, #include <complex.h>应该给你一个global ::complex<T> 。 这是标准中的缺陷。

<complex.h> is a C header and it is not compatible with C++.

C++ defines C library compatibility through headers named in the pattern <c***>. So, the C++ counterpart to <complex.h> is named <ccomplex>. Here's what the C++ standard has to say about that:

Header <ccomplex>

The header behaves as if it simply includes the header <complex>.

If you attempt to use the C complex number library, you just get the C++ one instead.

Bottom line: you simply cannot run C complex math through a C++ compiler. At best, you can use the preprocessor to generate equivalent programs depending on __cplusplus.

For example,

#if __cplusplus # include <complex> typedef std::complex< double > cdouble; #else # include <complex.h> typedef double complex cdouble; #endif

Note, std::complex< double > and double complex are layout-compatible per C++14 [complex.numbers] §26.4/4 and C11 §6.2.5/13. The intent seems to be that you can use cdouble for cross-language function prototypes, although strictly speaking it depends on the ABI.


Incidentally, the C++ standard does define what happens when you #include <complex.h>, but it doesn't make any sense:

Every C header, each of which has a name of the form name.h, behaves as if each name placed in the standard library namespace by the corresponding cname header is placed within the global namespace scope. It is unspecified whether these names are first declared or defined within namespace scope (3.3.6) of the namespace std and are then injected into the global namespace scope by explicit using-declarations (7.3.3).

So, #include <complex.h> should give you a global ::complex<T>. This is a defect in the standard.

更多推荐

本文发布于:2023-08-05 05:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1427265.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   separate   files

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!