lt;string.hgt; conflicting with my own String.h(lt;字符串.hgt;与我自己的 String.h 冲突)
问题描述
我有一个在 g++ 中编译正常的项目(我现在看不到版本),现在在 xCode 上却不是.
我认为我现在遇到了问题......我的项目中有一个 String.h 文件,看起来 xCode 编译器(即 gcc)正在尝试从 < 添加我自己的字符串文件.cstring >...我不确定,但看看这张图片
http://www.jode.com.br/Joe/xCode1.png
I have a project that was compiling ok within g++(I can't see the version right now) and now on xCode it is not.
I think that I got the problem now... I have a String.h file in my project and it seems tha the xCode compiler(that is gcc) is trying to add my own string file from the < cstring >... I am not sure of it, but take a look at this picture
http://www.jode.com.br/Joe/xCode1.png
从它的外观来看,它包含我自己的而不是系统文件,我想知道...不应该#include <文件 > 是一个系统包括?因为 <> ?系统不应该在自己的路径中包含文件,而不是在我的应用程序的原始路径中包含文件吗?
正如我所说,我不确定这是否会发生,因为过去 2 天我只是迁移到 osx...
我打算将我的类和文件名更改为不冲突,所以它会起作用,如果这真的是问题,但我想知道,应该有另一种方法来做到这一点,因为现在我的项目不是那么大我可以在一段时间内做到这一点,但如果项目更大怎么办?很难更改所有包含和类名...
from what it looks like, it is including my own instead of the system file, I was wondering... shouldn't #include < file > be a system include? because of the < > ? and shouldn't the system include a file within its own path and not the original path of my application?
As I said, I am not sure if this is what happening because I am just migrating to osx these past 2 days...
I was going to change my class and file name to not conflict, so it would work, if this is really the problem, but I was wondering, there should be another way to do this, because now my project isn't that big so I can do this in some time, but what if the project was bigger? it would be dificult to change all includes and class names...
感谢任何帮助
谢谢,
乔纳森
推荐答案
使用与标准头文件相同的名称命名您的头文件,例如 string.h 并简单地使用 #include <String.h>
自找麻烦(大小写的不同在某些平台上没有区别).
Naming your headers with the same name as standard headers like string.h and including them simply with #include <String.h>
is asking for trouble (the difference in casing makes no difference on some platforms).
但是,正如您所说,在命名标题时很难尝试提前弄清楚它们是什么.因此,最简单的方法是将包含路径设置为在您的标头所在的子目录之外的一个目录级别,例如:
As you said, however, it would be difficult to try to figure out what those are in advance when naming your headers. Thus, the easiest way to do this is to set to set your include path one directory level outside of a sub-directory in which your headers reside, ex:
#include <Jonathan/String.h>
现在您不必担心 String.h 文件名是否与您正在使用的库中的某些内容冲突,除非它们碰巧也包含 <Jonathan/String.h>
这不太可能.所有体面的第三方库也这样做.例如,我们在 boost 中不包含 <function.hpp>
,而是包含 <boost/function.hpp>
.与 GL/GL.h 相同,而不是简单的 GL.h.这种做法在很大程度上避免了冲突,您不必通过将 String.h 重命名为 Text.h 之类的名称来解决问题.
Now you don't have to worry about whether the String.h file name conflicts with something in one the libraries you are using unless they happen to also be including <Jonathan/String.h>
which is unlikely. All decent third-party libraries do this as well. We don't include <function.hpp>
in boost, for instance, but instead include <boost/function.hpp>
. Same with GL/GL.h instead of simply GL.h. This practice avoids conflicts for the most part and you don't have to work around problems by renaming String.h to something like Text.h.
这篇关于<字符串.h>与我自己的 String.h 冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:<字符串.h>与我自己的 String.h 冲突
基础教程推荐
- 在 C++ 中循环遍历所有 Lua 全局变量 2021-01-01
- Windows Media Foundation 录制音频 2021-01-01
- 如何“在 Finder 中显示"或“在资源管理器中显 2021-01-01
- 为什么语句不能出现在命名空间范围内? 2021-01-01
- 为 C/C++ 中的项目的 makefile 生成依赖项 2022-01-01
- 从 std::cin 读取密码 2021-01-01
- 使用从字符串中提取的参数调用函数 2022-01-01
- 管理共享内存应该分配多少内存?(助推) 2022-12-07
- 如何在不破坏 vtbl 的情况下做相当于 memset(this, ...) 的操作? 2022-01-01
- 如何使图像调整大小以在 Qt 中缩放? 2021-01-01