How to get current source path in C++ - Linux

c++

Solution

The path used to compile the source file is accessible through the standard C macro `__FILE__` (see http://gcc.gnu.org/onlinedocs/cpp/Standard-Predefined-Macros.html)

If you give an absolute path as input to your compiler (at least for gcc) `__FILE__` will hold the absolute path of the file, and vice versa for relative paths. Other compilers may differ slightly.

If you are using GNU Make and you list your source files in the variable `SOURCE_FILES` like so:

SOURCE_FILES := src/file1.cpp src/file2.cpp ...

you can make sure the files are given by their absolute path like so:

SOURCE_FILES := $(abspath src/file1.cpp src/file2.cpp ...)

Problem

I want to be able to get the current source file path. ``` string txt_file = CURRENT_FILE_PATH +"../../txt_files/first.txt"; inFile.open(txt_file .c_str()); ``` Is there a way to get the CURRENT_FILE_PATH ? I don't mean the executable path. I mean the current location of the source file that the code is running from. Thanks a lot, Giora.

Original source

Related problems