0

I use the NetBeans to program C++ , I want to get the current absolute path of the executable file

(~/NetBeansWorkSpace/project_1/dist/Debug/GNU-Linux-x86/executableFileName)

so I use

1, system("pwd")

2,getcwd(buffer,bufferSize)

then click the run button but they all get the wrong path: ~/NetBeansWorkSpace/project_1

Here is the surprise , I run the bash

cd ~/NetBeansWorkSpace/project_1/dist/Debug/GNU-Linux-x86/executableFileName

./executableFileName

I get the right path .

It's WHY ???

Zhang LongQI
  • 494
  • 1
  • 11
  • 25
  • 4
    Seems your IDE is setting the current directory of your program when it runs it. In general, you can't rely on the working directory to be where your executable is. – Cameron Oct 29 '12 at 16:12
  • `pwd` and `getcwd` return the current working directory, not a path to the executable's location... – bames53 Oct 29 '12 at 16:26

4 Answers4

1

Nothing is wrong - NetBeans is running your program with the current working directory set to the project directory (~/NetBeansWorkSpace/project_1).

Your program should not depend on the current directory being the same as the directory where your program resides. See this thread if you want to see a few different methods for getting the absolute path of your program.

Community
  • 1
  • 1
Matt Kline
  • 10,149
  • 7
  • 50
  • 87
1

As everyone else has stated, NetBeans is setting the working directory before running your application. If you want to get the working directory of the executable, I believe that the following should work.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char const* *argv) {
    char *resolved = realpath(argv[0], NULL);
    if (resolved != NULL) {
        char *fname = strrchr(resolved, '/');
        if (fname != NULL) {
            fname[1] = '\0';
        }
        printf("absolute path of %s is %s\n", argv[0], resolved);
        free(resolved);
    } else {
        perror("realpath");
    }
    return EXIT_SUCCESS;
}
D.Shawley
  • 58,213
  • 10
  • 98
  • 113
0

NetBeans starts your application from ~/NetBeansWorkSpace/project_1/ by prefixing the path dist/Debug/GNU-Linux-x86/ leading to it.

Open a shell, do a cd ~/NetBeansWorkSpace/project_1/, then do dist/Debug/GNU-Linux-x86/executableFileName and you'll be getting the same result as if you ran the app from NetBeans.

alk
  • 69,737
  • 10
  • 105
  • 255
0

For Linux:
Function to execute system command

int syscommand(string aCommand, string & result) {
    FILE * f;
    if ( !(f = popen( aCommand.c_str(), "r" )) ) {
            cout << "Can not open file" << endl;
            return NEGATIVE_ANSWER;
        }
        const int BUFSIZE = 4096;
        char buf[ BUFSIZE ];
        if (fgets(buf,BUFSIZE,f)!=NULL) {
            result = buf;
        }
        pclose( f );
        return POSITIVE_ANSWER;
    }

Then we get app name

string getBundleName () {
    pid_t procpid = getpid();
    stringstream toCom;
    toCom << "cat /proc/" << procpid << "/comm";
    string fRes="";
    syscommand(toCom.str(),fRes);
    size_t last_pos = fRes.find_last_not_of(" \n\r\t") + 1;
    if (last_pos != string::npos) {
        fRes.erase(last_pos);
    }
    return fRes;
}

Then we extract application path

    string getBundlePath () {
    pid_t procpid = getpid();
    string appName = getBundleName();
    stringstream command;
    command <<  "readlink /proc/" << procpid << "/exe | sed \"s/\\(\\/" << appName << "\\)$//\"";
    string fRes;
    syscommand(command.str(),fRes);
    return fRes;
    }

Do not forget to trim the line after

ETech
  • 1,613
  • 16
  • 17