I'm a bit of a newbie to C++, but I have some programming experience. I made a basic program, following a guide I found on t'internet. It compiled with g++ easily, but when I ran it it threw these errors:
./FP.cpp: line 1: //: Is a directory
./FP.cpp: line 3: using: command not found
./FP.cpp: line 5: syntax error near unexpected token ('
./FP.cpp: line 5:int main ()'
I'm using Geany on a Raspberry Pi (but using a command line to run the program as the Geany interpreter doesn't work). Here's the program:
// First program in c++
#include <iostream>
using namespace std;
int main()
{
cout << "Hello World!";
return 0;
}
Any ideas?
Cheers!
It looks like you're trying to execute the source file, i.e. run it as a shell script.
To compile your program you would do something like this:
$ g++ -Wall FP.cpp -o FP
This produces an executable named FP. So you should now see both the original program, FP.cpp, and the executable, FP, in your current directory.
To run the executable (compiled program):
$ ./FP
Calling ./FP.cpp tries to execute the source code, which is not what you want. You need to compile it via g++, and then run the executable (usually ./FP)
Related
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.
In Eclipse I am using mingw-w64 V5.3.0 as the compiler and I have enabled C++11 (which was the solution in the following related post here and here). My compilation command looks as follows (from eclipse console window):
g++ -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0 -o "src\Launch.o" "..\src\Launch.cpp"
The simple code I am attempting to run is as follows:
#include <stdio.h>
#include <iostream>
#include <string>
int main()
{
std::string test = std::to_string(0);
std::cout <<"HI" << std::endl;
return 0;
}
The program compiles but for an error stated in the question. Running it without the to_string line works fine. There have been suggestions back in 2012 that MingW has a problem with to_string(), but was resolved in later versions has shown here.
Screen shot below:
And the console output is as follows:
The following is the error I receive when running the code from the .exe directly:
As mentioned in the comments, the issue is a linking issue, however it is linking correctly to iostream which is in the same directory as string.
Eclipse was looking in System32 for the library and driver files, despite PATH and Eclipse pointing to the MingW64 compiler on the computer. Eclipse is also showing it is linked to the MingW64 libraries correctly as the path to the headers when right-clicking and opening deceleration is shown to be correct. Why then it looks in System32 for the library at run-time I don't understand.
The problem was 'solved' by copying the entire MingW64 compiler driver folder into the System32 folder on Windows.
Can we write a program in c++ that compile a c++ source code using a compiler ?
for example we have a program that takes the file name and then compiles it:
Enter your C++ source code file name : cppSource.cpp
your program compiled.
output: cppSource.exe
Or:
Enter your C++ source code file name : cppSource.cpp
Sorry. There is no C++ Compiler in your computer.
I do not mean that we write a Compiler.
I mean write a program that compiles the cpp file using a compiler.
How can we access a compiler. and how to detect that in a computer a compiler is installed or not.
Use system function to call any executable, for example g++ compiler:
#include <stdlib.h>
int retval = system("g++ file.cpp");
Return value of system can be checked, and it will be the return value of the called executable if the shell was able to execute it. In this case, usually it will be 0 if a compiler exists and the code compiled successfully.
Alternatively (and also to prevent called program output from being displayed), for additional details, it would be possible to redirect the program output to a file and then open that file and parse it's content.
int retval = system("g++ file.cpp > output.txt");
I recently installed xcode command line tools on my Mac. Its OS is version 10.9.5. To test out the g++ compiler I created a simple "hello world" c++ program, called it program.cpp and put it on my desktop:
#include <iostream>
using namespace std;
int main()
{
cout<<“Hello World”;
return 0;
}
Then I intended to create a compiled version of program.cpp, opened terminal, changed my directory to the desktop and typed the following command:
g++ -o program program.cpp
I received the following compilation errors:
program.cpp:7:11: error: non-ASCII characters are not allowed outside of
literals and identifiers
cout<<“Hello World”;
^
program.cpp:7:14: error: use of undeclared identifier 'Hello'
cout<<“Hello World”;
^
program.cpp:7:25: error: non-ASCII characters are not allowed outside of
literals and identifiers
cout<<“Hello World”;
^
3 errors generated.
Is this a problem with the command line tools or something else? Any help would be appreciated.
You aren't using double quote characters:
“Hello World”
is not the same as:
"Hello World"
I guess this is due to a copy-and-paste from PDF or something, as programs like Word like to change "" to “”.
Also if you are using the Xcode Command Line tools, you are actually using clang and not g++.
There might be an "U.S. International - PC" input method set on your mac (assuming you are on mac) and that has accented characters which result in '“' being inserted when you expected '"'.
enter link description here
I installed the following:
MINGW32_NT-6.1 i686 Msys
I am working with the command line.
Wrote the "typical" HelloWorld.cpp program.
IF I compile with: cpp HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (18k)
BUT execution fails: 16 bit MS-DOS Subsystem. NTVDM CPU error
IF I compile with: g++ HelloWorld.cpp -o HelloWorld.exe COMPILE is good. (48k)
Execution is good.
I cannot determine the BEST way to execute the compile and what the difference is between the methods. Any suggestions? or good references?
THANKS.
"cpp" is the "C PreProcessor", not the compiler. So you're just getting something strange in HelloWorld.exe
Execute the "type HelloWorld.exe" and see what it gives. It shouldn't even be a binary file - just a long text file with all the "#includes" and "#defines" replaced.
To your question - the second way is "right", because you actually invoke the compiler/linker and produce a valid executable. The first "way" is a valid command, but it has almost nothing to do with compilation and linking.