How to modify include path for a specific include
Question
I've got an application which is using both iconv functionality and libxml2.
libxml2 is installed in /usr/local
, so I am using the compiler flag -I/usr/local/include
. There is also a standalone libiconv installation in /usr/local
which I don't want to use (I want to use the one in glibc).
Within my application code, I can sort out the iconv problem by doing:
#include </usr/include/iconv.h>
However, the problem is that the libxml2 stuff is also using iconv for it's own internal purposes. And the libxml2 headers just do:
#include <iconv.h>
Is there any way around that? For example can I do anything in my code where I am including the libxml headers, to tell it where to search for iconv?
Solution
First, include the correct <iconv.h>
before any libxml2 headers, even in sources that don't use iconv. This will prevent libxml headers from including any other version (assuming the header guards are the same...).
For a long-term fix, you will need to fix your system (because it is, in fact, broken). You cannot install packages in /usr/local
and then later expect to be able to enable or disable them individually. Instead, install your packages with separate prefixes. For example, install libxml2 in /opt/libxml2
, and install iconv in /opt/iconv
.
OTHER TIPS
If you do -I/usr/include -I/usr/local/include
, gcc should look in /usr/include first...except that gcc treats system headers differently, so that doesn't work. As an ugly hack, you can copy /usr/include/iconv.h to a different directory and specify it in a -I flag before /usr/local/include.