C++ headers missing from new c++ versions [duplicate] - c++

I'm trying to make something in Linux, but it complains that it can't find iostream.h. What do I need to install to get this file?

The correct name of this standard header is just iostream without an extension.
If your compiler still cannot find it, try the following:
find /usr/include -name iostream -type f -print
...and add it to your include path, following your compiler's documentation.

The header <iostream.h> is an antiquated header from before C++ became standardized as ISO C++ 1998 (it is from the C++ Annotated Reference Manual). The standard C++ header is <iostream>. There are some minor differences between the two, with the biggest difference being that <iostream> puts the included contents in namespace std, so you have to qualify cin, cout, endl, istream, etc. with "std::". As somewhat of a hack (it is a hack because header files should never contain "using" directives as they completely defeat the purpose of namespaces), you could define "iostream.h" as follows:
#ifndef HEADER_IOSTREAM_H
#define HEADER_IOSTREAM_H
#include <iostream>
using namespace std; // Beware, this completely defeats the whole point of
// having namespaces and could lead to name clashes; on the
// other hand, code that still includes <iostream.h> was
// probably created before namespaces, anyway.
#endif
While this is not exactly identical to the original antiquated header, this should be close enough for most purposes (i.e. there should be either nothing or very few things that you will have to fix).

I needed to compile partport on Debian and had problems (CentOS 4.5 worked fine). I did this without any success:
ln -s /usr/include/c++/4.5/iostream /usr/include/c++/4.5/iostream.h
I discovered that iostream.h was provided from C++, and I found it on CentOS 4.5.
So I copied the file iostream.h from CentOS 4.5 to Ubuntu 11.04 (Natty Narwhal), and it worked:
scp root#ip.centos-4.5:/usr/include/c++/3.3.4/backward/iostream.h /usr/include/c++/4.5/iostream.h

Related

Why is every frequently used standard C function automatically included in any C++ program? [duplicate]

I use Dev C++ 5.11. TDM-GCC 4.8.1
And this code runs well.
#include<iostream>
using namespace std;
int main()
{
printf("%d\n", 42);
cout << "good";
}
But as far as I know, iostream does not include "printf". (http://en.cppreference.com/w/cpp/header/iostream)
Why this code run? iostream acutally include printf? Is it a kind of standard?
The list of header files included in a system/standard header file is library implementation dependent (that is usually associated with the compiler you're using), and (as far as I remember) the C++ standard does not prohibit one header file from automatically including another one
In your case <iostream> is probably also #including <stdio.h> (or <cstdio>).
Relying on a header file being included in another is non portable to different standard libraries, compilers and platforms, so it's better to make sure that you explicitly #include everything you need.

Intel C++ Compiler can't find stdio.h

I am trying to build my code using the Intel C++ Compiler, but for some reason it fails with this error:
catastrophic error: cannot open source file "stdio.h"
In this line #include <stdio.h>.
Any ideas?
stdio.h is a standard header file; it's a bad idea to have a local file of the same name. If you meant to include the standard header, it should be on your include path, and you should include it with
#include <stdio.h>
You should also consider whether you might get more benefit from including <iostream> or including <cstdio> (like including <stdio.h>, but puts the symbols safely into the std namespace).
If you're running on Windows, then installing Visual Studio, then invoking "psxevars.bat" might solve your problem, it solved it for me.

Compiling a .CPP using GCC

how do I compile a .c or .cpp file using GCC?
I need to include some standard libraries (fstream, string, iostream) - how do I do this?
For clarification, here:
#pragma once
#include <iostream>
#include <fstream>
#include <string>
#include"ocgenerator.h";
#include"structures.h";
#include"util.h";
#include"util2.h";
using namespace std;
(my .h files are in the same directory as the src file)
If I use the command:
gcc src.cpp -o src.o
I get a lot of errors: memcpy, atoi, atol, strncmp, etc, ... "are not declared in this scope". What should I add to the command?
edit: Or is it a scope thing, and do I have to add std:: to all those functions?
memcpy and strncmp are declared in <cstring>, atoi and atol in <cstdlib>. Just include these headers to bring in their declarations.
Side notes :
No semicolon after preprocessor directives, including #include "".
No using namespace std;, especially not in headers !
(Why is "using namespace std" considered bad practice?)
Since you're building a C++ project, don't forget to link with the standard library via -lstdc++, or use g++ which forwards it to the linker for you.
Note that with GCC you don't have to prefix standard C functions with std:: (as they are also declared in the global namespace), but you should anyway for your code to be standard-compliant.
The cplusplus site helps out. If you search for a specific function, on the top you will find the necessary references for the code to compile:
http://www.cplusplus.com/reference/cstdlib/atol/
You can even check in the URL. In this case, you need 'cstdlib'.
Regarding the compiler, there are several available, g++ is a good option.
I also suggest creating a makefile for automating the building process, because it becomes an headache when your codebase grows.
For any library function you use in your code, read the man page for that function to see which header declares it, and #include that header in any source file that uses the function.
For example, man memcpy shows the memcpy(3) man page (the (3) refers to section 3 of the manual; use man 3 memcpy to specify the section).
NAME
memcpy - copy memory area
SYNOPSIS
#include <string.h>
void *memcpy(void *dest, const void *src, size_t n);
...
memcpy is part of the C standard library, and is declared in <string.h>. For C++ you can use <string.h>, but it's probably better to use <cstring>, which puts the function name in the std namespace. In general, each standard C header <foo.h> is duplicated as a C++ header <cfoo>.
I get a lot of errors: memcpy, atoi, atol, strncmp, etc,
memcpy and strncmp are declared in <cstring>. atoi and atol are <cstdlib> (or in <string.h> and <stdlib.h> if you're programming in C rather than in C++).
And be sure to use the g++ command, not the gcc command, to compile C++ source. They both invoke the same compiler (gcc compiles C++ code if the source file name ends in .cpp), but the g++ command adds some options that are needed for C++.

Where is namespace std defined?

I want to take a peek inside of namespace std but, i'm not able to actually find the file on my computer where it is defined. I tried googling this but, i haven't had much luck.
On most Unix systems, the C++ headers are usually stored in /usr/include/c++/<version>/, where <version> is the GCC/libstdc++ version (i.e. 4.9 or 4.9.2), or else the libc++ version i.e. v1.
Within that directory are all (or just most?) of the standard-mandated headers, which are mostly just ordinary C++ code. For libstdc++, note in particular that most of the older headers just include something in bits/; few of the C++11-specific headers do this.
A list of every thing included in namespace std can be found here.
If you are using Visual Studio you can find it locally here :
~\Microsoft Visual Studio\VC\crt\src
There is also an online representation here.
NOTE : Edit the src files at your own risk, I'd recommend not editing them at all.
Many of the things implemented in the std namespace are templated, which means their entire implementation will be in the header files. For example, std::vector should be in the vector header file. Simply look at the options for your compiler to find out where those header files are located.
There may be some non-templated parent classes and free standing functions, which will not be in the headers. Again, look at the compiler documentation to see if the source files are included somewhere and where they would be.

istream and ostream problem - C++ [duplicate]

This question already has answers here:
C++ error: ‘string’ has not been declared
(3 answers)
Closed 10 months ago.
I am using two compilers g++ and Dev - C++. when I compile my program on Dev-C++ it compiles perfectly. but when i try to compile it on g++ it gives me two errors:
In file included from a2test.cpp:27:
----.h:25: error: 'ostream' has not been declared
----.h:26: error: 'istream' has not been declared
Can anyone tell me what can I do to solve this problem.
Thanks
Make sure you include fstream. Also, put "std::" before ostream or put "using namespace std" somewhere.
It would help if you posted the code, as right now I'm just guessing based on common mistakes.
I would guess you forgot to include fstream because different compilers may use different header files and it may be the case that g++ has a header file with
// iostream
#include <fstream>
While Dev-C++ may have
// iostream
// no include for fstream in this file
So you're accidentally importing the correct header file rather than doing it explicitly.
For header files, I just use this site when I forget which one.
ostream - C++ Reference
It seems you need to include ostream to get ostream. Probably the same thing for istream.
My psychic debugging skills indicate that the problem likely means that your call to g++ and the g++ Dev-CPP is using are different versions of gcc. One of the headers in the (presumably earlier) version included with Dev-CPP might #include a standard C++ header that it doesn't need to, which would allow headers which aren't strictly correct to compile.
Make sure you've actually #included <iostream>, or <istream> and <ostream>, or <iosfwd> -- some header which actually includes these types for you.
(Side Note: Please don't use Dev-CPP -- the project is pretty much dead, and the editor commits quite a few sins. Plus it isn't a good editor anyway. How about Code::Blocks or Visual Studio (both free) instead?)
dont know if this will help, but firstly, yuou should remember to omit the ".h" that some other compilers (MS-C++) use, but not ANSI/G++.so it should be just
#include <iostream>
Secondly, don't forget :
using namespace std;
3rdly, it's been a long time, but if I remember correctly, in g++, th istream and ostream functions are in the "std" library .. so you can do something like this :
using std::istream;
//later
istream::iostate state = ...
or alternatively, you can use them directly like this :
std::istream::iostate state = ...
Hopefully that'll give you some ideas.