Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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/[email protected]/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?

like image 517
amit kumar Avatar asked Jun 17 '09 08:06

amit kumar


People also ask

How do I use Environment Variables in CMake?

Use the syntax $ENV{VAR} to read environment variable VAR . To test whether an environment variable is defined, use the signature if(DEFINED ENV{<name>}) of the if() command. For general information on environment variables, see the Environment Variables section in the cmake-language(7) manual.

How do I set Environment Variables in Visual Studio?

In Visual Studio 2019 right-click your project, choose Properties . In the project properties window, select the Debug tab. Then, under Environment variables change the value of your environment from Development to Production or other environments.

How do I change my path in CMake?

CMake will use whatever path the running CMake executable is in. Furthermore, it may get confused if you switch paths between runs without clearing the cache. So what you have to do is simply instead of running cmake <path_to_src> from the command line, run ~/usr/cmake-path/bin/cmake <path_to_src> .

How do I add a path in Visual Studio?

In the System dialog box, click Advanced system settings. On the Advanced tab of the System Properties dialog box, click Environment Variables. In the System Variables box of the Environment Variables dialog box, scroll to Path and select it.


1 Answers

For setting custom project setting in Visual Studio from CMake you can use a XML file as a template which can be configured from CMake to work as the .user file.
At my work we use this to set custom debug parameters.

Check the directory containing the generated .vcxproj files for the user settings in the .user files. Here is a snippet of an example UserTemplate.vcxproj.user file we use.

    <?xml version="1.0" encoding="Windows-1252"?>       <VisualStudioUserFile         ProjectType="Visual C++"         Version="9.00"         ShowAllFiles="false"         >         <Configurations>             <Configuration                 Name="Debug|@USERFILE_PLATFORM@"                 >                 <DebugSettings                     Command="@USERFILE_COMMAND_DEBUG@"                     WorkingDirectory="@USERFILE_WORKING_DIRECTORY_DEBUG@"                     CommandArguments="@USERFILE_COMMAND_ARGUMENTS_DEBUG@"                     Attach="false"                     DebuggerType="3"                     Remote="1"                     RemoteMachine="@USERFILE_REMOTE_MACHINE_DEBUG@"                                 <!-- More settings removed for snippet -->                 />             </Configuration>                 <!-- Rest of Configurations --> 

Another example of a UserTemplate.vcxproj.user to set the PATH variable, would be:

    <?xml version="1.0" encoding="utf-8"?>     <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">       <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">         <LocalDebuggerEnvironment>PATH=..\Your_path;%PATH%".</LocalDebuggerEnvironment>         <DebuggerFlavor>WindowsLocalDebugger</DebuggerFlavor>       </PropertyGroup>     </Project> 

Setting the UserTemplate.vcxproj.user file next to your CMakeLists.txt file, you can inject any needed variables from CMake into the .vcxproj.user file of your builded project. In CMake you can set the appropiate CMake variables (and add more in the template file if you need them). Next you can do something like this to configure the file.

    # Find user and system name     SET(SYSTEM_NAME $ENV{USERDOMAIN} CACHE STRING SystemName)     SET(USER_NAME $ENV{USERNAME} CACHE STRING UserName)      # Configure the template file     SET(USER_FILE ${_projectName}.vcxproj.${SYSTEM_NAME}.${USER_NAME}.user)     SET(OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${USER_FILE})     CONFIGURE_FILE(UserTemplate.vcxproj.user${USER_FILE} @ONLY) 

If you don't care about the system and the user name, the following configuration would be enough.

    # Configure the template file     SET(USER_FILE ${_projectName}.vcxproj.user)     SET(OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${USER_FILE})     CONFIGURE_FILE(UserTemplate.vcxproj.user ${USER_FILE} @ONLY) 
like image 173
pkit Avatar answered Sep 30 '22 17:09

pkit