【问题标题】:GCC iostream fstream error in Ubuntu 13.10Ubuntu 13.10 中的 GCC iostream fstream 错误
【发布时间】:2013-11-03 17:10:45
【问题描述】:

我使用的是 Ubuntu 13.10。我收到以下代码的一些错误。

#include <stdlib.h>
#include <stdio.h>
#include <fstream.h>

int main(int argc, char *argv[])
{
    error.set_program_name(argv[0]);    

    if ( argc != 2 )
    {
    //  printf(argv[0] + " usage: fifo_client [string] \n");
    /// cout << argv[0] << " usage: fifo_client [string]" << endl;
        exit(EXIT_FAILURE);
    }

    ofstream out(fifo_file);
    if(out)

        out << argv[1] << endl;

    return(EXIT_SUCCESS);
}

如果我使用命令运行上述程序 a.c

gcc a.c -o a

a.c:1:20: fatal error: iostream: No such file or directory
 #include <iostream>
                    ^
compilation terminated.

我不知道是什么问题。

【问题讨论】:

  • 重命名为a.cpp,并使用g++命令。
  • 您显示的代码也不是#include &lt;iostream,但您编译的代码可以。所以你没有向我们展示你正在编译的内容。

标签: c++ c ubuntu gcc


【解决方案1】:

使用 g++ 代替 gcc。如果 c++ 文件具有正确的扩展名(例如 .cpp)或正确的参数(-x c++),gcc 可以编译它,但是添加与 C++ 库链接所需的参数过于复杂,无法避免简单的解决方案。

【讨论】:

    【解决方案2】:

    问题在于您正在混合 C 和 C++ 代码并使用 GCC 进行编译。

    【讨论】:

      【解决方案3】:

      试试

      #include <fstream>
      using namespace std;
      

      而不是#include &lt;fstream.h&gt; 无论如何,您的源代码并不完整,无法提出正确的建议。

      【讨论】:

        【解决方案4】:

        我在我的编译器中运行了你的代码并得到了以下错误:-

        test2.c:3:21: fatal error: fstream.h: No such file or directory
         #include <fstream.h>
                             ^
        compilation terminated.
        

        所以我认为你的问题有错字。

        这是因为你混合了 c 和 c++ 代码,fstream 是 c++ 的一部分。尝试通过 g++ 运行。

        【讨论】:

          猜你喜欢
          • 2014-01-30
          • 2014-09-11
          • 2014-06-16
          • 1970-01-01
          • 2014-01-22
          • 2014-01-14
          • 2017-05-21
          • 2013-11-26
          • 1970-01-01
          相关资源
          最近更新 更多