Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

linking <iostream.h> in linux using gcc

Tags:

c++

linux

gcc

I'm trying to run my very first c++ program in linux (linux mint 8). I use either gcc or g++, both with the same problem: the compiler does not find the library I am trying to import.

I suspect something like I should either copy the iostream.h file (which I don't know where to look for) in the working folder, move my file to compile somewhere else or use an option of some sort.

Thanks for your suggestions.

Here's the gcc command, the c++ code, and the error message:

gcc -o addition listing2.5.c

.

#include <iostream.h>

int Addition(int a, int b)
{
    return (a + b);
}

int main()
{
    cout << "Resultat : " << Addition(2, 4) << "\n";
    return 0;
}

.

listing2.5.c:1:22: error: iostream.h: No such file or directory
listing2.5.c: In function ‘main’:
listing2.5.c:10: error: ‘cout’ undeclared (first use in this function)
listing2.5.c:10: error: (Each undeclared identifier is reported only once
listing2.5.c:10: error: for each function it appears in.)

Now the code compiles, but I cannot run it from the command line using the file name. addition: command not found Any suggestion?

like image 666
Morlock Avatar asked Feb 03 '10 00:02

Morlock


People also ask

Where is iostream h in Linux?

h file; however, there is a standard iostream file (with no extension), installed in /usr/include/c++/4.4/. It's part of the libstdc++6-4.4-dev package.

Does GCC work on Linux?

The GNU Compiler Collection, commonly known as GCC, is a set of compilers and development tools available for Linux, Windows, various BSDs, and a wide assortment of other operating systems. It includes support primarily for C and C++ and includes Objective-C, Ada, Go, Fortran, and D.


2 Answers

  • cout is defined in the std:: namespace, you need to use std::cout instead of just cout.
  • You should also use #include <iostream> not the old iostream.h
  • use g++ to compile C++ programs, it'll link in the standard c++ library. gcc will not. gcc will also compile your code as C code if you give it a .c suffix. Give your files a .cpp suffix.
like image 87
nos Avatar answered Oct 07 '22 08:10

nos


please use g++ not gcc to compile it

like image 27
AaronLee Avatar answered Oct 07 '22 09:10

AaronLee