简体   繁体   English

应用程序认为它在我的主文件夹OSX中

[英]Application thinks it's in my home folder OSX

I compile using eclipse and c++11 under both Windows and OSX. 我在Windows和OSX下都使用eclipse和c ++ 11进行编译。 When I create a release and drag it to another folder, the Windows binary has the current current working directory. 当我创建发行版并将其拖动到另一个文件夹时,Windows二进制文件具有当前的当前工作目录。 The OSX release thinks it's in my home folder. OSX版本认为它在我的主文件夹中。 No matter where I place it, it thinks it is running in my home directory. 不管我在哪里放置它,它都认为它在我的主目录中运行。

I put a print statement in my code to prove it: Current working dir: /Users/Dom 我在代码中添加了一条打印语句以证明这一点:当前工作目录:/ Users / Dom

This problem only occurs if I double click the binary. 仅当我双击二进制文件时,才会出现此问题。 If I open a terminal and run the program using ./ it opens in the proper directory. 如果我打开终端并使用./运行程序,它将在正确的目录中打开。

I'm not sure how to fix this, but it needs to be fixed in order for the application to find relative path specific resources. 我不确定如何解决此问题,但是需要对其进行修复,以便应用程序找到相对于路径的特定资源。

Thanks! 谢谢!

SOLUTION: 解:

char path[1024];
uint32_t size = sizeof(path);
if (_NSGetExecutablePath(path, &size) == 0){ /*...*/ }

It won't work under Windows if you're UAC-elevated either, as the current directory is reset to %WINDIR%\\system32 during the elevation. 如果您使用UAC进行升级,则在Windows下也不起作用,因为在升级过程中当前目录将重置为%WINDIR%\\system32 All processes [on these platforms] have the concept of a current directory which is NOT guaranteed to be the program's directory. [在这些平台上]所有进程都具有当前目录的概念,该目录不能保证是程序的目录。 You'll likely need to find your binary in another way, such as one of these ways: https://stackoverflow.com/a/1024937/2101267 您可能需要以其他方式找到二进制文件,例如以下一种方式: https : //stackoverflow.com/a/1024937/2101267

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM