Using CMake include_directories command with spaces

I am using CMake to build my project and I have the following line:

include_directories(${LLVM_INCLUDE_DIRS})

      

which after evaluation LLVM_INCLUDE_DIRS

evaluates:

include_directories(C:\Program Files\LLVM\include)

      

The problem is that it counts as two included directories: "C: \ Program" and "Files \ LLVM \ include".

Any idea how I can solve this problem? I tried using quotes, but it didn't work.

EDIT: It turned out that the problem is in the file llvm-3.0\share\llvm\cmake\LLVMConfig.cmake

. I enclosed the following paths with quotes and the problem was resolved:

set(LLVM_INSTALL_PREFIX C:/Program Files/LLVM)
set(LLVM_INCLUDE_DIRS ${LLVM_INSTALL_PREFIX}/include)
set(LLVM_LIBRARY_DIRS ${LLVM_INSTALL_PREFIX}/lib)

      

+3


source to share


2 answers


This is most likely a bug at the point where it is LLVM_INCLUDE_DIRS

asked, not a problem with include_directories

.

To test this, try calling include_directories("C:\\Program Files\\LLVM\\include")

- it should work correctly.

The problem is that it LLVM_INCLUDE_DIRS

was created without using quotes. Try, for example, run this:

set(LLVM_INCLUDE_DIRS C:\\Program Files\\LLVM\\include)
message("${LLVM_INCLUDE_DIRS}")
set(LLVM_INCLUDE_DIRS "C:\\Program Files\\LLVM\\include")
message("${LLVM_INCLUDE_DIRS}")

      



Output:

C:\Program;Files\LLVM\include
C:\Program Files\LLVM\include

      

Notice the comma on the first output line. This is a list of 2 items.

So, the way to fix this is to change the way you create LLVM_INCLUDE_DIRS

.

+3


source


In CMake

  • whitespace - list separator (for example ;

    ),
  • Evaluating variable names basically replaces the variable name with the content and
  • \

    is an escaped character (to get a character, it must also be escaped)

So in your example is the include_directories(C:\\Pogram Files\\LLVM\\include)

same as

include_directories( C:\\Program;Files\\LLVM\\include)

      

that is, a list with two elements. To avoid this,



  • also avoid spaces:

    include_directories( C:\\Program\ Files\\LLVM\\include)

    or

  • surrounds the path with quotes:

    include_directories( "C:\\Program Files\\LLVM\\include")

Obviously, the second option is the best choice since it

  • easier and easier to read and
  • can be used with variable evaluation as in your example (since the result of the evaluation is then surrounded by quotes and thus one item is processed)

    include_directories ("$ {LLVM_INCLUDE_DIRS}")

This also works if LLVM_INCLUDE_DIRS

is a list of multiple directories, because the items in that list will then be explicitly separated by a character ;

, so there is no need for an unquoted space as the implicit separator of the list items.

Side note: When using hardcoded pathnames (for whatever reason) in my CMake files, I usually use forward slashes as directory separators since this works on Windows too, and avoids having to avoid all backslashes.

+3


source







All Articles