[CMake] Cannot build sub-directory library if top-level CMakeLists.txt call install(EXPORT ...)

Ziji Hu davidhuzj at gmail.com
Tue Mar 13 02:34:23 EDT 2018


Hi all,

I'm a newbie of CMake. I'm trying to add a feature as a sub-library to
an existing application. But I met troubles of install(EXPORT ...)

My project source code structure shows as below:
app (the existing application)
     |-- top-level CMakeLists.txt
     |
     |-- sublib (my new feature)
     |        |-- src
     |        |-- include
     |        |-- CMakeLists.txt
     |
     |-- other existing src ...

I build sublib in the CMakeLists.txt inside my own feature as:
     ...
     add_library(sublib ${LIB_SRC})
     ...

I modify the top-level CMakeLists.txt in the existing application to
link sublib:
     ...
     add_subdirectory(subdirectory of sublib)
     ...
     target_link_directories(app sublib)
     ...

I thought it is enough. But CMake threw out an error:
CMake Error: install(EXPORT "appTargets" ...) includes target "app"
which requires target "sublib" that is not in the export set.

I guess it is because "app" is exported by install(EXPORT .. ) in the
top-level CMakeLists.txt. Thus I also install and export sublib.
I add the install and export into sublib CMakeLists.txt:
    ...
    install(TARGETS sublib
        EXPORT  sublibTargets
        ARCHIVE DESTINATION ${BIN_INSTALL_DIR}
        LIBRARY DESTINATION ${LIB_INSTALL_DIR}
        RUNTIME DESTINATION ${LIB_INSTALL_DIR}
        )

    install(EXPORT sublibTargets
        FILE sublib-config.cmake
        DESTINATION ${LIB_INSTALL_DIR}/cmake/sublib
        )
    ...
I also add find_package() in top-level CMakeLists.txt:
    ...
    find_package(sublib REQUIRED)
    target_link_directories(app sublib)
    ...

However, it becomes worse. sublib is not built at all and
sublib-config.cmake is not found.

Could you please tell me how fix the EXPORT issue?

Thank you.

Best regards,
David Hu


More information about the CMake mailing list