9

I have a source file that I can run via the terminal using

gcc source.c -I/usr/include/libxml2 -lxml2 -o output

but when I #include the source file which includes the libxml source files, the compiler complains that the libxml/xmlmemory.h: , libxml/parser.h:, libxml/xpath.h cannot be found : no such file or directory.

some_id
  • 29,466
  • 62
  • 182
  • 304

3 Answers3

6

You need always to keep the -I/usr/include/libxml2 in your gcc statement, so that it can find the header files.

Benoit Thiery
  • 6,325
  • 4
  • 22
  • 28
  • 1
    What if this file needs to compiled as part of a much larger system? Should I just copy the needed files into the directory where this source file kept? – some_id Apr 04 '11 at 21:02
  • Ahah. I hope you're not including the headers from _your_ header files. That would suck. Make sure you depend on libxml2 only internally (in your compilation units, i.e. .c/.cxx/.cpp files) – sehe Apr 04 '11 at 21:39
  • I include in the .c files. but doesnt work. I dont have the libxml files in the same directory as the .c I am compiling – some_id Apr 04 '11 at 21:46
  • @Helium3, yes whatever c files that needs to use libxml will have to be compiled with the -I option. – Benoit Thiery Apr 05 '11 at 04:58
5

You can also use xml2-config --cflags --libs to retrieve the compilation flags to add.

kjp@vbox:~/Dev/ScratchDir$ xml2-config --cflags --libs
-I/usr/include/libxml2
-lxml2
kjp@vbox:~/Dev/ScratchDir$
kjp
  • 3,086
  • 22
  • 30
0

It seems like the source is referencing libxml, not libxml2 Can you verify that

/usr/include/libxml2/libxml/xmlmemory.h

is in fact accessible? (try to view it)

You don't compile the headers, the headers just have to be accessible as 'description' of the what the libraries implement

sehe
  • 374,641
  • 47
  • 450
  • 633
  • Is it possible the files cannot be found because they arent in the same folder as the code being compiled? I include like so #include . i dont specify /usr/include/libxml2/libxml – some_id Apr 04 '11 at 20:51
  • It's what -I/usr/include/libxml2 is for – sehe Apr 04 '11 at 20:59
  • So how do I do this without calling I/usr/include/libxml2 -lxml2 when compiling? – some_id Apr 04 '11 at 21:34
  • Why would you do that? Make a compile script/makefile if you think it tedious. Using absolute paths is going to make your code unportable to other machines – sehe Apr 04 '11 at 21:38
  • The thing is that my code is part of a much larger system. Its an embedded project with multiple parts to the system. So it shouldnt have to be compiled with loads of command line arguments. So I should write a makefile? – some_id Apr 04 '11 at 21:45
  • Yup see my other comment here: http://stackoverflow.com/questions/5544074/cannot-compile-when-using-libxml/5544220#5544220 – sehe Apr 04 '11 at 21:46
  • @sehe : The link you posted looks to be the same URL as the page you included the comment on. Did you have a different page you wanted us to look at? ;-) – shellter Apr 04 '11 at 22:08
  • @shelter: it linked to a different answer with my other comment, ty – sehe Apr 04 '11 at 22:10
  • @sehe: I replied to your other comment. Im still not sure what to do. Because if the code will be compiled on a pandaboard, the libxml will need to be there anyway, and might have another path. Although the pandaboard will be running ubuntu. – some_id Apr 04 '11 at 22:27