4

If I want to compile a c source file that uses libxml, how do I setup libxml on mac osx so that my source file will just compile correctly and be ready to run?

I have been using XCode until now, but have switched to TextMate & Terminal, Do I need to create a makefile if I want to use libxml or how will this work?

Thanks

EDIT:

I use the current imports

#include <libxml/xmlmemory.h>
#include <libxml/parser.h>
1
  • Best thing to do is type: locate xmlmemory.h -- then copy the root folder location, paste it after -I to include the root folder. Commented Mar 2, 2014 at 18:55

4 Answers 4

5

The headers for the libxml2 distribution on 10.6 resides within /usr/include/libxml2/. Unfortunately, just the /usr/include part is within the system default header search path. Thus, you need to explicitly include it on the command line (or have your #includes also include the additional libxml2 folder). The library itself resides at the usual /usr/lib/libxml2.dylib.

Thus, use the following command to compile your program:

gcc source.c -I/usr/include/libxml2 -lxml2 -o output
Sign up to request clarification or add additional context in comments.

Comments

3

No setup required. Add -lxml2 to your link command.

If you just have a simple single-source file "project":

gcc yourSourceFile.c -I/usr/include/libxml2 -lxml2 -o executableProgram

2 Comments

I get errors like the following when trying it. gcc ParsingTestCode.c -lxml2 -o parser ParsingTestCode.c:4:30: error: libxml/xmlmemory.h: No such file or directory ParsingTestCode.c:5:27: error: libxml/parser.h: No such file or directory
@Helium3: Add -I/usr/include/libxml2 to your header search paths.
2

You can also xml2-config to get the compilation flags to use.

kjp@vbox:~/Dev/ScratchDir$ xml2-config --cflags --libs
-I/usr/include/libxml2
-lxml2
kjp@vbox:~/Dev/ScratchDir$

Comments

2

I vote for Kjp's answer with a simple modification. How about include the xml2-config to the gcc command itself -

g++ -o bin_name `xml2-config --cflags` main.c `xml2-config --libs`

This should work even if libxml is not installed on the default location OR the flag name is changed (in future).

CHEERS!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.