[CMake] Making find_package work + compiler flags

Guy Mac guymac at gmail.com
Thu Nov 14 13:35:42 EST 2019


Hi, the simplest way AFAIK would be like

install(
   TARGETS foo EXPORT Foo-config
   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
)

install(EXPORT Foo-config DESTINATION ${CMAKE_INSTALL_DATADIR}/Foo/cmake)

or

install(EXPORT Foo-config NAMESPACE foo: DESTINATION 
${CMAKE_INSTALL_DATADIR}/Foo/cmake)

You could also do

set(CMAKE_EXPORT_PACKAGE_REGISTRY ON)
export(TARGETS foo NAMESPACE foo:: FILE "Foo-config.cmake")
export(PACKAGE Foo)

which puts the build directory in ~/.cmake (Unix|Linux) or a registry 
key (Windows) so you don't have to do an install to build against the 
package, but you can easily run into problems so it is not recommended.

Note most docs will have the CamelCase syntax for config files (e.g. 
FooConfig vs foo-config) but in my experience either form works.

On 11/14/2019 3:28 AM, Edvard Fagerholm wrote:
> Hi,
>
> I'm trying to get started with CMake in a personal project having used 
> other build system previously. The main reason to try out CMake is 
> that CUDA support sucks in Bazel. I see a lot of conflicting 
> information in tutorials and blog posts and I'm trying to avoid going 
> down the rabbit hole reading docs, so I'm hoping someone can point me 
> to the simplest way of doing what I want.
>
> My template project has the following layout
>
> foo/CMakeLists.txt
>    /src/foo.cc
>        /foo.h
>        /internal/log.h
>
> The header foo.h should after installation be usable as "#include 
> <foo/foo.h>" by users and the header in the internal subfolder should 
> not be installed. It should also be possible for a user to just do:
>
> find_package(foo REQUIRED)
> target_link_libraries(bar PRIVATE foo)
>
> Some other minor points:
>
> 1. I want to use apply the "-Wall -Werror" flags on my own code (not 
> on deps or force it on client code).
>
> 2. My own code should be compiled as C++17.
>
> I'm attaching my current CMakeLists.txt below. What's currently 
> missing is that the library won't be found using "find_package", since 
> I haven't figured out the simplest way to accomplish this. I'm also 
> not sure whether I'm correctly and idiomatically.
>
> Anyone care to point on non-idiomatic things in my file below as well 
> as what the simplest way is to make find_package work? My 
> understanding is that there are some packages that can automatically 
> generate the required files.
>
> ==== start ====
> cmake_minimum_required(VERSION 3.15)
> project(foo
>   VERSION 0.1
>   DESCRIPTION "Foo project"
>   LANGUAGES CXX
> )
> include(GNUInstallDirs)
> add_library(foo SHARED src/foo.cc)
> target_compile_options(foo PRIVATE -Wall)
> target_compile_options(foo PRIVATE -Werror)
> set_target_properties(foo PROPERTIES CXX_STANDARD 17)
> target_compile_features(foo PUBLIC cxx_std_17)
>
> target_include_directories(foo
>   PUBLIC
>     $<INSTALL_INTERFACE:include>
>     $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>
>   PRIVATE
>     ${CMAKE_CURRENT_SOURCE_DIR}/src
> )
>
> install(
>   TARGETS foo
>   ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
>   LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
> )
> install(
>   FILES src/foo.h
>   DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/foo
> )
> ==== end ===
>
> Best,
> Edvard
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://cmake.org/pipermail/cmake/attachments/20191114/c06db303/attachment.html>


More information about the CMake mailing list