1

I need to have full path of C++ files (name of file is not neccesary). It musn't to be depended on path of execution. I can use __FILE__, hovewer it gives me only name of file. I was seeking in boost::filesystem, but I only found boost::filesystem::current_path() - unfortunately it gives me path of directory from which I run program.

I looking for path of cpp file, not path of exe. I need it for other tool to generate groups of cpp files after directory of cpp files.

Any ideas?

Edit: Maybe it is possible to use bash script which gives actual directory of cpp file (not sh file)?

Marcin
  • 422
  • 6
  • 17
  • Are you looking for the path within your program, or on command line in the shell? – unxnut Jul 09 '14 at 20:28
  • I have edited my question. I need to use C++ language. – Marcin Jul 09 '14 at 20:35
  • I use gcc-4.7.3 on Linux 64 bit. It gives me only name of file. – Marcin Jul 09 '14 at 20:38
  • If `__FILE__` doesn't do it for you, then this can't be done easily and would need special compiler support. If you're using makefiles, you might be able to do something like add `-DFULL_FILE_PATH=""` to your compiler's command line and then use the macro `FULL_FILE_PATH` in your source code. – Adam Rosenfield Jul 09 '14 at 20:42
  • According to http://bytes.com/topic/c/answers/453661-short-form-__file__, `__FILE__` is feeded by the compiler path given to the compiler, So can you change your tool chain to give full path of the file to your compiler ? – Jarod42 Jul 09 '14 at 20:46
  • Related to [full-file-path-in-assert-macro](http://stackoverflow.com/questions/2449576/c-c-full-file-path-in-assert-macro) – Jarod42 Jul 09 '14 at 20:48
  • It doesn't works for me. – Marcin Jul 09 '14 at 21:01
  • 1
    How do you compile your file ? It works on coliru: [shortpath](http://coliru.stacked-crooked.com/a/969101c88ed3c859) [fullpath](http://coliru.stacked-crooked.com/a/d3980d6274c7d1ae) – Jarod42 Jul 09 '14 at 21:17
  • I check it second time and it works for me. I don't know why it doesn't works before. Hovewer it need changes in method of compilation. I have many files, makefiles, so it can be difficult. I thought about something like `std::string cpp_path = system(get_current_path.sh)`. Eventually I can write script which generate files `dirname` for each directory with name of path and in *cpp files I can read this file. – Marcin Jul 09 '14 at 21:32
  • 2
    Do you have something that generate the Makefile or these files are yours ? The make rule `%.o: %.cpp $(CC) $(CFLAGS) -c $(realpath $<) -o $@` should do the trick. – Jarod42 Jul 09 '14 at 21:39

0 Answers0