how to include 2 files with same name?

Feb 2, 2011 at 5:39pm
Hello everyone,

I have a project which uses two libraries which I created and both libraries contain a header-file with the same name (e.g. "a.h"). I'm not including these file in my main, but instead I include two differently-named files (one from each library) which have the statement
#include <a.h>
in them and this is where my linker gets confused. If I compile only the libraries separately then there's no problem, but as soon as I compile my main program, both libraries are linked to the same a.h-file. How can I solve this problem? Apart from renaming the a.h-files of course. :-)

Kind regards,
Squall83
Feb 2, 2011 at 5:47pm
By renaming one, or moving one and changing how you include it.

1
2
#include <a.h>
#include <someDirectoryThatIsNOTonTheIncludeDirectoryPath/a.h> 


But seriously, rename one.
Last edited on Feb 2, 2011 at 5:47pm
Feb 2, 2011 at 5:55pm
But seriously, rename one.


^ This.

One (or both) of the files are probably named poorly. The name of the file should reflect its contents. If the contents of each file is different, then they should have different names --- and if their contents are the same, then you don't need two different files.
Feb 3, 2011 at 5:06am
Feb 3, 2011 at 7:47am
@Moschops: I don't include them in the same file.

@Moschops + Disch: There are 2 libraries with different functions (image artifact detection and image artifact restoration), but which have some functionality in common (i.e. colospace conversions between e.g. RGB and YUV). Both libraries need this functionality as it must be possible to use them independently. This functionality is stored in a class which has the same name in both libraries.

@Duoas: I'm sorry, but my problem is not diamond-shaped. I looked at the image on Wikipedia (http://en.wikipedia.org/wiki/Diamond_problem), but if instead of class A you take 2 classes named A then this is what my problem is. If I take the naming of classes from that Wiki-article then my main class D includes classes B and C from different libraries and BOTH libraries have a class A in them which they use for internal purposes. The linker however links both libraries to the same class A.

edit: Also afaik include guards only help in case of two classes which have the same name, but not if there are two files with the same name. I used them already.
Last edited on Feb 3, 2011 at 8:57am
Feb 3, 2011 at 10:46am
edit: Also afaik include guards only help in case of two classes which have the same name, but not if there are two files with the same name. I used them already.


That's exactly the opposite of what they're usually used for. As generally used, they stop you including the exact same file twice. I have never seen them applied to individual classes, although there's no reason you couldn't do that.

I thought your problem was that every time your preprocessor goes looking for the file named a.h, it will find one of them first and use that one every time. Linkers do not link to *.h files - they link to compiled object files. Your *.h files are included by the preprocessor, before compilation.

@Moschops: I don't include them in the same file.


The preprocessor doesn't care what file you include them in; it will follow the same search pattern and find the same one every time. If you can't rename one, then moving it off the search path and then adding the path to the front of its name when you include it will ensure that you can specify which one is being included. It's actually quite common; for example, I often see this sort of thing:

#include <OpenGL/OpenGL.h> where all the header files for a particular library are tucked away together in a subdirectory off the principal search path.

This functionality is stored in a class which has the same name in both libraries.


So your problem isn't just that you've got two header files with the same name, which is easily dealt with. You've got two classes with the exact same name? Presumably, that won't compile as it will look like a class redefinition. You could get round that with namespaces.

Duoas wrote:
Er,


Duoas, you're typing. Not speaking. You don't have to write down exactly what you would say if this were spoken conversation. You can leave out the filler words and mumbling. :)
Last edited on Feb 3, 2011 at 11:22am
Feb 3, 2011 at 11:09am
closed account (z05DSL3A)
I might have miss something here but if you have two libraries that both implement the functionality, would it not be best to separate that functionality out into its own library?

It is hard to visualise the problem when taking in generalities and not stating the exact problem.
Last edited on Feb 3, 2011 at 11:11am
Feb 3, 2011 at 11:19am
It is hard to visualise the problem when taking in generalities and not stating the exact problem.


It is. Given that the OP wrote these, ripping the guts out rewriting them into a better whole shouldn't be beyond the scope of the OP's efforts.
Last edited on Feb 3, 2011 at 11:21am
Feb 3, 2011 at 12:46pm
I now use relative paths and it works. Also not all of my includes used the quoted form. Sometimes I used <>, which is kinda silly if the header file I seek is actually in the same folder as the file which includes it.

Also extracting the functionality into a separate library is an option as well. Thanks for pointing it out, I will keep it in mind.

Thanks for the help, everyone! :-)
Topic archived. No new replies allowed.