How to create a cmake header-only library that depends on external header files?

后端 未结 3 352
温柔的废话
温柔的废话 2020-12-30 04:56

I have a project with the following file structure:

project
 |
 |-------> lib1
 |           |----> lib1.h
 |
 |-------> lib2
 |           |---->          


        
3条回答
  •  鱼传尺愫
    2020-12-30 05:47

    As stated in the comments, target_include_directories should be given a path to a directory, not to a file.

    Moreover, if you want to create a dependency for lib2 on lib1, you should do it through target_link_libraries: a dependency is not only about include directories, but also about compile options, definitions, target properties...

    target_sources doesn't work with interface libraries. From this answer, You can use a custom target without commands to associate the sources to a target without impacting the build process (for msvc, QtCreator and other GUI-based tools, this makes the sources accessible through the IDE; AFAIK it's useless for other build tools).

    Your cmake may look like this:

    add_library(lib1 INTERFACE)
    target_sources(lib1 INTERFACE lib1.h)
    
    target_include_directories(lib1 INTERFACE
        "${PROJECT_SOURCE_DIR}/lib1"
    )
    
    add_library(lib2 INTERFACE)
    if(MSVC)
        add_custom_target(lib2.headers SOURCES lib2.h)
    endif()
    
    target_include_directories(lib2 INTERFACE
        "${PROJECT_SOURCE_DIR}/lib2"
    )
    
    target_link_libraries(lib2 INTERFACE lib1)
    
    add_executable(project main.cc)
    target_link_libraries(project lib2)
    

    Advanced tip: you can specify a different directory in target_include_directories for the build tree and the install tree (see documentation):

    target_include_directories(lib1 INTERFACE
        $
        $
    )
    

提交回复
热议问题