What are the differences between IMPORTED target and INTERFACE libraries?

前端 未结 2 1283
长情又很酷
长情又很酷 2020-12-23 14:43

As I understand it INTERFACE libraries are like Visual Studio property sheets, so very useful. We can use it to link with static libs and propagate properties.

But I

2条回答
  •  误落风尘
    2020-12-23 15:21

    It seems like there is a lot of overlap. Say you have a shared library and headers on disk and you want to make it available so that bits of your CMake can do this

    target_link_libraries(my_target foo)
    

    and automatically link with it and get the necessary include directories.

    You can do it either like this:

    find_package(Foo)
    
    add_library(foo SHARED IMPORTED)
    set_target_properties(foo PROPERTIES
        IMPORTED_LOCATION ${FOO_LIBRARIES} # The DLL, .so or .dylib
        INTERFACE_INCLUDE_DIRECTORIES ${FOO_INCLUDE_DIR}
        INTERFACE_COMPILE_DEFINITIONS "ENABLE_FOO"
    )
    

    Or like this:

    add_library(foo INTERFACE)
    target_link_libraries(foo INTERFACE ${FOO_LIBRARIES})
    target_include_directories(foo INTERFACE ${FOO_INCLUDE_DIR})
    target_compile_definitions(foo INTERFACE "-DENABLE_FOO")
    

    They both work and behave identically as far as I can tell. There is even an 'imported interface library' available via add_library(foo INTERFACE IMPORTED) though that didn't seem to work and I have no idea what it is for.

    Frankly the docs don't really explain which you should use for libraries, and I'm afraid I don't find Angew's "that's not really what they were designed for" very compelling.

    I guess use either. I think the interface library is easier to understand and more consistent with the use of INTERFACE properties from internal libraries.

提交回复
热议问题