开发者

linking <iostream.h> in linux using gcc

开发者 https://www.devze.com 2022-12-19 03:50 出处:网络
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 thelibrary I am trying to import.

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开发者_如何学Python 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?


  • 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.


please use g++ not gcc to compile it


You need <iostream> not <iostream.h>.

They are also header files not libraries.

Other things to fix, cout should be std::cout and you should use std::endl instead of "\n".


You need <iostream>, <iostream.h> is non-standard too-old header. Try this:

#include <iostream>

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

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


If you don't want to use std alongside cout as below-

std::cout << "Hello World";

You can also define std at beginning of program by 'using namespace' keywords as-

     #include <iostream >

     using namespace std;

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

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

Now you need not to write std,everytime you use I/O operations.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号