Boost and path to the app dir?

Started by
8 comments, last by MARS_999 14 years, 6 months ago
I am trying to find the path to the app under OSX with boost. I this so far path my_path( "some_dir/file.txt" ); but that assumes I know that path. I need to find the path to the file correct? SO how does one do that? Thanks
Advertisement
Is boost::filesystem::current_path() what you're looking for?
Humble people don't refer to themselves as humble (W.R.T. "IMHO".)
Yeah I think that will work!

Thanks
I'd suggest examining the command line instead. The first parameter on the command line ( argv[0] ) should be the path to the program.

At least under Windows this can be different to the current working directory.
Quote:Original post by Adam_42
I'd suggest examining the command line instead. The first parameter on the command line ( argv[0] ) should be the path to the program.

At least under Windows this can be different to the current working directory.


This is a better idea...however I think the argv[0] is actually what was used to invoke the process? So you could end up just having the executable name, if the current directory==executable directory, or if the executable directory is part of the PATH env variable and the user just typed c:\>MyExecutable.exe for example. Does anyone know if it's ever possible for this to not even contain the filename, for example if the process was spawned from another?

I think what you really want is a boost::filesystem (read cross platform) version of the Win32 function GetModuleFilename. Looking in the boost docs it doesn't look like there is anything that fits the bill...

Perhaps you could look at the OSX api and see if there is something similar? If you are aiming for total cross platform, then I'm afraid I don't know of anything, but you could perhaps implement your own cross platform abstraction.
According to my experience with Linux and Windows the argv[0] only has what the user gave to it in the command line.

So it is pretty much useless if you want to know the full path to the executable.

I do not know OSX but I would be surprised if the behaviour is that much different there.
In Cocoa you would obtain the path with [[NSBundle mainBundle] bundlePath]. You can use Objective-C++ to access Cocoa from a C++ application, so that might be your best bet.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

That's funny, I asked pretty much this same question on StackOverflow the other day. Here's a link with some useful information. The short of it is that there's no cross-platform way to do it, which frankly sounds ridiculous to me!

http://stackoverflow.com/questions/1528298/get-path-of-executable
Quote:Original post by hymerman
That's funny, I asked pretty much this same question on StackOverflow the other day. Here's a link with some useful information. The short of it is that there's no cross-platform way to do it, which frankly sounds ridiculous to me!

http://stackoverflow.com/questions/1528298/get-path-of-executable
I went back and forth over this with SiCrane a few months back. IIRC, the upshot is that most platforms give you a path in argv[0] which is sufficient to exec() the current process.

There are unfortunately two problems with this: a) even when you get a working path, it tends to be relative to the PATH environment variable, and b) cygwin only hands you the executable name, no matter what you do.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

Sigh, issues with coding x platform and no good way to do it... I will take a look at maybe doing a preprocessor #ifdef statement or something to setup the system I am coding for and use the local API I guess. I was hoping BOOST would help since its supposed to be a C++ coders dream toolbox from what I hear.

Anyway thanks all for the info.

This topic is closed to new replies.

Advertisement