Reputation: 23
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!
Upvotes: 0
Views: 9968
Reputation: 1
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.
Upvotes: 2
Reputation: 5728
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.
Upvotes: 0
Reputation: 294
You can't execute a .cpp file. Find where the compiled program is and run that.
Upvotes: 0