Pedro Faria
Pedro Faria

Reputation: 869

Problem at linking libxml2 library in my C project

I want to use the libxml2 C library in a project (I'm on Windows, and using gcc in CMD). I am trying to include it (as a static library) in a source file, so I can use its functions. But, gcc is having problems to find the header files of the library. So, how can I configure gcc compilation, so that it can finds the necessary header files?

My folder structure:

root
|
|--test.c
|--bin/--libxml2.a
|--lib/--libxml2.lib
|--include/--libxml/
             |--c14v.h
             |--parser.h
             |--xmlstring.h
             |--xmlversion.h
             |... and many other header files

As you can see above, I already compiled the library. So, I have its archive (libxml2.a) and lib file (libxml2.lib), and, all its header file (include/libxml/). Right now, I am trying to compile this simple test.c file below:

#include <stdio.h>
#include "libxml/parser.h"

int main () {
    return 0;
}

When I try to compile it with gcc test.c -o test -I/include/ -L/bin/ -llibxml2 command, I get the following error:

test.c:2:10: fatal error: libxml/parser.h: No such file or directory
    2 | #include "libxml/parser.h"
      |          ^~~~~~~~~~~~~~~~~
compilation terminated.

In my understanding, I used the -I gcc option, because the header files are in include folder. But it did not work. So, I tried to eliminate the include folder. So my folder structure become:

root
|
|--test.c
|--bin/--libxml2.a
|--lib/--libxml2.lib
|--libxml/
   |--c14v.h
   |--parser.h
   |--xmlstring.h
   |--xmlversion.h
   |... and many other header files

Now, with gcc test.c -o test -L/bin/ -llibxml2 I get the same error about a different file. So, gcc can now find libxml/parser.h, but it cannot find libxml/xmlversion.h, which is one of the many header files in the libxml folder.

In file included from test.c:2:
libxml/parser.h:15:10: fatal error: libxml/xmlversion.h: No such file or directory
   15 | #include <libxml/xmlversion.h>
      |          ^~~~~~~~~~~~~~~~~~~~~
compilation terminated.

What I am doing wrong here? Can I pass the include folder path to gcc (with -l or other option), so it can find all the header files stored in this folder?

Also, am I linking correctly the library with -l and -L options? There is something I am missing here?

Thank you for any help!

Upvotes: 2

Views: 1122

Answers (1)

Pedro Faria
Pedro Faria

Reputation: 869

As @KamilCuk pointed out in the comments, I was defining incorrectly the paths in the gcc options. So the correct command that solved the entire problem was:

gcc test.c -o test -Iinclude/ -Llib/ -l:libxml2.a

Upvotes: 2

Related Questions