You are not logged in.

#1 2009-06-29 17:47:51

MrAllan
Member
Registered: 2008-12-08
Posts: 132

Can't include libxml2

I'm trying to compile the following C file:

#include <libxml/parser.h>

int main() {
    return 0;
}

with gcc (gcc calc.c -o calc).

This results in an error (error: libxml/parser.h: No such file or directory). So I changed '#include <libxml/parser.h>' to '#include <libxml2/libxml/parser.h>'. This results in many errors of the kind:

/usr/include/libxml2/libxml/parser.h:1159: error: expected '=', ',', ';', 'asm' or '__atribute__' beforce 'xmlDocPtr'

I know libxml2 is installed, I checked it with pacman and OpenBox compiles without problems. I got the error on Arch and Mac OS X. It seems like this is a pretty basic mistake I'm making (very few results with google). Can anyone point me to the right direction?

Last edited by MrAllan (2009-07-29 16:04:13)

Offline

#2 2009-06-29 18:04:12

kowalski
Member
Registered: 2009-05-07
Posts: 82

Re: Can't include libxml2

Try

gcc -o calc calc.c $(xml2-config --cflags --libs)

This should give gcc the required flags and libraries to include.


He who says A doesn't have to say B. He can also recognize that A was false.

Offline

#3 2009-06-29 18:05:50

Trent
Member
From: Baltimore, MD (US)
Registered: 2009-04-16
Posts: 990

Re: Can't include libxml2

Leave it as <libxml/parser.h> and tell gcc to look in /usr/include/libxml2.

gcc -o calc -I/usr/include/libxml2 calc.c

Better yet, use the CFLAGS provided by xml2-config:

gcc -o calc $(xml2-config --cflags --libs) calc.c

To make things easier on yourself, consider using a Makefile.

edit:  Aargh... two minutes too slow... wink

Last edited by Trent (2009-06-29 18:07:37)

Offline

#4 2009-06-30 15:25:43

MrAllan
Member
Registered: 2008-12-08
Posts: 132

Re: Can't include libxml2

Thanks!

Offline

#5 2009-06-30 16:13:33

kowalski
Member
Registered: 2009-05-07
Posts: 82

Re: Can't include libxml2

Speaking of this matter,

pkg-config --cflags --libs $LIBNAME

provides a similar way for the necessary includes and flags for libraries listed in

$PKG_CONFIG_PATH

As in

/usr/lib/pkgconfig/gtk+-2.0.pc

He who says A doesn't have to say B. He can also recognize that A was false.

Offline

#6 2009-07-29 16:03:44

MrAllan
Member
Registered: 2008-12-08
Posts: 132

Re: Can't include libxml2

I followed your advice and created a makefile, but it doesn't work too well. I got this line in my makefile:

g++ -c $(CFLAGS) calc.cpp

But it only works if i define CFLAGS like so:

CFLAGS = -I/usr/include/libxml2 -lxml2 -lz -lm

And not if I define CFLAGS like so:

CFLAGS = $(xml2-config --cflags --libs)

Any ideas why this is happening?

Offline

#7 2009-08-04 19:30:19

raf_kig
Member
Registered: 2008-11-28
Posts: 143

Re: Can't include libxml2

That's because your syntax is incorrect.
use this instead:

CFLAGS := $(shell pkg-config libxml-2.0 --cflags)
LDFLAGS := $(shell pkg-config libxml-2.0 --libs)

Regards,

raf

Last edited by raf_kig (2009-08-04 19:31:35)

Offline

Board footer

Powered by FluxBB