[CMake] Making find_package work + compiler flags

Edvard Fagerholm edvard.fagerholm at gmail.com
Thu Nov 14 05:28:58 EST 2019


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/8ec0957b/attachment.html>


More information about the CMake mailing list