Search code examples
c++windowscygwinmingwcommand-prompt

Program works in cmd, immediately exits with status 127 in cygwin, no output in emacs shell


I've got the following program, which I'm compiling using MinGW:

#include <iostream>
#include <string>
using namespace std;

int main() {
    cout << "aoeu" << endl;
    string str;
    return 0;
}

When I compile and run this program in the Windows command prompt, I get the following output as expected:

C:\p\conscell>conscell.exe
aoeu

However, when I run it in Cygwin, I get the following output (or lack thereof):

$ ./conscell.exe
$ echo $?
127

As you can see, the exit code is 127, indicating that the file or a library dependency could not be found. Additionally, when I run the program in M-x shell in emacs (which runs C:/Program Files/Emacs/libexec/emacs/25.3/x86_64-w64-mingw32/cmdproxy.exe) there is no output.

When I delete the string str; line, changing the program to this:

#include <iostream>
#include <string>
using namespace std;

int main() {
    cout << "aoeu" << endl;
    return 0;
}

Then Cygwin and Emacs CAN find the file, as witnessed by the Cygwin output:

$ ./conscell.exe
aoeu

This would appear to indicate that using an std::string in the program causes it to depend on some files that cmd can find, but Cygwin and cmdproxy.exe cannot. Any help on figuring out what files those are would be appreciated.


Solution

  • You should check your executable with dependency walker or similar utilities to figure out on which libraries it depends on. And then put them into the same folder as executable so they can be found regardless of current environment. See Dynamic-Link Library Search Order. Alternatively you can link them statically.