Header files with prefix in CMake project

后端 未结 2 1135
闹比i
闹比i 2020-12-01 19:15

I have set up a CMake project whose directory structure looks as follows:

src/
--CMakeLists.txt
--libA/
----CMakeLists.txt
----foo.h
----foo.cpp
--main/
----         


        
2条回答
  •  伪装坚强ぢ
    2020-12-01 19:45

    This is an old question but I was having exactly the same issue. I ended up getting around this by adding an export_headers() function that creates symbolic links to the headers within the binary:

    function(export_headers TARGET HEADER_SOURCE_DIR HEADER_DEST_DIR)
    
        # Put all headers that are in the source directory into EXPORT_HEADERS variable
        file(GLOB_RECURSE EXPORT_HEADERS CONFIGURE_DEPENDS 
            RELATIVE "${HEADER_SOURCE_DIR}" 
            "${HEADER_SOURCE_DIR}/*.h"
        )
    
        # For each header that will be exported
        foreach(HEADER ${EXPORT_HEADERS})
    
            # Get the directory portion that needs to be created        
            get_filename_component(HEADER_DIRECTORY "${HEADER}" DIRECTORY)
    
            # Create the directory  
            add_custom_command(TARGET ${TARGET} POST_BUILD
                COMMAND ${CMAKE_COMMAND} -E make_directory "${HEADER_DEST_DIR}/${HEADER_DIRECTORY}"
            )
    
            if (MSVC)
    
                # Make a hard link to the file
                add_custom_command(TARGET ${TARGET} POST_BUILD
                    COMMAND if not exist "${HEADER_DEST_DIR}/${HEADER}" \( mklink /h "${HEADER_DEST_DIR}/${HEADER}" "${HEADER_SOURCE_DIR}/${HEADER}" \) 
                )
    
            else()
    
                # Make a symbolic link to the file
                add_custom_command(TARGET ${TARGET} POST_BUILD
                    COMMAND ln -sf "${HEADER_SOURCE_DIR}/${HEADER}" "${HEADER_DEST_DIR}/${HEADER}"
                )
    
            endif()
    
    
        endforeach(HEADER)
    
    endfunction()
    

    You would call this with something like:

    add_library(libA STATIC ${LIBA_SOURCES}
    export_headers(libA ${CMAKE_CURRENT_SOURCE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/include/libA)
    target_include_directories(libA INTERFACE ${CMAKE_CURRENT_BINARY_DIR}/include)
    

    Then, if you link to libA, you will be able to #include .

提交回复
热议问题