简体   繁体   English

如何使用system()函数在c ++程序中设置路径

[英]How to set path inside of c++ program using system() function

I need to write c++ program which sets path using system() function: 我需要编写使用system()函数设置路径的c ++程序:

system("set PATH=%PATH%;C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64");
system("nvcc -x cu -o cudapair cudapair.c");

But it doesnt work. 但它不起作用。 It throws error, because path wasn't set. 由于未设置路径,因此会引发错误。 What's the problem? 有什么问题?

I need to write c++ program which sets path using system() function 我需要编写使用system()函数设置路径的C ++程序

I'm assuming what you actually need to do is write a C++ program that sets the PATH for the environment in which it will then execute 我假设您实际需要做的是编写一个C ++程序,该程序为将在其中执行的环境设置PATH

nvcc -x cu -o cudapair cudapair.c

You think you need to make that environment setting with the system function, but in fact you don't. 您认为您需要使用system功能进行该环境设置,但实际上您不需要这样做。

You should understand that a process cannot change its own environment. 您应该了解,流程无法更改其自身的环境。 A process inherits its environment from its parent process, and it can change the environment that is inherited by its child processes. 进程从其父进程继承其环境,并且可以更改其进程继承的环境。

That's why your posted attempt does not work. 这就是为什么您发布的尝试不起作用的原因。

system("set PATH=%PATH%;C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64");

executes a child process of your program. 执行程序的子进程。 That child process gets the same environment settings as your program, and can't change them. 该子进程将获得与您的程序相同的环境设置,并且无法更改它们。 What does that child process do? 该子进程做什么? It invokes the Windows shell to run the commandline: 它调用Windows Shell运行命令行:

set PATH=%PATH%;C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64");

This would change the environment settings of any more child processes that were started by this commandline . 更改此命令行启动的更多子进程的环境设置。 But there aren't any. 但是没有。 The commandline makes an environment setting that no process uses. 命令行进行环境设置,任何进程都不会使用。 Your system call returns. 您的system调用返回。 That environment setting no longer exists anywhere. 该环境设置不再存在于任何地方。 Nothing has changed. 什么也没有变。

You then call: 然后,您致电:

system("nvcc -x cu -o cudapair cudapair.c");

which starts a second child process, again with the same environment settings that your program started with. 这将启动第二个子进程,并再次使用与程序开始时相同的环境设置。

What you should do 你应该怎么做

  1. Get the value of PATH from the environment that your program inherits. 从程序继承的环境中获取PATH的值。
  2. Using that value, create the new value of PATH that you want to pass to your child process. 使用该值,创建要传递给子进程的PATH的新值。
  3. Put that new value of PATH into the environment your child process will inherit. PATH新值放入子进程将继承的环境中。
  4. Run your child process. 运行您的子进程。

You use system only to do 4 . 您仅使用system4

To do 1 , use the Microsoft C library function getenv_s (It is a secure variant of the Standard C++ std::getenv ) 若要做1 ,请使用Microsoft C库函数getenv_s (这是标准C ++ std::getenv的安全变体)

To do 3 , use the Microsoft C library function _putenv_s (Note the leading underscore.) 若要执行3 ,请使用Microsoft C库函数_putenv_s (请注意开头的下划线。)

Here is an illustrative program for Visual C++: 这是Visual C ++的说明性程序:

#include <iostream>
#include <string>
#include <cstdlib>

const std::size_t ENV_BUF_SIZE = 1024; // Enough for your PATH?

int main()
{
    char buf[ENV_BUF_SIZE];
    std::size_t bufsize = ENV_BUF_SIZE;
    int e = getenv_s(&bufsize,buf,bufsize,"PATH");  
    if (e) {
        std::cerr << "`getenv_s` failed, returned " << e << '\n';
        exit(EXIT_FAILURE);
    }
    std::string env_path = buf;
    std::cout << "In main process, `PATH`=" << env_path << std::endl;
    env_path += ";C:\\Program Files (x86)\\Microsoft Visual Studio 12.0\\VC\\bin\\amd64";
    e = _putenv_s("PATH",env_path.c_str());
    if (e) {
        std::cerr << "`_putenv_s` failed, returned " << e << std::endl;
        exit(EXIT_FAILURE);
    }
    std::cout << std::endl;
    e = std::system("echo \"In child process `PATH`=%PATH%\"");
    if (e) {
        std::cerr << "`std::system` failed, returned " << e << std::endl;
        exit(EXIT_FAILURE);
    }
    return 0;
}

See it live 现场观看

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM