如何使用 CMake 和 Visual Studio 设置路径环境变量来运行测试 [英] How to Set Path Environment Variable using CMake and Visual Studio to Run Test

查看:24
本文介绍了如何使用 CMake 和 Visual Studio 设置路径环境变量来运行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 CMake 生成 Visual Studio 项目文件.我想在设置 PATH 环境变量后运行测试可执行文件,以便它能够加载所需的 dll.我按照 http://www.mail-archive 上的讨论进行了尝试.com/cmake@cmake.org/msg21493.html 但它不起作用.

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.

您是否为此目的将 CMake 与 Visual Studio 结合使用?请分享您的经验.

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

此外,我发现没有简单的方法来调试我的 CMake 脚本,例如查看它分配给 PATH 变量的值.使用 CMAKE_VERBOSE_MAKEFILE 设置 CMake 详细没有帮助.我将如何自己调试它?

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?

推荐答案

要从 CMake 在 Visual Studio 中设置自定义项目设置,您可以使用 XML 文件作为模板,该模板可以从 CMake 配置为用作 .用户文件.
在我的工作中,我们使用它来设置自定义调试参数.

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.

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

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 -->

UserTemplate.vcxproj.user 设置 PATH 变量的另一个示例是:

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>

CMakeLists.txt 文件旁边设置 UserTemplate.vcxproj.user 文件,您可以将任何需要的变量从 CMake 注入 .vcxproj.user 构建项目的文件.在 CMake 中,您可以设置适当的 CMake 变量(如果需要,还可以在模板文件中添加更多变量).接下来你可以做这样的事情来配置文件.

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)

这篇关于如何使用 CMake 和 Visual Studio 设置路径环境变量来运行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆