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

时间:2022-04-02 11:20:13

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项目文件。我想在设置PATH环境变量后运行测试可执行文件,以便它能够加载所需的dll。我根据http://www.mail-archive.com/cmake@cmake.org/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_MAKEFILE does not help. How would I go about debugging it myself?

此外,我找不到调试我的CMake脚本的简单方法,例如查看它赋予PATH变量的值。使用CMAKE_VERBOSE_MAKEFILE设置CMake详细没有帮助。我将如何自己调试?

5 个解决方案

#1


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.

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

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.

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

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

这样,您可以将CMake中的任何所需变量注入.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}.vcproj.${SYSTEM_NAME}.${USER_NAME}.user)
SET(OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${USER_FILE})
CONFIGURE_FILE(UserTemplate.user ${USER_FILE} @ONLY)

#2


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_properties explicitly first.

如果我试图让它工作,我会先使用set_tests_properties。

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

Then make it more general.

然后使它更通用。

#3


Here is related CMake feature request report:

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

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按照Set Visual Studio项目“自定义环境变量”设置解决 - 感谢Florian的评论如下。

#4


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

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

Here is a working example:

这是一个工作示例:

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

#5


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://*.com/a/40531167/9253113

只是想指出一个非常有用的补充,允许你设置多个环境变量而不是只有一个(例如,只有PATH)在这个链接中给出https://*.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)

符号“ ”告诉xml解析器引入LF字符。因此,如果用LF字符分隔,多个变量定义是可能的(CR字符也起作用,但不是CRLF的组合)

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

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

#1


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.

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

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.

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

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

这样,您可以将CMake中的任何所需变量注入.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}.vcproj.${SYSTEM_NAME}.${USER_NAME}.user)
SET(OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR}/${USER_FILE})
CONFIGURE_FILE(UserTemplate.user ${USER_FILE} @ONLY)

#2


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_properties explicitly first.

如果我试图让它工作,我会先使用set_tests_properties。

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

Then make it more general.

然后使它更通用。

#3


Here is related CMake feature request report:

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

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按照Set Visual Studio项目“自定义环境变量”设置解决 - 感谢Florian的评论如下。

#4


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

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

Here is a working example:

这是一个工作示例:

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

#5


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://*.com/a/40531167/9253113

只是想指出一个非常有用的补充,允许你设置多个环境变量而不是只有一个(例如,只有PATH)在这个链接中给出https://*.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)

符号“ ”告诉xml解析器引入LF字符。因此,如果用LF字符分隔,多个变量定义是可能的(CR字符也起作用,但不是CRLF的组合)

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

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