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

后端 未结 6 1655
梦毁少年i
梦毁少年i 2020-12-02 06:27

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 requir

6条回答
  •  忘掉有多难
    2020-12-02 07:10

    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.

        
          
            
                
                    
                    />
                
                    
    

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

        
        
          
            PATH=..\Your_path;%PATH%".
            WindowsLocalDebugger
          
        
    

    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)
    

提交回复
热议问题