Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set a CMake path from command line

Tags:

cmake

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?

like image 223
Emil Fredrik Avatar asked Oct 14 '25 16:10

Emil Fredrik


1 Answers

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.

like image 104
ComicSansMS Avatar answered Oct 18 '25 02:10

ComicSansMS



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!