English 中文(简体)
我如何利用一种组合式独立方法在C获得执行方案目录?
原标题:How do I get the executing program s directory in C using a plataform independent method?

我在北草坪开发了我对冲破和双赢32(每座建筑都汇编成册)平台的 app光,因此,准备使用功能是ice。 使用有<代码>gchar* g_get_ Current_dir(ave)功能的校准,并将现有名录退回,但我确实需要的是可执行目录。 在C方案拟订中我没有经验,因此欢迎任何建议。

最佳回答

如同有<代码>/proc的操作系统一样,名录可以<编码>readlink/proc/ Self/exe,即使有<编码>argv[0],也可获得实际可执行文档的全部途径。

但是,如果可执行性从fexecv(许多系统没有数据)开始,如果作为需要你的系统加以实施,则可能不会奏效。 <编码>fexecv 仅见execve。 除此以外,还通过了公开档案记录,而不是档案名称。 在欧林肯下,通过电话execve,介绍“proc/ Self/%i”生成的插图,因此,在方案启动时,档案必须放在档案系统中。

我认为,德国联邦大学/德国大学支持<代码>fexecve。

在档案系统被执行后,可以将可起诉的档案重新命名或与档案系统脱钩,这样它就成为一个无名的档案,一旦其停止开放(在这种情况下,档案通常需要由纸浆商开放)。

问题回答

GetModuleFileName in Windows. argv[0] in linux

<>光> 如果你进行跨层节目,你也应该使用一些横跨平台的图书馆来掩盖这一层(Qt, wxWidgets, ACE, Boost[Don tnow,但我认为它有点]......

我不能谈论窗户。 然而,在UNIX,可以达到一定比例的时间,但并没有保证。 造成这种情况的原因包括要求从诸如炮弹等其他方案中解脱。 在某些情况下,这种方案可能会使可迫害的生活,例如,一种相对的道路,如“././.mybin/exe/myprogram”。 《太平洋行动计划》的变量也使跟踪无法执行。

Let me ask: what are you trying to do, or, more correctly, why do you need to know? IMO you don t need to know. you can check getcwd() and if you are not running in a directory that works, exit. It should not matter where your executable image lives.

这里是一部法典的空白,大部分时间都奏效,并不总是!

要求主人发出呼吁,使用“纳尔吉斯”(0)号,我使用开放式电话,使该守则适合少数人群。

char *
mypath(const char *src)
{
   FILE *cmd=NULL;
   static char path_2_me[PATH_MAX]={0x0};
   char tmp[PATH_MAX]={0x0};
   const char *basename=strrchr(src,  / );

   if(basename==NULL) 
        basename=src;
   else
        basename++;      

   if(memcmp(src, "./", 2)==0)      
     sprintf(path_2_me,"%s/%s", getcwd(tmp, PATH_MAX), basename);             
   else
   {
      sprintf(tmp, "/usr/bin/which %s", basename);
      cmd=popen(tmp, "r");      
      fgets(path_2_me, sizeof(path_2_me), cmd); /* one read only */
      pclose(cmd);
      /* check for what your version of which says on failure */
      if(memcmp(path_2_me, "no ", 3)==0)
         *path_2_me=0x0;
   }  

   return (*path_2_me) ?path_2_me: NULL;
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签