1

I have a projects that consists of modules that are built separately. Some modules use make only, others use cmake.

The top project has a makefile, which is used to trigger the build of the submodules. The makefile at the top defines make variables for pathes to libs, includes, and binaries. The submodules then use these variables.

My problem is that I cannot pass all these make variables to the projects that use cmake.

For the include directory, this work-around in CMakeLists.txt works:

INCLUDE_DIRECTORIES("/$(INCLUDE_DIR)")

The string is passed as is all the way to the generated makefile, and is then expanded by make upon building. The leading slash is needed. Without it, the content of INCLUDE_DIR gets prefixed by the cmake project root (it is then considered a relative path).

This solution does not work for linking:

LINK_DIRECTORIES("/$(LIB_DIR)")

The link command that I see upon building has the literal string -L/$(LIB_DIR). Removing the leading slash gives a warning about relative paths, and the link command has still the unexpanded $LIB_DIR.

  • How do I make sure that the expansion works for the make variable LIB_DIR passed though cmake to the generated makefile? Since it is make that runs the linker (I think?) I don't see why it wouldn't expand.
  • Is there another, less ugly solution, solution for fixing it for INCLUDE_DIR as well?

A backup solution is to let the top makefile define CMAKE variables. It should work fine, and is probably what I'll do if I can't make it work otherwise. But it would be nice to just define these variables once and make everything else take them into account.

3
  • 1
    I would make these CMake variables using $ENV{VAR} Commented Feb 2, 2015 at 15:18
  • 1
    I mean set (INCLUDE_DIR $ENV{INCLUDE_DIR}) Commented Feb 2, 2015 at 15:20
  • This worked! Make it an answer and you'll get the credit. Thanks! Commented Feb 4, 2015 at 11:58

1 Answer 1

2

One option is to use an environment variable to set a CMake variable. For example:

set (INCLUDE_DIR $ENV{INCLUDE_DIR})

then use the CMake variable like other CMake variables:

INCLUDE_DIRECTORIES(${INCLUDE_DIR})

If you want the variable to show up in cmake-gui or ccmake you can put it in the cache using the following:

set(INCLUDE_DIR $ENV{INCLUDE_DIR} CACHE PATH "An environment variable containing a path" FORCE)

for my projects I have wrapped the concept in a cmake function:

function( define_from_environment VariableName PackageName)
  if (NOT DEFINED ${VariableName})
    message( STATUS "${VariableName}=$ENV{${VariableName}}" )
    if (NOT "$ENV{${VariableName}}" STREQUAL "")
      set(${VariableName} $ENV{${VariableName}} CACHE PATH "Set the path variable ${VariableName} for ${PackageName}" FORCE)
    endif (NOT "$ENV{${VariableName}}" STREQUAL "")
  endif(NOT DEFINED ${VariableName})
endfunction( define_from_environment)

An example usage of this CMake function is

define_from_environment(GDCM_DIR GDCM)

which sets the CMake variable GDCM_DIR to a CMake cache variable if the environment variable GDCM_DIR is set.

Sign up to request clarification or add additional context in comments.

1 Comment

For now I have gone the simplest way: INCLUDE_DIRECTORIES($ENV{INCLUDE_DIR}).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.