简体   繁体   English

无法执行已编译的C ++ exe文件

[英]Can't execute compiled C++ exe file

I am having trouble executing my C++ code. 我在执行C ++代码时遇到问题。 I have written a basic "Hello World" program, and compiled it using the g++ make command. 我编写了一个基本的“Hello World”程序,并使用g ++ make命令编译它。 Here is my code: 这是我的代码:

#include <iostream>

using namespace std; 

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

I am on Windows 10, using Emacs for code editing, and CygWin for compilation. 我在Windows 10上,使用Emacs进行代码编辑,使用CygWin进行编译。 I saved this file as hello.cpp. 我将此文件保存为hello.cpp。 I then navigated to the directory in CygWin. 然后我导航到CygWin中的目录。 Then I did the command make hello . 然后我做了命令make hello This created hello.exe. 这创建了hello.exe。 Then, I attempted to execute the file using ./hello.exe . 然后,我尝试使用./hello.exe执行该文件。 I also tried ./hello which also didn't work. 我也试过./hello./hello When I type one of these commands and hit Enter, it just on the next line, not doing anything. 当我输入其中一个命令并点击Enter时,它就在下一行,没有做任何事情。 I can type in this blank line, but it won't do anything. 我可以输入这个空行,但它不会做任何事情。 Does anyone know a way to make my code execute properly. 有没有人知道让我的代码正确执行的方法。 Thank you. 谢谢。

EDIT: I tried running this at cpp.sh, an online C++ compiler, and it worked fine. 编辑:我尝试在cpp.sh,一个在线C ++编译器上运行它,它运行正常。

Your program probably is working but the console window is closing before you can see anything. 您的程序可能正在运行,但控制台窗口正在关闭,您可以看到任何内容。

Try adding an input at the end of the program so it will wait. 尝试在程序结束时添加输入,以便等待。

IE IE

int a;
cin >> a;

Your code is most likely executing, but not outputting anything. 您的代码很可能正在执行,但不会输出任何内容。 That's because it's failing. 那是因为它失败了。 Try checking the return value after it has run with echo $? 使用echo $?运行后尝试检查返回值echo $? . If it's not 0 then it has crashed. 如果它不是0那么它已经崩溃了。 Also run it in gdb and see if it fails. 也可以在gdb运行它,看它是否失败。 The reason why it's failing is most likely a windows/cygwin clash - it's not your code. 它失败的原因很可能是windows / cygwin冲突 - 这不是你的代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM