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

后端 未结 6 1645
梦毁少年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:27

    You can give any options globally with the new VS_USER_PROPS target property (version >= 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 
    #include 
    
    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" [=[
     
    
      
        PATH=C:\Test
      
      
        PATH=C:\Test
      
    
    ]=])
    
    set_target_properties(
        ${PROJECT_NAME}
        PROPERTIES
            VS_USER_PROPS "${PROJECT_NAME}.Cpp.user.props"
    ) 
    

    References

    • CMake Issue #8884: Set Visual Studio project "custom environment variables" setting with CMake
    • set diagnostics:caret from CMakeLists.txt

提交回复
热议问题