添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
兴奋的草稿本  ·  HttpContext.Applicatio ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?

Surprise! They went and changed the language in incompatible ways. That was nice of them. – Eric Seppanen Feb 3, 2010 at 0:19 I don't code in C++ often enough to know or care who's in charge of C++ standards. But I once had the same experience as Morlock, of coming back to C++ after many years and discovering that #includes no longer use .h and cin/cout went and hid inside std::. – Eric Seppanen Feb 3, 2010 at 17:21
  • 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.
  • Hum, the "addition" file created does not work. I try to use it from the command line, writing: addition, but it says "command not found". I also tried to click on it and no terminal pops up. How could I make it work? – Morlock Feb 3, 2010 at 0:44 You really need to provide more information than this. (Why g++ should be used for example.) – John Parker May 13, 2013 at 16:23 g++ and gcc is the same compiler but with different frontend. One of this difference is that g++ with link with C++ standard libraries (like <iostream> your are using) and not the gcc frontend. – Steve S. Jul 10, 2017 at 1:47

    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;
    

    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.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.