c++macosbinarybinaries

How do I execute an existing binary that's in the same location as the main cpp file?


I'm making a program that depends heavily on another C binary. Since I don't feel like learning how to use headers and what not yet, I wanted to take the simple rout and just run a pre-compiled binary from the same folder in my cpp program.

Right now, my folder is setup like this: It has main.cpp, CMakeLists.txt, and the ibootim binary. Inside of main.cpp, how would I call ibootim?

From coding in python, it's taught me that I should be able to run

system("./ibootim");

but that doesn't work. Terminal tells me that there's no file found. Obvioiusly if I were to put the entire path to that binary, it would work. However, if other users were to download this, it would not work for them since they don't have the same computer, username, etc. as I do.

So my first question, my primary concern would be:

How do you run another binary that's in the same directory in a c++ program?

If this isn't possible for some reason, then I can try downloading ibootim from source and maybe using the header file:

How do you execute code from a C header in a C++ program?


Solution

  • In c++ if you want to use a binary you can use std::system() function.

    But to do this the binary must be on the PATH. If your binary is not on the path you can do something like this.

    #include <iostream>
    
    int main(){
    #if _WIN32
        std::system("./mybinarie.exe");
    #else
        std::system("./mybinarie");
    #endif
    
    return 0;
    }
    

    Starting the shell with std::system will ensure that you are in your working folder and that if the binary is in the working folder it should work.