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

Mor*_*ock 15 c++ linux gcc

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

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

谢谢你的建议.

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

gcc -o addition listing2.5.c
Run Code Online (Sandbox Code Playgroud)

.

#include <iostream.h>

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

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

.

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.)
Run Code Online (Sandbox Code Playgroud)

现在代码编译,但我不能使用文件名从命令行运行它.addition: command not found有什么建议吗?

nos*_*nos 27

  • cout在std :: namespace中定义,你需要使用std::cout而不是just cout.
  • 你也应该使用#include <iostream>旧的iostream.h
  • 使用g ++编译C++程序,它将链接到标准的c ++库中.gcc不会.如果你给它一个.c后缀,gcc也会把你的代码编译为C代码.为文件指定.cpp后缀.


Mik*_*hor 5

你需要<iostream><iostream.h>.

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

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

  • 不,使用""\n"`更好.我想你知道`std :: endl`也会刷新流. (4认同)

小智 5

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

  • 您确实需要提供比这更多的信息。(例如,为什么应该使用 g++。) (3认同)