Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/macos/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c++;bash添加到路径_C++_Macos_Bash_Path - Fatal编程技术网

C++ c++;bash添加到路径

C++ c++;bash添加到路径,c++,macos,bash,path,C++,Macos,Bash,Path,我正在尝试制作一个可执行文件,将export PATH=“$PATH:/my/current/directory”附加到我的~/.bash_配置文件(我在OSX 10.9.2上编译w/g++)中。现在我只想让当前工作目录到达当前shell(我想我是用setenv()将它放到子shell中,但我不知道这是否有帮助)我可以从那里接受 好的,源代码: #include <iostream> #include <string> using namespace std; int m

我正在尝试制作一个可执行文件,将export PATH=“$PATH:/my/current/directory”附加到我的~/.bash_配置文件(我在OSX 10.9.2上编译w/g++)中。现在我只想让当前工作目录到达当前shell(我想我是用setenv()将它放到子shell中,但我不知道这是否有帮助)我可以从那里接受

好的,源代码:

#include <iostream>
#include <string>
using namespace std;
int main(){
  /*Add current directory to path (locally)*/
  string CWD = getenv("PWD");
  string endquote = "\"";
  string mystring = "PATH=\"$PATH:";
  mystring += CWD;
  mystring += endquote;
  // JUST TRYING TO GET THE PATH TO UPDATE IN SHELL
  // WILL EVENTUALLY UPDATE THIS TO GO INTO .bash_profile
  system(mystring);
  system("echo $PATH");
  return 0;
}
#包括
#包括
使用名称空间std;
int main(){
/*将当前目录添加到路径(本地)*/
字符串CWD=getenv(“PWD”);
字符串endquote=“\”;
字符串mystring=“PATH=\”$PATH:;
mystring+=CWD;
mystring+=endquote;
//只是想在SHELL中获取更新的路径
//最终将更新此文件以进入.bash_配置文件
系统(mystring);
系统(“echo$PATH”);
返回0;
}
和错误:

setup.cpp:11:3: error: no matching function for call to 'system'
  system(mystring);
  ^~~~~~
/usr/include/stdlib.h:177:6: note: candidate function not viable: no known conversion from 'string' (aka 'basic_string<char,
      char_traits<char>, allocator<char> >') to 'const char *' for 1st argument
int      system(const char *) __DARWIN_ALIAS_C(system);
         ^
1 error generated.
setup.cpp:11:3:错误:调用“系统”时没有匹配的函数
系统(mystring);
^~~~~~
/usr/include/stdlib.h:177:6:注意:候选函数不可行:没有已知的第一个参数从“string”(又名“basic_string”)到“const char*”的转换
int系统(常量字符*)\uuuuu DARWIN\uAlias\uC(系统);
^
生成1个错误。

这里的方法是使用构造函数(将const char*更改为string)?我对他们了解不多,但见鬼,我已经花了好几个小时在这上面了,所以我还是多花点时间好吗?

你需要打电话给它

 system(mystring.c_str());
system()
函数没有关于
std::string
的概念,它需要一个
const char*
参数,可以使用
c_str()
方法从
std::string
中获取该参数

还要注意应用的
system()
命令不会将路径导出到从它开始的(子)shell之外的任何位置,也不会导出到本地配置文件


要真正实现您的目标,您需要打开并修改
~/.bashrc
文件(您只需在此处附加另一个
导出路径=$PATH:
行)!(请参阅更多信息)

< P>我认为您应该少依赖环境变量和外部程序,更多地依赖于编程和C++库和POSIX API的编程。在这种情况下,您实际上不需要太多:

  • getuid
    查找当前用户
  • getpwuid
    查找当前用户的主目录
  • getcwd
    获取当前工作目录
  • 相当标准的文件输出流
此六行程序将
export PATH=“$PATH:current\u working\u PATH”
附加到
~/.bash\u profile

#include <fstream>
#include <string>
#include <pwd.h>
#include <unistd.h>
#include <sys/param.h>

using namespace std;

int main()
{
    char path[MAXPATHLEN];
    struct passwd* user_info = getpwuid(getuid());
    string user_directory = user_info->pw_dir;
    string current_directory = getcwd(path, sizeof path);

    ofstream bash_profile(user_directory + "/.bash_profile", ios_base::app);
    bash_profile << "export PATH=\"$PATH:" << current_directory << '"' << endl;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int main()
{
字符路径[MAXPATHLEN];
结构passwd*user_info=getpwuid(getuid());
字符串user\u directory=user\u info->pw\u dir;
字符串current_directory=getcwd(path,sizeof path);
流bash_配置文件(用户目录+“/.bash_配置文件”,ios_base::app);

bash_profile系统(mystring.c_str())怎么样
?请注意,子进程不能修改其父进程的环境。您可以修改bash启动文件,但实际上不能修改环境。@Ernest Friedman Hill:这很好。请注意,虽然这将编译并运行,但不会达到您的预期效果。命令将在由它们对启动可执行文件的shell没有任何影响。是的,我就是这么想的!谢谢大家。还有c_str()消除了编译错误,在这一点上,除了功能之外,这是我心中的一根刺……谢谢,你是对的,我完全错了。我昨天刚开始胡闹一些bash编程,所以我想这就是我的想法。