1

I am new to CMake and having trouble understanding how to use it.

I have a cross-platform c++ project. It builds using CMake and my CMakeLists.txt lives in a folder called MyProject along with the project. I checked out a pugixml git submodule and it now lives under MyProject/pugixml, with its sources being under MyProject/pugixml/src. It however has its own CMake file: MyProject/pugixml/CMakeLists.txt.

How do I modify my CMakeLists.txt to also build and use pugixml? I tried something like

include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${GENERATED_HEADER_OUTPUT_DIRECTORY} pugixml/src)

and

add_library(PugiXML deps/pugixml/src/pugiconfig.hpp deps/pugixml/src/pugixml.cpp deps/pugixml/src/pugixml.hpp)

and

add_subdirectory(pugixml) in different combinations (these are examples I found on Github), but it doesn't work - I get unresolved external symbol errors, so I assume the cpp never got compiled.

Valentin
  • 1,108
  • 8
  • 18
  • 2
    `I get "unresolved external symbol" errors` - This means you need to **link** with a library. `add_subdirectory(pugixml)` just builds external library, but linking with it should be performed **explicitely** with [target_link_libraries](https://cmake.org/cmake/help/v3.7/command/target_link_libraries.html). – Tsyvarev Jan 12 '18 at 07:43
  • Also, consider using the more modern [`target_include_directories`](https://cmake.org/cmake/help/v3.10/command/target_include_directories.html) instead of the old `include_directories`. – ComicSansMS Jan 12 '18 at 08:05

1 Answers1

0

Try this:

add_subdirectory(pugixml)
target_link_libraries(MyProject PugiXML)

assuming PugiXML is the target name that is exposed by the CMake files in /pugixml.

Nibor
  • 1,236
  • 9
  • 23