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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1005901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 10:39:29  来源:igfitidea点击:

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

visual-studiodllbuildcmake

提问by amit

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.htmlbut it does not work.

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

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

您是否为此目的将 CMake 与 Visual Studio 一起使用?请分享您的经验。

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_MAKEFILEdoes not help. How would I go about debugging it myself?

此外,我发现没有简单的方法来调试我的 CMake 脚本,例如查看它分配给 PATH 变量的值。将 CMake 设置为 verboseCMAKE_VERBOSE_MAKEFILE无济于事。我将如何自己调试它?

采纳答案by pkit

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 .userfile.
At my work we use this to set custom debug parameters.

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

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

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

    <?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.userto set the PATH variable, would be:

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

    <?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.userfile next to your CMakeLists.txtfile, you can inject any needed variables from CMake into the .vcxproj.userfile 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.

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

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

回答by iain

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

现在才发现这个问题。调试我使用的 cmake 文件

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.

我从来没有设置路径让我的测试运行。您在使用 CTest 吗?您所关注的链接似乎与 ctest 一起使用。

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

如果我试图让它发挥作用,我会set_tests_properties首先明确使用。

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

Then make it more general.

然后让它更通用。

回答by mloskot

Here is related CMake feature request report:

这是相关的 CMake 功能请求报告:

http://www.kwwidgets.org/Bug/view.php?id=8884

http://www.kwwidgets.org/Bug/view.php?id=8884

UPDATE: Solved as per Set Visual Studio project "custom environment variables" setting with CMake- thanks to Florian for the comment below.

更新:根据使用 CMake 设置 Visual Studio 项目“自定义环境变量”设置解决- 感谢 Florian 在下面的评论。

回答by Florian

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

您可以使用新的VS_USER_PROPS目标属性(版本 >= 3.8)全局提供任何选项。

Here is a working example:

这是一个工作示例:

CMakeLists.txt

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

参考

回答by Vishwesh

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

Cmake 有一个 VS_DEBUGGER_ENVIRONMENT 属性,可用于设置自定义 PATH

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

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.

其他一些有用的属性是 VS_DEBUGGER_COMMAND_ARGUMENTS、VS_DEBUGGER_WORKING_DIRECTORY。

回答by Francisco Martinez

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

只是想指出,此链接中提供了一个非常有用的附加功能,允许您设置多个环境变量,而不是只有一个(例如,只有 PATH)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

例如,如果除了设置 PATH 之外,您还想设置另一个变量 OTHERVAR,则必须修改该行

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

其中符号“ &#xA;”告诉xml解析器引入LF字符。因此,如果由 LF 字符分隔,则可以有多个变量定义(CR 字符也有效,但不是组合 CRLF)

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

另请注意,&#xA;和下一个变量之间不能有任何空格。