Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

So I run my app. I need for it to know where its executable is. How to find path to it using Boost.Filesystem?

like image 724
Rella Avatar asked Apr 17 '11 14:04

Rella


People also ask

What is boost :: filesystem :: path?

boost::filesystem::path is the central class in Boost. Filesystem for representing and processing paths. Definitions can be found in the namespace boost::filesystem and in the header file boost/filesystem. hpp . Paths can be built by passing a string to the constructor of boost::filesystem::path (see Example 35.1).

How do I get the directory that a program is running from?

For Windows system at console you can use system( dir ) command. And console gives you information about directory and etc. Read about the dir command at cmd .

What is Boost Filesystem?

The Boost Filesystem Library provides portable facilities to query and manipulate paths, files, and directories. The motivation for the library is the need to be able to perform portable script-like operations from within C++ programs.


2 Answers

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.

like image 76
davka Avatar answered Sep 20 '22 12:09

davka


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();
like image 22
StaceyGirl Avatar answered Sep 21 '22 12:09

StaceyGirl