查找Mac上的执行路径
C++ Find execution path on Mac
假设我的可执行文件位于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
- 无法获取进程 ID 4 (ntoskrnl.exe) 的可执行路径
- CMake将依赖项复制到可执行输出路径
- 定义在其中执行不同代码路径的代码节
- 获取自己的可执行文件的当前路径 (C++)
- 只有已知执行路径的模板编译
- 在C 中使用AssocqueryString获取可执行路径
- 如何将执行路径与文件路径和参数分开
- 任务管理器如何获取进程的命令行可执行路径
- 如何对字符串执行路径操作
- C++ 执行路径中带有空格的.exe
- 获取系统本机形式的可执行路径
- 获取cmake可执行路径以在自定义命令上运行它
- 调试执行路径中缺少msvcr100d.dll和msvcp100d.dll
- C/ c++ -可执行路径
- 如何通过程序更改进程的执行路径
- 查找Mac上的执行路径
- 从C++中的可执行路径(或从hWnd或从pid)获取程序名称
- c++在execv中运行grep搜索可执行路径
- 如何确保在编译时没有执行路径到达某一行
- 在c++中有没有办法允许不同的数据类型有不同的执行路径?