Search code examples
cmakedynamic-linkinginclude-path

Compiling a CMake project against libraries in a non-standard location


I have two projects using CMake.

The first is a shared library. It compiles and installs fine. Currently, it is still necessary to build 'debug' releases of it. So presently it is installed under ~/localdebug. That folder looks like the root of a filesystem with a 'include' and 'lib' directory. The same concept as '/usr/local'.

The second is a program. It needs to compile and link against my library in ~/localdebug. The CMakeLists.txt file for it looks like this:

cmake_minimum_required(VERSION 2.6)
set(CMAKE_C_FLAGS "-std=gnu99")

#add_definitions(-pg)
find_library(SANDGROUSE_LIB NAMES sandgrouse)
add_library(http_parser http_parser.c)
add_executable(rsva11001adapter main.c rsva11001.c)

target_link_libraries(rsva11001adapter http_parser ${SANDGROUSE_LIB})

I run the following to set up the make files:

cmake --debug-output -DCMAKE_BUILD_TYPE=Debug -DCMAKE_PREFIX_PATH="/home/ericu/localdebug" ..

Based on the CMake wiki, setting DCMAKE_PREFIX_PATH does exactly what I want.

CMAKE_PREFIX_PATH

(since CMake 2.6.0) This is used when searching for include files, binaries, or libraries using either the FIND_PACKAGE(), FIND_PATH(), FIND_PROGRAM(), or FIND_LIBRARY() commands. For each path in the CMAKE_PREFIX_PATH list, CMake will check "PATH/include" and "PATH" when FIND_PATH() is called, "PATH/bin" and "PATH" when FIND_PROGRAM() is called, and "PATH/lib" and "PATH" when FIND_LIBRARY() is called. See the documentation for FIND_PACKAGE(), FIND_LIBRARY(), FIND_PATH(), and FIND_PROGRAM() for more details.

However, when I do a 'make VERBOSE=1' this is what I get:

cd /home/ericu/rsva11001adapter/build/src && /usr/bin/gcc -std=gnu99 -g -o CMakeFiles/rsva11001adapter.dir/main.c.o -c /home/ericu/rsva11001adapter/src/main.c
/home/ericu/rsva11001adapter/src/main.c:19:31: fatal error: sandgrouse/server.h: No such file or directory
compilation terminated.

So, it does not seem that CMake is finding things in CMAKE_PREFIX_PATH. It obviously is not adding -I variables to the compiler invocations either.

An inspection of CMakeCache.txt makes it seem as though it has no idea what the variable is:

// No help, variable specified on the command line.
CMAKE_PREFIX_PATH:UNINITIALIZED=/home/ericu/localdebug

I've been working on this for over an hour. I'm nearly at the point of giving up using CMake if it is this difficult to use a non-standard library with it.


Solution

  • You should instruct CMake to add -I flags when compiling your library:

    find_path(SANDGROUSE_INCLUDE_DIR sandgrouse/server.h)
    include_directories(${SANDGROUSE_INCLUDE_DIR}
    

    Place these lines before add_library() invocation.