使用 gcc 在 linux 中链接 <iostream.h>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2188751/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 19:45:26  来源:igfitidea点击:

linking <iostream.h> in linux using gcc

c++linuxgcc

提问by Morlock

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.

我正在尝试在 linux (linux mint 8) 中运行我的第一个 C++ 程序。我使用 gcc 或 g++,两者都有相同的问题:编译器没有找到我试图导入的库。

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.

我怀疑我应该在工作文件夹中复制 iostream.h 文件(我不知道在哪里查找),将我的文件移动到其他地方进行编译或使用某种选项。

Thanks for your suggestions.

感谢您的建议。

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

这是 gcc 命令、c++ 代码和错误消息:

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 foundAny suggestion?

现在代码可以编译了,但我无法使用文件名从命令行运行它。addition: command not found有什么建议吗?

采纳答案by nos

  • coutis defined in the std:: namespace, you need to use std::coutinstead 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.
  • cout在 std:: 命名空间中定义,您需要使用std::cout而不仅仅是cout.
  • 你也应该使用#include <iostream>不旧的iostream.h
  • 使用 g++ 编译 C++ 程序,它将链接到标准的 C++ 库中。gcc 不会。如果您给它一个 .c 后缀,gcc 也会将您的代码编译为 C 代码。为您的文件添加 .cpp 后缀。

回答by AraK

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

你需要的<iostream><iostream.h>是非标准的太旧的标头。尝试这个:

#include <iostream>

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

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

回答by Mike Anchor

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

你需要<iostream><iostream.h>

They are also header files not libraries.

它们也是头文件而不是库。

Other things to fix, coutshould be std::coutand you should use std::endlinstead of "\n".

其他需要修复的事情cout应该是std::cout,你应该使用std::endl而不是"\n".

回答by AaronLee

please use g++ not gcc to compile it

请使用 g++ 而不是 gcc 来编译它

回答by Rahul Satal

If you don't want to use stdalongside coutas below-

如果您不想将stdcout一起使用,如下所示-

std::cout << "Hello World";

std::cout << "Hello World";

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

您还可以通过“ using namespace”关键字在程序开头定义std为-

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

现在你不需要写 std,每次你使用 I/O 操作。