Emil Fredrik
Emil Fredrik

Reputation: 117

How to set a CMake path from command line

What is the difference between

CMAKE_PREFIX_PATH=$PWD/../../qt5/qtbase/:$PWD/../../opencv/build/install/ cmake ..

and

cmake -D CMAKE_PREFIX_PATH=$PWD/../../qt5/qtbase/:$PWD/../../opencv/build/install/ ..

For me, the first seems to work fine but not the second! I'm curious to understand why.

As far as I understand it, in the first version, the shell sets the variable before the cmake command is invoked, whereas the second tells cmake to set the variable. Is that correct?

Upvotes: 4

Views: 12209

Answers (2)

revl
revl

Reputation: 161

You correctly identified the difference between these two variables. They have the same name, but they exist separately from each other - one is a process environment variable and another is CMake's internal variable.

Consequently, they're governed by different rules. By convention, paths in environment variables are separated using colons while CMake lists are semicolon-separated.

You need to use semicolons when defining CMAKE_PREFIX_PATH on the command line, like so (the value must also be quoted so that the shell doesn't treat the semicolon as a command separator):

cmake -D CMAKE_PREFIX_PATH="$PWD/../../qt5/qtbase;$PWD/../../opencv/build/install" ..

Upvotes: 0

ComicSansMS
ComicSansMS

Reputation: 54589

The second version is what you will want to use in most cases.

As you have noticed, the first version sets the variable as a shell environment variable, while the second sets a CMake variable.

Note that we are talking about two different variables here, which are both named CMAKE_PREFIX_PATH. Quoting the manual for find_file:

  1. Search paths specified in cmake-specific cache variables. These are intended to be used on the command line with a -DVAR=value. This can be skipped if NO_CMAKE_PATH is passed.

    [...]

    CMAKE_PREFIX_PATH

  2. Search paths specified in cmake-specific environment variables. These are intended to be set in the user’s shell configuration. This can be skipped if NO_CMAKE_ENVIRONMENT_PATH is passed.

    [...]

    CMAKE_PREFIX_PATH

Note also that your first version is non-portable and specific to the shell you are using. For instance, a Windows shell will reject that command altogether.

The reason your second version did not work for you is that you are using the $PWD variable from your shell, which CMake does not understand. In the first version it will get expanded to your current working directory, while the second version will just pass the literal, unresolved string $PWD to CMake.

Upvotes: 6

Related Questions