简体   繁体   English

C ++编译但在执行时会出错

[英]C++ compiles but gives error when executed

I am new to Linux Ubuntu 11.10 and have basic C++exposure. 我是Linux Ubuntu 11.10的新手,并且有基本的C ++曝光。

I installed the g++ by 我安装了g ++

sudo apt-get install build-essential

and created a directory cpp in my home directory. 并在我的主目录中创建了一个目录cpp。 I then wrote a program hello.cpp in my cpp directory 然后我在我的cpp目录中编写了一个程序hello.cpp

#include <iostream>
using namespace std;

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

and compiled using 并使用编译

g++ -W hello.cpp -o hello

The program compiles without any errors/warnings. 程序编译时没有任何错误/警告。 When I try to execute the file 当我尝试执行该文件

./hello.cpp

I get error messages: 我收到错误消息:

line 3: using: command not found
line 6: syntax error near unexpected token `('
line 6: `int main() {'

I tried looking at a lot of posts but could not resolve this. 我试过看了很多帖子但是无法解决这个问题。 I have MS VisualStudio on Windows, but I would rather learn C++ on Ubuntu. 我在Windows上有MS VisualStudio,但我宁愿在Ubuntu上学习C ++。 Thanks in advance. 提前致谢。

I think that the problem is that you're trying to execute the .cpp source file rather than the generated executable. 我认为问题在于您尝试执行.cpp源文件而不是生成的可执行文件。 Try running ./hello instead of ./hello.cpp , since hello is the actual executable. 尝试运行./hello而不是./hello.cpp ,因为hello是实际的可执行文件。 The errors you're currently getting are caused by the shell interpreter choking on C++ syntax, since it's trying to run it as a shell script. 您当前获得的错误是由shell解释器阻塞C ++语法引起的,因为它试图将其作为shell脚本运行。

Hope this helps! 希望这可以帮助!

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

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