Boost.Filesystem how to find out in which directory your executable is?

26,015

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();
Share:
26,015
Rella
Author by

Rella

Hi! sorry - I am C/C++ noobe, and I am reading a book=)

Updated on July 09, 2022

Comments

  • Rella
    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
    Emile Cormier about 13 years
    This 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
    Rella about 13 years
    +1 Is it possible to remove executable filename and keep only path to folder?
  • rubenvb
    rubenvb about 13 years
  • davka
    davka about 13 years
    @Blender: check out the parent_path() method of the path class. My boost version is old so I don't have it to try
  • Nim
    Nim about 13 years
    Restoring 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
    ildjarn about 13 years
    @Nim : That's a bit obtuse -- path already has a parent_path() member function.
  • Nim
    Nim about 13 years
    @ildjarn, ofcourse there is the easy way! :) I completely missed the comment by @davka above mine!!!
  • ergosys
    ergosys over 12 years
    This can fail in several ways since it relies on the search path.
  • Ruslan
    Ruslan about 7 years
    It won't work if you have chdired to another directory, and your app was run via a relative path, like ./myapp.
  • edA-qa mort-ora-y
    edA-qa mort-ora-y over 6 years
    You 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
    Andry over 5 years
    great example of how bad answers become accepted on the site
  • StaceyGirl
    StaceyGirl about 5 years
    The 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 defined argc/argv and 5) std::experimental which is not really the standard library as the answer claims.
  • munsingh
    munsingh almost 4 years
    This works as expected. The boost::filesystem::current_path() will return the path of the executable. No matter which directory you run it from.
  • StaceyGirl
    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
    Muzuri over 2 years
    This also will work: boost::dll::program_location().parent_path().parent_path();