如何包含 C 系统头而不是 C++ C++ 中同名的系统头文件程序?
我的 Mac OS X 系统似乎有几个不同版本的头文件 complex.h
,并且它们不兼容。
/usr/include/complex.h
定义了_Complex
数字的 C99 实现,/usr/include/c++/4.2.1/backward/complex.h< /code> 是 C++ 标头的一个薄包装,没有
.h
,它定义了std::complex
数字的 C++ 实现。
我的问题是我有一个使用 C99 复数编译的 C 库,并且我需要将我的 C++ 程序链接到它。然而,它的包含文件引用
,并且在编译我的程序时,g++ 会选择 C++ 向后兼容标头,然后一切就乱了套。
我尝试过将 -I/usr/include
标志传递给 g++,但没有帮助。
在包含
时,如何强制 g++ 使用 C 标头而不是 C++ 标头?
My Mac OS X system seems to have several different versions of the header file complex.h
, and they are incompatible.
/usr/include/complex.h
defines the C99 implementation of_Complex
numbers,/usr/include/c++/4.2.1/backward/complex.h
is a thin wrapper around the C++ header without the.h
, that defines the C++ implementation ofstd::complex
numbers.
My problem is that I have a C library compiled using C99 complex numbers, and I need to link my C++ program against it. However, its include file references <complex.h>
, and when compiling my program, g++ picks up the C++ backwards-compatibility header instead, and all hell breaks loose.
I've tried passing a -I/usr/include
flag to g++, but it didn't help.
How do I force g++ to use the C header instead of the C++ one when including <complex.h>
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这不是一个完全最佳的解决方案,但如果您只是想确保在不修改代码的情况下获得所需版本的标头,您可以尝试使用
-nostdinc
和- 的组合nostdinc++
带有-I/usr/include
标志。如果我正确理解文档,这应该会阻止 gcc 查看系统头文件的标准列表,而是遵循-I
标志提供的那些文件。这应该会推翻 Os X 自动将向后兼容目录包含在标头搜索列表中的决定。然而,使用这些选项您可能无法编译。 GCC 对于您提供带有
-I
标志的目录的情况做了规定,该标志已经存在于默认系统头搜索列表中(它将忽略无关的-I
标志) )。希望它足够聪明,注意到系统搜索列表已被清除(通过-nostdinc
标志),从而允许它添加属性-I/usr/include
。It's not an entirely optimal solution, but if you simply want to ensure that you get the desired version of the header without modifying the code, you could try using a combination of the
-nostdinc
and-nostdinc++
with a-I/usr/include
flag. If I understand the documentation correctly, that should prevent gcc from looking at the standard list of system header files and instead defer to those supplied with the-I
flag. That should override Os X's decision to automatically include the backward compatibility directory on the header search list.There is the possibility however, that with those options you will be unable to compile. GCC has provisions for the case where you supply a directory with the
-I
flag that are already present on the default system header search list (it will ignore the extraneous-I
flag). Hopefully it is smart enough to notice that the system search list has been cleared (by the-nostdinc
flags), allowing it to property add-I/usr/include
.我想我应该回来接受答案。
最上面的评论基本上是“不要这样做”。我最终没有这样做。
I figured I should come back and accept an answer.
The top comment basically says "Just don't do it". I ended up not doing it.
假设 C++ 代码使用的 C 库中的函数都没有
_Complex
参数或返回类型,则可以通过包围#include来修改 C 头文件。 h>
指令,以及标头中使用_Complex
的任何其他内容,以及#ifndef __cplusplus
/#endif
指令,以便认为C++ 代码不会看到它并且会被它迷惑。另一方面,如果您绝对无法修改 C 标头,则可以将函数原型和 C++ 代码使用的其他任何内容复制到 C++ 代码包含的新标头中。编辑
如果您真的只想消除标题歧义,您可以尝试
#include "/usr/include/complex.h"
而不是简单地# include
,但是像这样使用标准库头的绝对路径并不是一个好的做法,而且可能不可移植。Assuming that none of the functions from the C library that are used by the C++ code have
_Complex
parameters or return types, you can modify the C header file by surrounding the#include <complex.h>
directive, as well as anything else in the header that uses_Complex
, with#ifndef __cplusplus
/#endif
directives so that the C++ code won't see it and be confused by it. On the other hand, if you absolutely can't modify the C header, you can copy the function prototypes and anything else used by the C++ code into a new header that is included by the C++ code.EDIT
If you really have your heart set on just disambiguating the headers, you can try
#include "/usr/include/complex.h"
instead of simply#include <complex.h>
, but using an absolute path to a standard library header like this isn't good practice and probably isn't portable.用 extern 块将代码中的标头包裹起来,即
我认为这可以解决问题。对上面的代码格式表示抱歉,该功能显然在 iPhone 上不起作用。
Wrap the headers in your code with an extern block, I.e
I think that will do the trick. Sorry about code formatting above, that functionality does not work on iPhone apparently.