Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find_path doesn't work if environment variable has spaces

Tags:

windows

cmake

I'm trying to make my cmake project automatically compile but I have some difficulties when my path contains spaces.

Here is my command line (windows command prompt)

C:\Code\codetrainerplugins-build>type %CODETRAINER_PATH%\include\common\exportapi.h
#pragma once
... the file is found ...

Here is my CMakeLists.txt file:

CMAKE_MINIMUM_REQUIRED (VERSION 2.6)
PROJECT (CodeTrainerPlugins)

MESSAGE("$ENV{CODETRAINER_PATH}")

FIND_PATH   (CODETRAINER_FRAMEWORK_PATH 
                NAMES include/common/ExportApi.h
                PATHS
                    ENV CODETRAINER_PATH
            )


if (CODETRAINER_FRAMEWORK_PATH)
    MESSAGE(STATUS "CodeTrainer Framework found at: ${CODETRAINER_FRAMEWORK_PATH}")
else()
    MESSAGE(FATAL_ERROR "CodeTrainer Framework not found")
endif()

ADD_SUBDIRECTORY(function)
ADD_SUBDIRECTORY(test)

Here is the output when CODETRAINER_PATH variable contains spaces (see the space in the path):

C:\Code\codetrainerplugins-build>echo %CODETRAINER_PATH%
"C:\Code Trainer"
C:\Code\codetrainerplugins-build>
C:\Code\codetrainerplugins-build>cmake ..\codetrainerplugins
-- Building for: Visual Studio 10
"C:\Code Trainer"
CMake Error at CMakeLists.txt:16 (MESSAGE):
  CodeTrainer Framework not found


-- Configuring incomplete, errors occurred!
See also "C:/Code/codetrainerplugins-build/CMakeFiles/CMakeOutput.log".

C:\Code\codetrainerplugins-build>

But when the path used has no spaces everything works ok (see below):

C:\Code\codetrainerplugins-build>echo %CODETRAINER_PATH%
C:\CodeTrainer

C:\Code\codetrainerplugins-build>cmake ..\codetrainerplugins
C:\CodeTrainer
-- CodeTrainer Framework found at: C:/CodeTrainer
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Code/codetrainerplugins-build

C:\Code\codetrainerplugins-build>

Do you have any solution on how to solve this issue?

I am using cmake 2.8.12 for Windows.

Thanks, Iulian

like image 228
INS Avatar asked Feb 17 '14 20:02

INS


1 Answers

I must admit, I would have expected this to "just work" too, however it looks like it's actually the quotation marks in CODETRAINER_PATH when it has spaces that are the cause of the problem.

Either don't add quotes when defining the environment variable CODETRAINER_PATH, or modify your CMake code something like this:

STRING(REPLACE "\"" "" CODETRAINER_PATH_WITHOUT_QUOTES $ENV{CODETRAINER_PATH})
FIND_PATH(CODETRAINER_FRAMEWORK_PATH 
          NAMES include/common/ExportApi.h
          PATHS ${CODETRAINER_PATH_WITHOUT_QUOTES}
          )
like image 145
Fraser Avatar answered Nov 15 '22 18:11

Fraser