查找Mac上的执行路径

C++ Find execution path on Mac

本文关键字:执行 路径 Mac 查找      更新时间:2023-10-16

假设我的可执行文件位于Mac OSX的/Users/test_user/app,我从/Users/test_user/Desktop/run_app运行它:

Desktop run_app$ /Users/test_user/app/exec 

在我的c++代码中,我如何找到可执行文件位置的路径(在这种情况下是/users/test_user/app)?我需要在我的代码中的这个路径中引用一些其他文件,并且不想在代码中放置绝对路径,因为一些用户可能会将文件夹放在不同的位置。

man 3 dyld说:

_NSGetExecutablePath()将主可执行文件的路径复制到缓冲缓冲区。的大小缓冲区。如果路径复制成功,此函数返回0。如果缓冲区不够大,它返回-1,并设置* bufsize到所需的尺寸。注意_NSGetExecutablePath()将返回"a"可执行文件的"路径"而不是可执行文件的"真实路径"。也就是说,路径可能是一个符号链接,而不是真正的文件。与深

#include <mach-o/dyld.h>
#include <limits.h>
int main(int argc, char **argv)
{
  char buf [PATH_MAX];
  uint32_t bufsize = PATH_MAX;
  if(!_NSGetExecutablePath(buf, &bufsize))
    puts(buf);
  return 0;
}

如果我正确理解你,你应该能够使用NSProcessInfo-arguments方法来获得可执行路径。

要将Objective-C代码与c++代码混合,只需将源文件的扩展名从。cpp更改为。mm。然后将Foundation.framework添加到目标的Link Binary With Library构建阶段。

[EDIT]更新显示argv[0][[[NSProcessInfo processInfo] arguments] objectAtIndex:0]之间的差异。

然后使用代码,您可以像下面的代码那样做:

#include <iostream>
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    // print out raw args
    NSMutableArray *arguments = [NSMutableArray array];
    for (NSUInteger i = 0; i < argc; i++) {
            NSString *argument = [NSString stringWithUTF8String:argv[i]];
            if (argument) [arguments addObject:argument];
    }
    NSLog(@"arguments == %@", arguments);
    const char *executablePath =
       [[[[NSProcessInfo processInfo] arguments] objectAtIndex:0]
                                             fileSystemRepresentation];
    printf("executablePath == %sn", executablePath);
    const char *executableDir =
         [[[[[NSProcessInfo processInfo] arguments] objectAtIndex:0]
             stringByDeletingLastPathComponent] fileSystemRepresentation];
    printf("executableDir == %sn", executableDir);
    [pool release];
    return 0;
}

如果我将cd放入可执行文件的父目录,然后使用相对路径执行可执行文件:

MacPro:~ mdouma46$ cd /Users/mdouma46/Library/Developer/Xcode/DerivedData/executablePath-ewememtbkdajumdlpnciyymduoah/Build/Products/Debug
MacPro:Debug mdouma46$ ./executablePath blah blah2

得到以下输出:

2011-08-10 12:59:52.161 executablePath[43554:707] arguments == (
    "./executablePath",
    blah,
    blah2
)
executablePath == /Users/mdouma46/Library/Developer/Xcode/DerivedData/executablePath-ewememtbkdajumdlpnciyymduoah/Build/Products/Debug/executablePath
executableDir == /Users/mdouma46/Library/Developer/Xcode/DerivedData/executablePath-ewememtbkdajumdlpnciyymduoah/Build/Products/Debug

因此,虽然argv[0]不一定是完整路径,但从[[[NSProcessInfo processInfo] arguments] objectAtIndex:0]返回的结果将是。

所以,有[[[NSProcessInfo processInfo] arguments] objectAtIndex:0],或者稍微简单一点的方法就是使用NSBundle本身,即使它是一个命令行工具(参见什么是命令行基础工具的"主包"?):

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
const char *executablePath =
    [[[NSBundle mainBundle] executablePath] fileSystemRepresentation];
[pool release];

也许您可以使用which命令。http://unixhelp.ed.ac.uk/CGI/man-cgi?which

如果它是一个"真正的" OS X应用程序,正确的方法是创建一个bundle:

http://developer.apple.com/library/mac/文档/CoreFoundation/引用/CFBundleRef/引用/必要reference.html