C++ - gcc o n Fedora Linux - c++

I'm trying to run a simple Hello World! program on a Fedora Linux system through the terminal using gcc, and get the following:
[abder-rahman#Abder Desktop]$ gcc hello.cpp
hello.cpp:1:22: fatal error: iostream.h: No such file or directory compilation terminated.
Why is that? What is going wrong?
Thanks.

You should compile C++ programs with g++, not gcc.
There's no iostream.h header in standard C++. It's iostream (though iostream.h may be offered for backwards compatibility with ancient C++ code).

use g++ instead of gcc
g++ hello.cpp

Related

Compiling a .cpp file with c++ command instead of g++ (Linux)

Let's say we have a simple c++ file named hello.cpp which prints "Hello World!".
We generally create the executable using g++ hello.cpp. When I tried executing the command c++ hello.cpp it creates the executable successfully. Shouldn't it throw an error saying there is no c++ command available? and suggest us to use g++?
I tried running man c++ on the terminal, this brings up the GNU C Project page. So, does the terminal replace our c++ hello.cpp with g++ hello.cpp internally? It shouldn't do that right?
Additional Info:
Similarly, if I have a hello.c program that prints "Hello World!". When I execute c hello.c on the command line, I get the error:
$ c hello.c
c: command not found
This is expected since we have to use gcc hello.c. Why am not getting a similar error for the c++ hello.cpp?
On my Ubuntu system, I see this:
$ ls -l /usr/bin/c++
lrwxrwxrwx 1 root root 21 May 6 2019 /usr/bin/c++ -> /etc/alternatives/c++
$ ls -l /etc/alternatives/c++
lrwxrwxrwx 1 root root 12 May 6 2019 /etc/alternatives/c++ -> /usr/bin/g++
So c++ is actually an alias (symbolic link) to g++
The alternatives system allows changing what compiler c++ is an alias to:
$ update-alternatives --display c++
c++ - auto mode
link best version is /usr/bin/g++
link currently points to /usr/bin/g++
link c++ is /usr/bin/c++
slave c++.1.gz is /usr/share/man/man1/c++.1.gz
/usr/bin/clang++ - priority 10
/usr/bin/g++ - priority 20
slave c++.1.gz: /usr/share/man/man1/g++.1.gz
So c++ could actually be either g++ or clang++
For a C compiler, you don't want the c command, but cc, which again, could be either gcc or clang.
My CentOS system isn't quite so obvious with the symlinks, but g++ --version and c++ --version give the same output and both say they are g++. However, cc is a direct symlink to gcc there.
Shouldn't it throw an error saying there is no c++ command available? and suggest us to use g++?
Weeelll, there are no regulations or standards that restrict or require c++ command to do anything, so there is no "should" or "shouldn't". However, it would be strongly expected that c++ is a working C++ compatible compiler, that supports similar or same flags as cc does.
does the terminal replace our c++ hello.cpp with g++ hello.cpp internally?
A terminal is the device that displays things. Terminal does not replace it, it has no effect on it.
Most probably your system designer, but maybe administrator or publisher or distributor or package designer (or anyone in the chain), configured your system to provide a command named c++. Usually, that command is a symbolic link to a working C++ compiler, usually g++ on Linux systems. On my system, /usr/bin/c++ program is just installed with package named gcc, but some systems allow it to be configurable.
It shouldn't do that right?
As stated above, terminal shouldn't replace commands, it has no effect on it.
This is expected since
It is expected since there is no command named c. There are endless other unknown commands.
cc is the good old name for a C compiler. c99 is the standardized name of C99 compatible compiler.
Why am not getting a similar error for the c++ hello.cpp?
Because a command named c++ exists on your system.

c++ SDL2 - ld||cannot find -lmingw32|

I was creating program on windows with SDL2 and the program worked fine, but when I changed my os to linux mint (and install code::blocks, gcc, g++, SDL2), I run into troubles with compiling my code.
I have one error:
- ld||cannot find -lmingw32|
I guess that I don't have mingw32 library, where can I get it? Or is the problem diferent?
BTW: I also tryed to google it.
Thank for response.
Read the manual.
On linux you don't need -lmingw32. Instead, use
`sdl2-config --libs`
to get the list of all needed linker flags.
Example usage:
gcc -o myprogram myprogram.o `sdl2-config --libs`
Problem was solved by removing linker on mingw32 and lib rotozoom.h.

Cannot include standard libraries in C++ file

#include <iostream>
using namespace std;
int main(){
std::cout << "Hello World\n";
return 0;
}
command 1 (works)
clang hello.cc -o hello -lc++
command 2 (don't works)
/path/to/custom/clang hello.cc -o hello -lc++
main.cc:2:10: fatal error: 'iostream' file not found
#include <iostream>
^
1 error generated.
Why I can't compile with command 2 ?
It looks like you're trying to compile C++ with a C compiler. Try running clang++ instead.
clang++ hello.cc -o hello
Without running clang as a C++ compiler it won't have the C++ standard library headers available for you to include. Using clang++ the C++ standard library headers are available and the C++ standard library is linked for you automatically.
That is a known Ubuntu issue. Their clang just isn't set up right. I complained about it here -- and this remained unfixed for years.
But the good news is that it now works with the most recent 16.10 release.
Edit: Based on your updated question I would say that "custom clang" does not know about its include files.

installing gfortran in cygwin: gfortran: cyglto_plugin.dll not found

I'm trying to get the gfortran compiler with cygwin. When attempting to compile a hello world program, I get the following error:
gfortran: fatal error: -fuse-linker-plugin, but cyglto_plugin.dll not found
compilation terminated.
To install the compiler, I used the cygwin setup and selected
gcc-fortran: GNU compiler collection
libgfortran3: GCC fortran runtime library
I also mistakenly selected the toolchains for mingw, even though I don't have the mingw compiler (as far as I'm aware). g++, which I installed awhile ago, works fine.
What do I need to do to compile/find the library?
It is because of the gcc and gfortran versions are not same, try checking that. If so, do the following
$ apt-cyg remove gcc-fortran
$ apt-cyg install gcc-fortran

How to add speech.h to g++ in Cygwin?

I'm just trying to compile a c++ program using the emulated g++ compiler in Cygwin on a Windows 7 machine that has the following includes:
iostream
string
windows.h
stdio.h
mmsystem.h
initguid.h
objbase.h
objerror.h
ole2ver.h
speech.h
It compiles them all fine with the exception of speech.h, which is sort of the bread and butter of what I'm working on, soo.. yeah.
Returns the following:
test.cpp:11:20: fatal error: speech.h: No such file or directory compilation terminated.
Any and all help will be much appreciated.
The files that do compile are on your system path, so the compiler can find them. speech.h isn't, so you have to tell it where to look:
g++ -c test.cpp -I<Path_to_speech.h>/speech.h ...
I.e. if it's in C:\Users\Kirk\test\include, then
g++ -c test.cpp -IC:/Users/Kirk/test/include/speech.h ...