G++ not finding <iostream.h> in Ubuntu - c++

I just installed Ubuntu and tried making the famed "Hello World" program to make sure that all the basics were working. For some reason though, g++ fails to compile my program with the error: "'cout' is not a member of 'std'". I've installed the build-essential package. Am I missing something else?
#include <iostream.h>
int main() {
std::cout << "Hello World!" << std::endl;
return 0;
}
Looks pretty good to me...

Use #include <iostream> - iostream.h is not standard and may differ from the standard behaviour.
See e.g. the C++ FAQ lite entry on the matter.

The standard header is called <iostream>, not <iostream.h>. Also, it is agood idea to compile your C++code with the -Wall and -pedantic flags, which can point out lots of errors with non-standard code that g++ would otherwise ignore. Use:
g++ -Wall -pedantic myprog.cpp

Sounds like it did find iostream.h but it does not define cout in the std namespace. It is there for backwards compatibility with older programs that expect cout to be in the global namespace.

use
#include<iostream>
using namespace std;
without namespace you won't be able to use cout or cin

Related

Program doesn't start when linking boost filesystem

I'm trying to run a helloworld program which uses boost filesystem.
I'm on Windows with MinGW 8.1 and boost 1.70.
The problem is that, although everything compiles, the program doesn't run. I mean, it runs but doesn't print anything, which means the main function is not even executed:
#include <boost/filesystem.hpp>
#include <iostream>
using namespace std;
using namespace std::string_literals;
namespace fs = boost::filesystem;
int main()
{
cout << "Hello Boost!" << endl;
fs::path abHome{"C:/Users/Me"s};
fs::path jsonFile = abHome / "jsonFile.json"s;
if (!fs::exists(jsonFile)) {
cout << "Creating json file from scratch." << endl;
}
}
"Hello Boost" isn't ever printed to the console.
I've compiled with both CMake and g++ from command line to try to better understand what's going on:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -lboost_filesystem-mgw81-mt-x64-1_70 -lboost_system-mgw81-mt-x64-1_70 -I"C:/Code/boost_1_70_0"
I've compiled boost for MinGW by following the guide and everything went well, in the output folder I see many different versions of each library based on the default targets (I haven't really picked them, just went with the defaults).
How can I debug the launch of main.exe to see what's causing the crash? It's been many years since I wrote C++ so I need help to get back on track! :)
The problem was, as #kenba pointed out, that the dynamic linking of the boost dlls was failing.
I erroneously thought I had linked the static version of the boost libraries.
To actually achieve that I should have used this command:
g++ main.cpp -o main -L"C:/Code/boost_1_70_0/stage/lib" -l:"libboost_filesystem-mgw81-mt-x64-1_70.a" -l:"libboost_system-mgw81-mt-x64-1_70.a" -I"C:/Code/boost_1_70_0"
instead of the one I posted in the OP.

Why g++ compiler is not able to find unique_ptr?

I am trying to compile a small C++ code which invloves unique_ptr as given below.
#include <iostream>
#include <memory>
using namespace std;
int main()
{
unique_ptr<int> p1(new int);
}
when I tried to compile the code using g++, it is throwing up 'unique_ptr' was not declared in this scope. I was trying to compile on Linux box. even I tried with '-std=c++11' option. It was saying 'unrecognized command line option -std=c++11'. Can any one please let me know how to fix this?
You need to include it, it comes out of the <memory> library
#include <memory>
According to the GCC 4.4 release notes, unique_ptr was not in GCC's standard C++ library before 4.4.
So you might want to check your GCC version first, using g++ --version like #40two said.

C++ <iostream.h> Error

I am at the absolutely newest level of new when it comes to C++. It may seem like a noob mistake, but I think I'm missing something with my first program, "Hello World!".
I'm running from Ubuntu (not sure if this is any different from working with Windows), and I'm using a book called Teach Yourself C++ in 21 Days.
The code I'm resembling looks exactly like this:
#include <iostream.h>
int main()
{
cout <<"Hello World!\n";
return 0;
}
I have this exactly in my text editor, but I keep getting greeted by the same error whenever I try to compile it!
first.cpp:2:22: fatal error: iostream.h: No such file or directory
compilation terminated.
I'm pretty distressed as this is literally the first step in my coding career! I'm not sure if ubuntu needs to be treated differently than Windows (which is what the book is using as reference).
Help!
There are two problems here:
You need to omit the .h suffix:
#include <iostream>
Also, cout is an unqualified name, and needs to be qualified with the std namespace since you are not using namespace std:
std::cout << "Hello World!\n";
There shouldn't be any iostream.h it's simply called iostream and should look like this:
#include <iostream>
int main()
{
std::cout <<"Hello World!\n";
return 0;
}
(also notice the std:: before the cout, since it means that it's from the standard namespace.)
You want just iostream
#include <iostream>
I suspect the book is very old. Names are qualified in the std namespace, so you may want to add
using namespace std;
For now at least.
use
#include <iostream>
in general STL header file don't have a .h
you need to compile using g++ compiler, not by gcc
g++ hello.cpp

Compiling simple Hello World program on OS X via command line

I've got a simple hello world example that I'm trying to compile on OS X, named hw.cpp:
#include <iostream>
#include <string>
using namespace std;
int main() {
cout << "Hello world!" << endl;
return 0;
}
I'd like to compile it using gcc, but I've had no success. I'd also like to hear the other options, like using Xcode ?
Try
g++ hw.cpp
./a.out
g++ is the C++ compiler frontend to GCC.
gcc is the C compiler frontend to GCC.
Yes, Xcode is definitely an option. It is a GUI IDE that is built on-top of GCC.
Though I prefer a slightly more verbose approach:
#include <iostream>
int main()
{
std::cout << "Hello world!" << std::endl;
}
g++ hw.cpp -o hw
./hw
user#host> g++ hw.cpp
user#host> ./a.out
Compiling it with gcc requires you to pass a number of command line options. Compile it with g++ instead.
Also, you can use an IDE like CLion (JetBrains) or a text editor like Atom, with the gpp-compiler plugin, works like a charm (F5 to compile & execute).
The new version of this should read like so:
xcrun g++ hw.cpp
./a.out
Use the following for multiple .cpp files
g++ *.cpp
./a.out
You didn't specify what the error you're seeing is.
Is the problem that gcc is giving you an error, or that you can't run gcc at all?
If it's the latter, the most likely explanation is that you didn't check "UNIX Development Support" when you installed the development tools, so the command-line executables aren't installed in your path. Re-install the development tools, and make sure to click "customize" and check that box.

Yet another linker issue

I'm having a linking issue with a basic C++ program. No, I'm not including .cpp files!
This is what's happening.
main.cpp:
#include "header.h"
#include <iostream>
int main() {
std::cout << "Hello!";
}
header.h:
#ifndef _HEADER_H
#define _HEADER_H
class Something {
public:
printContents();
};
#endif
something.cpp:
#include "header.h"
#include <iostream>
Something::printContents() {
cout << "This class's Contents!!";
}
What's happening is that I get a compiler error going: multiple definitions of some standard C function, such as strtod:
g++ -o ... main.o
build/....main.o: In function
`strtod':
../MinGW/bin/../lib/gcc/mingw32/3.4.5/../../../../include/stdlib.h:318:
multiple definition of `strtod'
build/..something.o:...something.cpp:(.text+0x0):
first defined here collect2: ld
returned 1 exit status
If I get rid of #include <iostream> in one of the two occasions and get rid of the couts, it will compile. What's going on? I'm using g++ and NetBeans to compile.
I tried in the command line:
g++ *.h *.cpp -o program
and the same thing happened.
Please note that _HEADER_H is an illegal name in C++ user code - names beginning with the underscore and an uppercase letter are reserved for the C++ implementation. This does not normally cause noticeable problems, but when you use what may be a common name in the implementation like HEADER in this context, it well might.
Modify,
Something::printContents()
{
std::cout << "This class's Contents!!";
}
NOTE: Specify the return datatype.
One of your problems is right here:
I tried in the command line: g++ *.h
*.cpp -o program
Don't pass your header files... Try something like this:
g++ *.cpp -o program
I could not reproduce your exact problem. I get this to compile and link nicely with the following few notes:
Add a void return type to the printContents-function (So it says void printContents(); in the header and void Something::printContents() { in the implementation-file)
Use std::cout rather than just cout. cout is not defined in the scope it is used
Make sure header.h ends with a blank line
Use HEADER_H rather than _HEADER_H (like Neil Butterworth says)
I use the command line g++ main.cpp something.cpp to compile.
I see a couple of problems. You shuold define the returning value of the function
printContents()
and you must write
std::cout
if you don't write
using namespace std;
The problem was in a multi-installation of MinGW. I had one already installed, and when I got Qt on my computer, it had installed it's own MinGW. Bummer, I ported the code to my university's servers and it ran OK.
Bummer!!
Thanks everybody for the help, I will definitely follow your guidelines in the future.
Header names - no underscores
Correct return type
Real code in the forums!
Leo Bruzzaniti