jefdaj
jefdaj

Reputation: 2065

What's wrong with my Cmake libxml2 project?

This is my first time using C/C++, as well as CMake, so there could be lots of things wrong.

I've set up a minimal project using CMake and libxml2. It looks like this:

xmltest/
xmltest/source/
xmltest/source/CMakeLists.txt
xmltest/source/testxml.c
xmltest/source/libxml2/
xmltest/source/libxml2/CMakeLists.txt
xmltest/source/libxml2/lots of source files...

In xmltest/source/CMakeLists.txt I put:

PROJECT( XMLTEST )
CMAKE_MINIMUM_REQUIRED( VERSION 2.8 )

ADD_SUBDIRECTORY( libxml2 )

INCLUDE_DIRECTORIES(
    "${PROJECT_SOURCE_DIR}"/libxml2/include
)

And xmltest/source/libxml2/CMakeLists.txt was taken from http://epiar.net/trac/browser/Build/cmake/ThirdParty/LibXml2/CMakeLists.txt . I left it alone except for fixing LIBXML_SRC_FOLDER.

The rest of xmltest/source/libxml2/ was extracted from http://xmlsoft.org/sources/libxml2-sources-2.7.6.tar.gz .

I used cmake-gui to build the project in xmltest/build/, and it worked fine. MinGW compiled it to libxml2.a and I was proud.

Then I tried to add an executable #includeing libxml2.

I added this to the main CMakeLists.txt:

ADD_EXECUTABLE( xmltest
    xmltest.c
)

TARGET_LINK_LIBRARIES( xmltest
    xml2
)

and created xmltest.c:

#include <libxml/parser.h>

int main()
{
    return 0;
}

CMake still generates it without complaining, but when compiling I get

fatal error: libxml/parser.h: No such file or directory

I've tried a few variations on the #include line:

#include <libxml>
#include <libxml2>
#include <xml2>
#include <libxml/parser.h>
#include <libxml2/parser.h>
#include <xml2/parser.h>
#include "libxml2/include/libxml/parser.h"

but it hasn't helped. Any ideas?

Upvotes: 2

Views: 3074

Answers (1)

arrowd
arrowd

Reputation: 34411

Try to remove qoutes there:

INCLUDE_DIRECTORIES(
    "${PROJECT_SOURCE_DIR}"/libxml2/include
)

Also make sure, that include_directories() invocation is done before add_executable() call.

Finally, make #include clause in your source be relative to xmltest/source/libxml2/include.

EDIT: Okay, since it's right answer, let me explain CMake's behavior.

In CMake, there are strings and there are lists. Lists are actually strings too, but they are separated by ";". So, "aaa;bbb;ccc" is a string and a list, while "aaabbbccc" is just a string (well it's list with single element, but it's irrelevant).

If you write set(STR1 abc) or set(STR2 "abc") you will get same strings. But if you write set(STR1 a b c) you will get a list with 3 elements. It's the same as if you write set(STR1 "a;b;c").

So, you faced your problem because "${PROJECT_SOURCE_DIR}"/libxml2/include is actually a list with two dirs. If you pass it into include_directories() you will get these flags: -I${PROJECT_SOURCE_DIR} -I/libxml2/include which is obviously not what you wished.

Upvotes: 2

Related Questions