C ++在Mac上查找执行路径 [英] C++ Find execution path on Mac

查看:71
本文介绍了C ++在Mac上查找执行路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Lets say my executable is located at /Users/test_user/app on Mac OSX and I am running it from /Users/test_user/Desktop/run_app:

Desktop run_app$ /Users/test_user/app/exec 

在我的C ++代码中,如何找到可执行文件位置的路径(在本例中为/users/test_user/app )?我需要在代码中的该路径下引用其他文件,并且不想在代码内放置绝对路径,因为某些用户可能会将文件夹放置在其他位置.

Within my C++ code how can I find the path to the location of the executable (which in this case would be /users/test_user/app)? I need to reference some other files at this path within my code and do not want to put absolute paths within the code as some users might place the folder in a different location.

推荐答案

提供了我的理解,您应该可以使用 NSProcessInfo

Provided I'm understanding you correctly, you should be able to use NSProcessInfo's -arguments method to get the executable path.

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

To mix in Objective-C code with C++ code, you can just change the filename extension of the source file in question from .cpp to .mm. Then add the Foundation.framework to the Link Binary With Library build phase of your Target.

已更新,以显示 argv [0] [[[[NSProcessInfo processInfo] arguments] objectAtIndex:0] 之间的区别.

updated to show the difference between argv[0] and [[[NSProcessInfo processInfo] arguments] objectAtIndex:0].

然后使用代码,您可以执行以下代码:

Then to use the code, you could do something like in the following code:

#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 == %s\n", executablePath);

    const char *executableDir =
         [[[[[NSProcessInfo processInfo] arguments] objectAtIndex:0]
             stringByDeletingLastPathComponent] fileSystemRepresentation];

    printf("executableDir == %s\n", executableDir);

    [pool release];

    return 0;
}

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

If I then cd into the parent directory of the executable, and then execute the executable using a relative path:

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] 返回的结果将是

So, while argv[0] may not necessarily be a full path, the result returned from [[[NSProcessInfo processInfo] arguments] objectAtIndex:0] will be.

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

So, there's [[[NSProcessInfo processInfo] arguments] objectAtIndex:0], or a slightly simpler approach is just to use NSBundle itself, even if it is a command-line tool (see What is the "main bundle" of a command-line foundation tool?):

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

这篇关于C ++在Mac上查找执行路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆