[CMake] Package found - passing _INCLUDE_DIRS to include_directories() and _LIBRARIES to target_link_libraries()

Walter Gray chrysalisx at gmail.com
Fri Jul 18 23:19:57 EDT 2014


Unless I'm mistaken, it says that the *CONVENTION* is to call 
include_directories(${<PACKAGE-NAME>_INCLUDE_DIRS}) manually, not that 
it is done automatically.  To get that kind of automatic include 
directory adding you need to have an import or interface target with 
INTERFACE_INCLUDE_DIRECTORIES defined by the find module, then link with 
that.  Take a look at topics related to Interface Libraries [1] and 
writing modern find modules [2]

[1] 
http://www.cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html#interface-libraries
[2] 
http://www.cmake.org/cmake/help/v3.0/manual/cmake-developer.7.html#find-modules

On 7/18/2014 7:31 PM, Michael Darling wrote:
> http://www.cmake.org/Wiki/CMake:How_To_Find_Libraries#How_package_finding_works 
>
>
> seems to indicate if <PACKAGE-NAME>_FOUND is found, the 
> <PACKAGE-NAME>_INCLUDE_DIRS is passed to the include_directories() 
> command, and <PACKAGE-NAME>_LIBRARIES is passed to target_link_libraries()
>
> Why is the reduced-case code below calling g++ without including 
> "-I~/codeTestPackages/lib", causing a "app/app.cpp:1:17: fatal error: 
> lib.h: No such file or directory" ?
>
> I'm on CMake v3.0.0.  Also tried CMake v3.0.20140718-g36a81 (git source.)
>
> All the source is below, and attached as a .tar.gz.
>
>
> *### CMakeLists.txt ###*
>
> cmake_minimum_required(VERSION 3.0)
> project(codeTestPackages)
> set(CMAKE_VERBOSE_MAKEFILE on)
> set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} ~/codeTestPackages/findModules)
> add_subdirectory(lib)
> add_subdirectory(app)
>
> *### findModules/Findlib.cmake ###*
>
> IF(NOT lib_FOUND)
>    set(lib_FOUND "yes")
>    message("lib_FOUND is ${lib_FOUND}")
>    set(lib_INCLUDE_DIRS ~/codeTestPackages/lib)
>    set(lib_LIBRARIES lib)
> ENDIF(NOT lib_FOUND)
>
> *### lib/CMakeListst.txt ###*
>
> include_directories(~/codeTestPackages/lib)
> add_library(lib lib.cpp)
>
> *### lib/lib.h ###*
>
> #ifndef __LIB__
> #define __LIB__
> namespace LIB {
> unsigned long libFunc(unsigned long inValue);
> }
> #endif
>
> *### lib/lib.cpp ###*
>
> #include <lib.h>
> namespace LIB {
> unsigned long libFunc(unsigned long inValue) {
>    return inValue+1;
> }
> }
>
> *### app/CMakeLists.txt ###*
>
> find_package(lib REQUIRED)
> add_executable(app app.cpp)
>
> *### app/app.cpp ###*
>
> #include <lib.h>
> using namespace LIB;
>
> int main() {
>    unsigned long x = 1;
>    unsigned long y = libFunc(x);
> }
>
>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/cmake/attachments/20140718/06621754/attachment.html>


More information about the CMake mailing list