[CMake] Good practice: using INTERFACE-libraries in FindABC.cmake?

Nils Gladitz nilsgladitz at gmail.com
Mon Feb 29 09:43:11 EST 2016


On 02/29/2016 03:34 PM, Patrick Boettcher wrote:
> Hi list,
>
> I came across the INTERFACE-type of libraries when writing a
> FindModule.cmake-file for custom libraries installed by my
> project.
>
> Here is what I'm doing after having found the libraries and the
> determined the paths: LIB1 is the library and LIB1_INCLUDE_DIRS its
> include-dirs:
>
>    add_library(name INTERFACE)
>
>    target_link_libraries(name INTERFACE ${LIB1})
>
>    if(FFTW3_FOUND)
>    	target_include_directories(name INTERFACE
>    		{$FFTW3_INCLUDE_DIRS})
>    	target_link_libraries(name INTERFACE ${FFTW3_LIBRARIES} )
>    endif()
>
>    target_include_directories(name INTERFACE ${LIB1_INCLUDE_DIRS})
>
>    # need c++11 compile options for if-name
>    set_property(TARGET name PROPERTY
> 		INTERFACE_COMPILE_FEATURES cxx_range_for)
>
> This makes that in the CMakeLists.txt which includes my package-file a
> user needing libname for his executable does:
>
>    add_executable(main main.cpp)
>    target_link_libraries(main name)
>
> This will
>
> 1) set C++11 flags for compilation
> 2) set the right include-pathes
> 3) link with the right libraries as per my order
>
> Awesome. But is this a good practice? Are there any pitfalls?

This sounds like a use case for IMPORTED rather than INTERFACE libraries.
INTERFACE libraries can be IMPORTED as well but are not intended for 
pre-build linkable libraries (which you seem to have).

This documents how to create find modules with IMPORTED targets:
https://cmake.org/cmake/help/v3.4/manual/cmake-developer.7.html#find-modules

Nils


More information about the CMake mailing list