What is `pwd` bundled with a Mac?

I am running a GLFW application (which I wrote in C ++)

I run it as follows:

./prog.app/Contents/MacOS/prog # from the command line 

However, my code cannot read the files with relative fix correctly.

So, I believe what happens is that this Mac Bundle changes the current directory.

1) why did he change this? 2) can I control this?

Thanks!

+4
source share
4 answers

Never make any assumptions about the current working directory when starting the application. Finder is consistent enough, but this is not the only way to launch the application. When debugging under Xcode, for example, the original working directory will be different, and there are various other methods that may or may not set the working directory as expected. You must test your application by running it from the command line in a completely unrelated directory - if it still finds its files in this state, then you are in good shape.

+1
source

You can use this code snippet to find out the location of the program.

 #include <mach-o/dyld.h> char exe_path[MAXPATHLEN]; char link_path[MAXPATHLEN]; uint32_t size = sizeof(exe_path); if (_NSGetExecutablePath(exe_path, &size) != 0) assert(); if (realpath(exe_path, link_path) == 0) assert(); return link_path; 
+4
source

This is often the user's home directory. I'm not sure if this is guaranteed, but you can use chdir () inside Cocoa applications. Depending on your requirements, users can have a better experience if you use file system bookmarks (10.6+) or aliases (previously). Or even absolute paths that are still resistant to changing the working directory.

+2
source

If you look at the sources of GLFW, you will see that on OSX during init it explicitly changes the current directory. Take a look here and scroll to line 236 (or search for chdir).

I found your SO question because I'm currently trying to understand why GLFW makes an explicit chdir during init. I discovered this because I have a program that runs on all platforms except OSX. On OSX, it cannot find data files using a relative path. My temporary solution is to get the current directory when starting main and reset the current directory after initializing GLFW.

0
source

Source: https://habr.com/ru/post/1300334/


All Articles