How to Set Path Environment Variable using CMake and Visual Studio to Run Test

Viewed 41944

I am using CMake to generate Visual Studio project files. I want to run the test executable after setting the PATH environment variable so that it is able to load the required dll. I tried as per the discussion at http://www.mail-archive.com/cmake@cmake.org/msg21493.html but it does not work.

Have you used CMake with Visual Studio for this purpose? Please share your experiences.

Also, I find no easy way to debug my CMake script, for example to see what value it assigns to the PATH variable. Setting CMake verbose with CMAKE_VERBOSE_MAKEFILE does not help. How would I go about debugging it myself?

6 Answers

Just spotted this question now. To debug cmake files I use

MESSAGE( STATUS "static text ${variable}" )

I have never had to set the path get my tests to run. Are you using CTest? It looks like the link you are following is used with ctest.

If I was trying to get this to work I would use set_tests_properties explicitly first.

set_tests_properties(SomeTest PROPERTIES ENVIRONMENT "PATH=c:\somedir;c:\otherdir")

Then make it more general.

Cmake has a VS_DEBUGGER_ENVIRONMENT property which can be used to set the custom PATH

https://cmake.org/cmake/help/v3.13/prop_tgt/VS_DEBUGGER_ENVIRONMENT.html

set(MY_PATH "PATH=%PATH%" ${MY_CUSTOM_PATH})
set_target_properties(MyTarget PROPERTIES VS_DEBUGGER_ENVIRONMENT "{MY_PATH}")

Some other useful properties are VS_DEBUGGER_COMMAND_ARGUMENTS, VS_DEBUGGER_WORKING_DIRECTORY.

You can give any options globally with the new VS_USER_PROPS target property (version >= 3.8).

Here is a working example:

CMakeLists.txt

cmake_minimum_required(VERSION 3.0)

project(SetEnvPathTest)

file(WRITE main.cpp [=[
// http://en.cppreference.com/w/cpp/utility/program/getenv
#include <iostream>
#include <cstdlib>

int main()
{
    if(const char* env_p = std::getenv("PATH"))
        std::cout << "Your PATH is: " << env_p << '\n';
}
]=])
add_executable(${PROJECT_NAME} main.cpp)

file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.Cpp.user.props" [=[
<?xml version="1.0" encoding="utf-8"?> 
<Project DefaultTargets="Build" ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
    <LocalDebuggerEnvironment>PATH=C:\Test</LocalDebuggerEnvironment>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
    <LocalDebuggerEnvironment>PATH=C:\Test</LocalDebuggerEnvironment>
  </PropertyGroup>
</Project>
]=])

set_target_properties(
    ${PROJECT_NAME}
    PROPERTIES
        VS_USER_PROPS "${PROJECT_NAME}.Cpp.user.props"
) 

References

Just wanted to point out that a very useful addition that allows you to set up multiple environment variables as opposed to only one (e.g., only PATH) is given in this link https://stackoverflow.com/a/40531167/9253113

For example, if in addition to setting PATH you wanted to set another variable OTHERVAR one would have to modify the line

<LocalDebuggerEnvironment>PATH=C:\Test</LocalDebuggerEnvironment>

to

<LocalDebuggerEnvironment>PATH=C:\Test &#xA;OTHERVAR="value of OTHERVAR"</LocalDebuggerEnvironment>

Where the symbol "&#xA;" tells the xml parser to introduce the LF character. So multiple variable definitions are possible if separated by the LF character (also the CR character works but NOT the combination CRLF)

Also notice that there CANNOT be any space between &#xA; and the next variable.

Related