sop sop - 1 year ago 128
C++ Question

Eclipse c++ debugging executable is not finding the file with relative path

I have a project that has more makefiles. At some specific folders, there are some makefiles that can create binaries/executables. I right-click on the makefile and do a

Make Tarkgets -> Build -> all
, so I get the executable right there. If I run the executable from console, it works fine, but if I do a right-click on it and
Debug as -> Local C/C++ Application
, it runs but at some point it is reading a file with relative path:

FILE *f = fopen(fName, "rb"); // fname is a relative path : ../../../path/to/file

if(f == NULL)

and it is not finding it; the pointer is empty/NULL. To me it seems like Eclipse is searching for the file from the place where the project was opened, not from where the executable is. I suppose this because if I create a project from the makefile that created the executable, it works, it finds the file. But from that place I cannot see the sources, so, for placing breakpoints I have to run step-in by step-in until I get in the right place/right source.

Can anyone help me to fix this thing? Is there a way to make it search the file from the place where the executable is? Thanks

P.S. : I've seen this post, it's similar, but it did not pointed me to anything...

Answer Source

I guess work directory is different for both cases, and thats why you can not open your file. Try to use absolute path for fopen() or look for a way to specify proper work directory for the second case.

There is a way to modify the working directory, as mentioned in this answer:

  • right-click on binary/executable Debug As -> Debug Configuration ...
  • in Arguments tab, in the bottom, there is a Working directory: area, uncheck the Use default and add the correct path (where is the executable/binary). The buttons Workspace... and File System... may help
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download