Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying include directories on the cmake command line

Is it possible to specify an include directory when running cmake. For example

cmake . -INCLUDE=/foo/bar 

The header files are in a separate directory from the sources that I would like to compile, and I would like to remedy this without tinkering with the Makefile generated by cmake.

Update

The project does have a CMakeLists.txt. Excerpt:

INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src) INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src/ga) INCLUDE_DIRECTORIES(${EO_SOURCE_DIR}/src/utils) 

Can ${EO_SOURCE_DIR} be set from the command line?

like image 552
Olumide Avatar asked Oct 20 '12 00:10

Olumide


People also ask

How do I include a directory in CMake?

First, you use include_directories() to tell CMake to add the directory as -I to the compilation command line. Second, you list the headers in your add_executable() or add_library() call.

How use CMake command line?

Running CMake from the command line From the command line, cmake can be run as an interactive question and answer session or as a non-interactive program. To run in interactive mode, just pass the option “-i” to cmake. This will cause cmake to ask you to enter a value for each value in the cache file for the project.

What are include directories?

The include directories are added to the INCLUDE_DIRECTORIES directory property for the current CMakeLists file. They are also added to the INCLUDE_DIRECTORIES target property for each target in the current CMakeLists file. The target property values are the ones used by the generators.

How do I change my path in CMake?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .


2 Answers

If the path to your headers is fixed in relation to your sources, then you should be able to avoid having to pass this info via the command line.

Say your project's directory structure is:

/CMakeLists.txt /my_sources/main.cpp /my_sources/foo.cpp /my_includes/foo.hpp 

and in your CMakeLists.txt, you currently have something like:

add_executable(MyExe my_sources/main.cpp my_sources/foo.cpp) 

then to add the /my_includes folder to the the list of include search paths, you only need to add the following:

include_directories(my_includes) 


For further info about include_directories, run

cmake --help-command include_directories 

Answer to update in question:

Yes, using the -D command line option just do

cmake . -DEO_SOURCE_DIR:PATH=<Path to required dir> 

The variable ${EO_SOURCE_DIR} gets cached as a result of this, so you only need this -D argument once (unless the required path changes or you delete your CMakeCache file, etc.)

like image 143
Fraser Avatar answered Sep 28 '22 01:09

Fraser


Proper way to do this is to define a variable in CMakeLists.txt and ask user to set it:

set(YOURLIB_INCLUDE_DIR "" CACHE FILEPATH "Path to yourlib includes")  if(NOT EXISTS ${YOURLIB_INCLUDE_DIR}/header.h)   message(SEND_ERROR "Can't find header.h in ${YOURLIB_INCLUDE_DIR}) endif()  include_directories(${YOURLIB_INCLUDE_DIR}) 

Now you can set it from the command line: cmake -D YOURLIB_INCLUDE_DIR=/path/to/yourlib/include .

like image 43
arrowd Avatar answered Sep 28 '22 00:09

arrowd