0

I'm using the following code to get the current working directory of a C++ file

//Getting current working directory
getcwd( wd, 1024 );
std::string cwd = wd;

If the function in this file is called from another function, the current working directory becomes the path of the calling function.

How do I get the current directory of where the original file/binary is located?

I'm adding details as the question was confusing.

I've created a .so file which is called from Scilab. If I use readlink(), I get the path to be /usr/bin/scilab-bin which is not what I want. How do I get the path of the so file, the current function is in?

Misha
  • 379
  • 1
  • 2
  • 12
  • https://stackoverflow.com/questions/1528298/get-path-of-executable – drescherjm Nov 05 '17 at 14:41
  • getcwd gives different values when you are in different functions? wht is *a C++ file* in your question? – OznOg Nov 05 '17 at 14:43
  • I think the OP means program/executable when he/she says function. – drescherjm Nov 05 '17 at 14:45
  • 3
    Files don't have working directories. – stark Nov 05 '17 at 15:00
  • There is a bit of wrong terminology used in this question which will make it confusing. – drescherjm Nov 05 '17 at 15:07
  • It's unclear what you're asking for. If you mean you want to change the CWD to the path where your function is defined, you can use the [`__FILE__`](https://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html) macro to determine the directory. – user0042 Nov 05 '17 at 15:07
  • I've edited the question. Please check. – Misha Nov 05 '17 at 15:19
  • My first guess is ldd. – drescherjm Nov 05 '17 at 15:39
  • https://stackoverflow.com/questions/13148608/how-can-i-find-the-full-file-path-given-a-library-name-like-libfoo-so-1 – drescherjm Nov 05 '17 at 15:40
  • 2
    Your question confuses four different things: the current working directory of a process, the path to the original C++ source file, the path to the program (binary) that's currently running, and the path to the shared object file the current function is in. You need to edit your question and clarify what it is you're trying to get. – melpomene Nov 05 '17 at 15:59
  • The c++ function has been converted to .so file. I want to determine the path of the c++ function/.so file from inside the c++ function. This path will be used to determine the location of other functions. I'm not sure `ldd` is the right option. – Misha Nov 05 '17 at 16:05
  • I tried `__FILE__` and it returns the name of the c++ file. I used `dirname(__FILE__)` and it returns `.` ( single dot). How do I get the path of the so file, the current function is in? – Misha Nov 05 '17 at 16:17
  • I don't think you can get the path of the .so file from the path of the c++ source file. What happens after the .so file is installed in the system. That probably is a different folder than where it was built. – drescherjm Nov 05 '17 at 22:33

2 Answers2

0

This question is a near-duplicate of How to check what shared libraries are loaded at run time for a given process?. The information you need is only available from the /proc file system, telling you what files have been dynamically mapped into memory in your process.

bmargulies
  • 97,814
  • 39
  • 186
  • 310
0

You mention .so files in the comments, so I will assume your target is GNU/Linux. You can use the dladdr1 function to obtain the pathname of the .so file which contains an address, like this:

#include <string>
#include <vector>

#include <dlfcn.h>
#include <libgen.h>
#include <link.h>
#include <string.h>

static std::string
get_path ()
{
  Dl_info info;
  void *extra_info;
  if (dladdr1 (reinterpret_cast <void *> (&get_path),
               &info, &extra_info, RTLD_DL_LINKMAP) == 0)
    // Some form of error handling.
    return "";
  link_map *lm = static_cast<link_map *> (extra_info);
  // Make a copy of l_name because dirname modifies its argument.
  std::vector<char> buffer (lm->l_name, lm->l_name + strlen (lm->l_name) + 1);
  return dirname (buffer.data ());
}

The first argument to dladdr1 is quite arbitrary, but it is better not to use a symbol exported from the DSO because it could end up referring to something in a different object (due to PLT stubs and copy relocations).

Florian Weimer
  • 32,022
  • 3
  • 48
  • 92