tags:

views:

548

answers:

3

Somehow this is the first time I've ever encountered this problem in many years of programming, and I'm not sure what the options are for handling it.

I am in the process of porting an application to Linux, and there is a header file in one of the directories that apparently has the same name as a header file in the standard C library, and when "cstdlib" is included from another file in that directory, it's trying to include the local file rather than the correct one from the standard library.

In particular, the file is named "endian.h", which is trying to be included from /usr/include/bits/waitstatus.h. So instead of including /usr/include/bits/endian.h it is trying to include ./endian.h. makes no difference

Is my only option to rename the endian.h in the project to something else, or is there a way that I can force the compiler to look in the same directory as the file that it's being included from first?


Edit:

Okay it was just a stupid mistake on my part. My Makefile was setting -I. , so it was looking in the current directory first. D'oh.

+4  A: 

There is an important difference between:

#include "endian.h" // Look in current directory first.

And

#include <endian.h> // Look in the standard search paths.

If you want the one in the current directory, use the quotes. If you want the system one, then use the angle brackets. Note that if you have put the current directory in the include path via the "-I" flag, then both might resolve to the one in the current directory, in which case you shouldn't use "-I" with the current directory.

Michael Aaron Safyan
I've updated the question to clarify this; the #include is in the standard library headers, and it uses angle brackets to include it's dependent file, but it's looking in my project file's path before it's looking in it's own path. Changing how I #include cstdlib doesn't make a difference here.
Gerald
Actually you hit it, I looked at the actual g++ command that was being executed and there was a "-I." Apparently the autotools are generating that in my makefiles, now I just have to figure out how to get rid of it.
Gerald
If the differencce were only that simple.
Martin York
Well, what you can do is put "endian.h" into a subfolder, so that, if done with angle brackets, you would have to include it with "#include <libraryname/endian.h>".
Michael Aaron Safyan
That's another idea, though I would just as soon change the name of the file as do that. Since this is a port from something that I don't "own" the original of, I wanted to avoid changing the names or locations of any of the existing files. Eliminating the "-I." from my Makefiles did the trick in any case.
Gerald
+1  A: 

If you include the file with "" instead of <>, the header should be first looked in the same directory as the file including it.

But if possible, renaming the file is much better IMHO: it will avoid problems if files are moved, when you change your build system, etc... The "" vs <> is compiler dependent, also (it is NOT mandated by the C standard to behave as g++ does).

David Cournapeau
+1  A: 

Maybe you can look at the "-I" and "-I-" options?

Thomas Padron-McCarthy