Boost.Filesystem how to find out in which directory your executable is?
Solution 1
If you mean from inside the executable that you're running, you can use boost::filesystem::current_path()
Solution 2
boost::filesystem::system_complete(argv[0]);
e.g.
[davka@bagvapp Debug]$ ./boostfstest
/home/davka/workspaces/v1.1-POC/boostfstest/Debug/boostfstest
Note that this gives you the full path including the executable file name.
Solution 3
You cannot, Boost.Filesystem does not provide such functionality.
But starting with Boost 1.61 you can use Boost.Dll and function boost::dll::program_location
:
#include <boost/dll.hpp>
boost::dll::program_location().parent_path();
Solution 4
You can't do it reliably with boost::filesystem.
However if you're on windows you can call GetModuleFileName
to get the complete path of the executable and then use boost::filesystem
to get the directory. ( see parent_path)
Solution 5
As discussed more comprehensively here, the most reliable way to do that is not through boost::filesystem. Instead, your implementation should take into the consideration the operating system on which the application is running.
However, for a quick implementation without portability concerns, you can check if your argv[0] returns the complete path to executable. If positive, you can do something like:
namespace fs=boost::filesystem;
fs::path selfpath=argv[0];
selfpath=selfpath.remove_filename();
Comments
-
Rella almost 2 years
So I run my app. I need for it to know where its executable is. How to find path to it using Boost.Filesystem?
-
Emile Cormier about 13 yearsThis will not work if the program directory is different from the current working directory. For example, consider a program started from the shell in this manner:
./foo/program
. -
Rella about 13 years+1 Is it possible to remove executable filename and keep only path to folder?
-
rubenvb about 13 years
-
davka about 13 years@Blender: check out the
parent_path()
method of thepath
class. My boost version is old so I don't have it to try -
Nim about 13 yearsRestoring parity, I can't see what's wrong with this either...@Blender, save it to a
std::string
, do a reverse find for `\` and a substring to that point - voila. -
ildjarn about 13 years@Nim : That's a bit obtuse --
path
already has aparent_path()
member function. -
Nim about 13 years@ildjarn, ofcourse there is the easy way! :) I completely missed the comment by @davka above mine!!!
-
ergosys over 12 yearsThis can fail in several ways since it relies on the search path.
-
Ruslan about 7 yearsIt won't work if you have
chdir
ed to another directory, and your app was run via a relative path, like./myapp
. -
edA-qa mort-ora-y over 6 yearsYou should call
path::canonical
on the resulting path if you wish to get at the original executable location. Otherwise if a symbolic link is used you'll get a path to where that is located. -
Andry over 5 yearsgreat example of how bad answers become accepted on the site
-
StaceyGirl about 5 yearsThe problem is that: 1) this needs access to
argv
array 2)argv[0]
might be missing (argc
can be 0) 3) yields invalid result if program found through environment variables 4) the example is incomplete - does not definedargc
/argv
and 5)std::experimental
which is not really the standard library as the answer claims. -
munsingh almost 4 yearsThis works as expected. The boost::filesystem::current_path() will return the path of the executable. No matter which directory you run it from.
-
StaceyGirl almost 4 years@munsingh
current_path
returns "current directory", not executable path. If you run a program from the shell from a home folder, that folder will be your current directory, but you executable can be in/usr/bin/something
. This is the most common way to execute command-line utilities on Unix systems. -
Muzuri over 2 yearsThis also will work:
boost::dll::program_location().parent_path().parent_path();