简体   繁体   中英

Running c++ file with raspberry pi

I was making a simple hello world c++ program. For some reason, it won't run after I compile it. Here's the program:

#include <iostream>
using namespace std;

int main() {
    cout << "hello world";
}

I compiled using:

g++ -std=c++0x helloworld.cpp

No errors. However, when I tried running it using ./helloworld.cpp, I got this:

./helloworld.cpp: line 2: using: command not found
./helloworld.cpp: line 5: syntax error near unexpected token `('
./helloworld.cpp: line 5: `int main()'

Of course, I tried looking this up, and found a link that had someone asking almost the exact same question as mine. ( C++ compiles but gives error when executed ). They told me to remove the .cpp. However, I tried doing ./helloworld and I still got errors. It told me this:

bash: helloworld: No such file or directory

Also, I was in the directory with helloworld.cpp in it, so I don't think that was the problem. Any help would be appreciated. Thanks!

g++ -std=c++0x helloworld.cpp

should have left you with an a.out file that you can execute.

However, when I tried running it using ./helloworld.cpp, I got this:
...

You can't execute the helloworld.cpp source from the shell.


You probably should use

g++ -std=c++0x helloworld.cpp -o helloworld
                            # ^^^^^^^^^^^^^

to name the executable file other than a.out

You can call ./helloworld then to run your compiled program.

You can't execute a .cpp file. Find where the compiled program is and run that.

The .cpp file is the file you wrote. It's a text file, so you obviously can't "run" it. If you build a program you create an executable, which you can execute. This is a different file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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