Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CMake: how to create include path to generated include files

Tags:

cmake

I have a project with about 600 directories, containing the source for about a dozen libraries and several dozen programs. Some of the programs depend upon a C++ header file that is generated from a text file.

How do I cleanly tell CMake how to include that path to the generated header file into the include path for those source files which require it?

Or alternatively, how can I force the generated file to be installed into a known location before CMake attempts to compile those programs? (This idea is based on the current Makefile system, which generates the header and installs it into the /include directory where all source files can find it.)

Or is there some other alternative?

-- EDIT: added "toy" example --

This example does not work. I need a way to tell prog1 and prog2 where to find home.h.

My tree:

.
|-- prog1/
|   |-- CMakeLists.txt
|   `-- prog1.cpp
|-- prog2/
|   |-- CMakeLists.txt
|   `-- prog2.cpp
`-- share/
    `-- dict/
        |-- CMakeLists.txt
        `-- gen.sh*

In share/dict:

cmake_minimum_required(VERSION 2.8.4)
project(dict)

set(SRC foo.c)
set(HEADERS foo.h home.h)

add_custom_target(home
  ALL
  DEPENDS ${CMAKE_INSTALL_PREFIX}/include/home.h
)

add_custom_command(
  OUTPUT ${CMAKE_INSTALL_PREFIX}/include/home.h
  COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_SOURCE_DIR}/gen.sh gen.sh
  COMMAND gen.sh ${CMAKE_INSTALL_PREFIX}
)

add_library(dict ${SRC})

install(TARGETS dict DESTINATION lib)
install(FILES ${HEADERS} DESTINATION include)

In prog1 (and similarly in prog2):

cmake_minimum_required(VERSION 2.8.4)
project(prog1)
add_executable(prog1 prog1.cpp)
target_link_libraries(prog1 dict)
install(TARGETS prog1 DESTINATION bin)
like image 981
CXJ Avatar asked Nov 29 '14 04:11

CXJ


People also ask

How add include path 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.

Where does CMake look for include files?

cmake is searched first in CMAKE_MODULE_PATH , then in the CMake module directory. There is one exception to this: if the file which calls include() is located itself in the CMake builtin module directory, then first the CMake builtin module directory is searched and CMAKE_MODULE_PATH afterwards.

What is the difference between Include_directories and Target_include_directories?

1, include_directories() is accessible for all the files in the source-tree 2, target_include_directories() is-only accessible for a specific target when compile.

What does CMake_include_directories_before do?

(optional) whether to add the given directories to the front or end of the current list of include paths; default behaviour is defined by CMAKE_INCLUDE_DIRECTORIES_BEFORE (optional) tells the compiler to tread the given directories as system include dirs, which might trigger special handling by the compiler

How do I add headers to a project using 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. As an example, if your project's sources are in src, and you need headers from include, you could do it like this:

How do I create a CMake build script for my project?

To create a plain text file that you can use as your CMake build script, proceed as follows: Open the Project pane from the left side of the IDE and select the Project view from the drop-down menu. Right-click on the root directory of your-module and select New > File. Note: You can create the build script in any location you want.

How do I create a cmakelists file?

Right-click on the root directory of your-module and select New > File . Note: You can create the build script in any location you want. However, when configuring the build script, paths to your native source files and libraries are relative to the location of the build script. Enter "CMakeLists.txt" as the filename and click OK .


Video Answer


1 Answers

After

add_library(dict ${SRC})

add

target_include_directories(dict 
    # The location of the headers before installation 
    $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/include>
    # The location of the headers after installation
    $<INSTALL_INTERFACE:include>
)

and read the link I already provided in the comment.

like image 94
steveire Avatar answered Sep 21 '22 18:09

steveire