库尝试包含< string.h>但包括“string.h”从我的项目,如何防止? [英] Library tries to include <string.h> but includes "string.h" from my project, how to prevent?

查看:310
本文介绍了库尝试包含< string.h>但包括“string.h”从我的项目,如何防止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 string.h 在我的项目中添加到我的头搜索路径与 -I 我使用CMake并从项目根目录而不是 string.h 所在的目录)进行编译。



项目使用一个外部库,它试图在其中一个头文件中包含 #include< string.h> 最终包括我的 string.h (因为它在标题搜索路径上包含<> )。



以下是显示此错误讯息:

 在/path/to/project/src/random_source_file.cpp:3中包含的文件中:
在包含在/usr/local/include/SDL2/SDL.h:67中的文件中:
在/usr/local/include/SDL2/SDL_main.h:25中包含的文件中:
在包含在/usr/local/include/SDL2/SDL_stdinc.h:60中的文件中:
在/path/to/project/src/string.h:7中包含的文件中:

SDL_stdinc.h 的第60行,有 #include< string.h> 。 / p>

如何解决这个问题?

解决方案



1)我创建的包含文件总是通过相对路径包含,并且不会包含在环境PATH中。

  #ifndef DTB_SUPPORT_HH 
#include../../bag/src/dtb_support.hh
#endif

2)我只通过环境PATH包含库文件,并且不使用相对路径。

  #include< string> 






编辑 - 起点:



我使用的相对路径包含源自以下经验:



作为一个承包商大小努力,I
发现原因添加一个符号到一个简单的文件,让它称为Foo.hh。



我用他们的工具找到 Foo.hh,修改它,并编辑了我正在使用的新符号
文件我放在那里。



然而,在重建期间,编译器抱怨的符号是
未知。



所以我双重检查了这两个文件,并意识到必须有另一个
Foo.hh。



然后我打开了整个系统的构建,使用
编译器在编译输出中报告所有的文件
对于每个编译单元(我认为-H?)。



刚开始我只访问了我感兴趣的编译单元
, .hh。



好奇心是我最好的,所以我通过编译日志
grepp,发现了成千上万的Foo.hh。我必须把这些
行汇集在一起​​,并用完整路径对它们进行排序。



结果是有一条Foo.hh的5个路径。比较他们,5个文件有3个不同的版本。



(FYI - 我从第一个删除我的符号,添加新符号到第二, )忽略其他3个文件。)


I have a string.h in my project added it to my header search path with -I (because I'm using CMake and compiling from the project root directory rather than the directory where string.h is located).

The project uses an external library that tries to #include <string.h> (the standard C header) in one of its header files that I include, and accidentally ends up including my string.h (because it was on the header search path that inclusion with <> uses).

Here's the (edited) error message that reveals this:

In file included from /path/to/project/src/random_source_file.cpp:3:
In file included from /usr/local/include/SDL2/SDL.h:67:
In file included from /usr/local/include/SDL2/SDL_main.h:25:
In file included from /usr/local/include/SDL2/SDL_stdinc.h:60:
In file included from /path/to/project/src/string.h:7:
etc.

At line 60 in SDL_stdinc.h there's the #include <string.h>.

How can I get around this?

解决方案

2 rules I use:

1) Include files that I have created are always included via a relative-path, and never included in an environment PATH.

#ifndef DTB_SUPPORT_HH
#include "../../bag/src/dtb_support.hh"
#endif

2) I only include Library files thru an environment PATH, and never using a relative path.

#include <string>


edit - origins:

My use of relative-path include grew out of the following experience:

As a contractor (one of several hundred) on a MLOC size effort, I found cause to add a symbol to a simple file, lets call it "Foo.hh".

I used their tool to find "Foo.hh", modified it, and edited the file I was working on to use the new symbol I put there.

During rebuild, however, the compiler complained the symbol was unknown.

So I double checked both files, and realized there must be another "Foo.hh".

I then lauched a build of the entire system, with option that caused the compiler to report in the compilation output all the files that were included for each compilation unit (I think -H?).

At first I only visited the compilation unit I was interested in, and found the second "Foo.hh".

Curiosity got the best of me, so I grepped thru the compilation log, and found thousands of includes of "Foo.hh". I had to gather these lines together, and sort them with the full path.

It turns out there were 5 paths to a "Foo.hh". Comparing them, the 5 files were of 3 different versions.

(FYI - I removed my symbol from the 1st, added the new symbol to the 2nd, and (by direction) ignored the other 3 files.)

这篇关于库尝试包含&lt; string.h&gt;但包括“string.h”从我的项目,如何防止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆