I have Project A, a library, and Project B, an executable which depends on A. A and B are completely separate with regard to directory structure, build systems, etc. A depends on a third party library, X. As it happens in my case, A, B, and X all use CMake, but A is the only project under my control. How can I configure CMake for A so that B does not need to know about X?
For the moment, B gets a linker error, and the only way that I can find to make that go away is to have B reference X, which I don’t want to do. I have created a minimal example which recreates my problem. X happens to be an open source project called pugixml.
Here is file CMakeLists.txt for A:
cmake_minimum_required(VERSION 3.18) project(projA) add_library(projA proja.cpp proja.hpp) target_include_directories(projA PUBLIC pugixml-1.12/src) target_link_directories(projA PUBLIC pugixml-1.12/build/Debug) target_link_libraries(projA PUBLIC pugixml) add_executable(projATest projatest.cpp) target_link_libraries(projATest PRIVATE projA)
I can provide the C++ code if you want, projatest.cpp is just a copy of some example pugixml code. This compiles and links fine and the tests run and pass. Interestingly, projATest knows nothing about pugixml.
Here is file CMakeLists.txt for B:
cmake_minimum_required(VERSION 3.18) project(projB) add_executable(projB projb.cpp) target_include_directories(projB PRIVATE C:/projects/pugixml/projA) target_link_directories(projB PRIVATE C:/projects/pugixml/projA/build/Debug) target_link_libraries(projB PRIVATE projA)
Under visual studio, compilation fails with “error LNK2019: unresolved external symbol” relating to pugixml.
How can I configure the CMake file for Project A such that Project B does not need to know about pugixml?