Dependencies across multiproject in CMake
-
Good afternoon,
can someone clarify please:- I have a project composed (for now) of a library and desktop project
- library uses network and sql, while desktop does not
I can't compile target desktop because it complains about network classes, which are not a part of this project at all (library itself is).
Root cmakelists:
cmake_minimum_required(VERSION 3.5) project( boilerplate LANGUAGES CXX VERSION 0.1 DESCRIPTION "boilerplate client") set(CMAKE_AUTOUIC ON) set(CMAKE_AUTOMOC ON) set(CMAKE_AUTORCC ON) set(CMAKE_CXX_STANDARD 17) set(CMAKE_CXX_STANDARD_REQUIRED ON) find_package(Qt6 REQUIRED COMPONENTS Core Widgets Gui LinguistTools) #above is used in both lib and the program qt_standard_project_setup() add_subdirectory(Core) add_subdirectory(Desktop)
Library cmakelists:
find_package(Qt6 REQUIRED COMPONENTS Network Sql) set(TS_FILES Core_en_GB.ts) qt_add_library(Core SHARED core.h core.cpp ${TS_FILES} logger.h logger.cpp ipmodel.h ipmodel.cpp core.qrc cookiejar.h cookiejar.cpp ) target_link_libraries(Core PRIVATE Qt6::Core) target_link_libraries(Core PRIVATE Qt6::Network) target_link_libraries(Core PRIVATE Qt6::Sql) target_link_libraries(Core PRIVATE Qt6::Gui) target_include_directories(Core INTERFACE ${CMAKE_CURRENT_SOURCE_DIR}) target_compile_definitions(Core PRIVATE BLACORE_LIBRARY) qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES})
Desktop cmakelists:
set(TS_FILES Desktop_en_GB.ts) set(PROJECT_SOURCES main.cpp blab.h blab.cpp blab.ui ${TS_FILES} ) qt_add_executable(Desktop MANUAL_FINALIZATION ${PROJECT_SOURCES} desktop.rc desktop.qrc ) qt_create_translation(QM_FILES ${CMAKE_SOURCE_DIR} ${TS_FILES}) target_link_libraries(Desktop PRIVATE Qt6::Core) target_link_libraries(Desktop PRIVATE Qt6::Widgets) target_link_libraries(Desktop PRIVATE Qt6::Gui) target_link_libraries(Desktop PRIVATE Core) set_target_properties(Desktop PROPERTIES ${BUNDLE_ID_OPTION} MACOSX_BUNDLE_BUNDLE_VERSION ${PROJECT_VERSION} MACOSX_BUNDLE_SHORT_VERSION_STRING ${PROJECT_VERSION_MAJOR}.${PROJECT_VERSION_MINOR} OUTPUT_NAME "Desktop boilerplate" MACOSX_BUNDLE TRUE WIN32_EXECUTABLE TRUE ) include(GNUInstallDirs) install(TARGETS Desktop BUNDLE DESTINATION . LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR} ) qt_finalize_executable(Desktop)
The exact error I get while compiling from the root level is:
In file included from "[..]/lib/core.h:6" from "[..]/Desktop/blab.h:5" from "[..]/Desktop/main.cpp:1" [..]/lib/cookiejar.h:4:10": fatal error: QNetworkCookieJar: No such file or directory
I can build the library itself just fine. I don't recall having such issues in the past.
-
You have to link PUBLIC to the libraries which you're using in your headers (which should be used by others). Please see the CMake documentation.
-
@JoeCFD In this project I followed what seems to be updated documentation from Qt, where they say the line in the lib's cmakelists
target_include_directories(Core INTERFACE ${CMAKE_CURRENT_SOURCE_DIR})
is sufficient (certainly seems that way, core.h is found in Desktop target).source https://doc.qt.io/qt-6/cmake-get-started.html
By calling target_include_directories, we make sure that the absolute path to the [library] directory is automatically added as an include path to all targets using our library.
Should I scrap this and do the usual, pure cmake way?
-
You have to link PUBLIC to the libraries which you're using in your headers (which should be used by others). Please see the CMake documentation.
-
@Christian-Ehrlicher My bad. Thank you, it worked! I need to up my cmake game...
-