且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何设置路径环境变量使用CMake和Visual Studio运行测试

更新时间:2022-10-20 19:12:35

要在Visual Studio中从CMake设置自定义项目设置,您可以使用XML文件作为模板,可以从CMake作为 .user 文件工作。

在我的工作中,我们使用它来设置自定义调试参数。



检查包含生成的 .vcproj 文件的目录中 .user 文件中的用户设置。以下是我们使用的示例 UserTemplate.vcproj 文件的片段。

 <?xml version =1.0encoding =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 @
<! - 更多设置已针对代码段移除 - >
/>
< / Configuration>
<! - 其余配置 - >这样,你可以从CMake注入任何所需的变量到 .user $ p 文件。
在CMake中,您可以设置适当的CMake变量(如果需要,可在模板文件中添加更多)。
 #查找用户和系统名称
SET(SYSTEM_NAME $ ENV {USERDOMAIN} CACHE STRING SystemName)
SET(USER_NAME $ ENV {USERNAME} CACHE STRING UserName)

#配置模板文件
SET(USER_FILE $ {_ projectName} .vcproj 。$ {SYSTEM_NAME}。$ {USER_NAME} .user)
SET(OUTPUT_PATH $ {CMAKE_CURRENT_BINARY_DIR} / $ {USER_FILE})
CONFIGURE_FILE(UserTemplate.user $ {USER_FILE} @ONLY)


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?

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 .vcproj files for the user settings in the .user files. Here is a snippet of an example UserTemplate.vcproj 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 -->

This way you can inject any needed variables from CMake into the .user file. 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}.vcproj.${SYSTEM_NAME}.${USER_NAME}.user)
SET(OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${USER_FILE})
CONFIGURE_FILE(UserTemplate.user ${USER_FILE} @ONLY)