0

次のエラー メッセージが表示されます。

usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In
function `_start':  (.text+0x20): undefined reference to `main' 
collect2: ld returned 1 exit status

私が持っている唯一のコードは次のとおりです。

FILE *f = fopen("data/file.dat", "rb");
fseek(f, 0, SEEK_END);
long pos = ftell(f);
fseek(f, 0, SEEK_SET);

char *bytes = malloc(pos);
fread(bytes, pos, 1, f);
fclose(f);

#include <stdio.h>さて、私は Java のバックグラウンドを持っていますが、グーグルで調べてみました。参照が欠落している可能性があると書かれていますが、それが何であるかはわかりませexternん。 .dat ファイルを参照する必要がない限り、他にファイルがないのでどこにあるのでしょうか?

編集私もバイト配列をキャストしようとしました(char*)malloc(pos);が、どちらも役に立ちませんでした。

EDIT 2 コード全体が NS-3 フレームワークを使用していますが、これらの行を追加するまですべてが完全にコンパイルされました。次のようになります。

#include "ns3/core-module.h"
#include "ns3/point-to-point-module.h"
#include "ns3/network-module.h"
#include "ns3/applications-module.h"
#include "ns3/wifi-module.h"
#include "ns3/mobility-module.h"
#include "ns3/csma-module.h"
#include "ns3/internet-module.h"

using namespace ns3;

NS_LOG_COMPONENT_DEFINE ("ThirdScriptExample");

int 
main (int argc, char *argv[])
{
      .....
//STARTS FILE READING
  FILE *f = fopen("data/Terse_Jurassic_10_14_18.dat", "rb");
  fseek(f, 0, SEEK_END);
  long pos = ftell(f);
  fseek(f, 0, SEEK_SET);

  char *bytes = (char*)malloc(pos);
  fread(bytes, pos, 1, f);
  fclose(f);

  Simulator::Stop (Seconds (10.0));

  pointToPoint.EnablePcapAll ("third");
  phy.EnablePcap ("third", apDevices.Get (0));
  csma.EnablePcap ("third", csmaDevices.Get (0), true);

  Simulator::Run ();
  Simulator::Destroy ();
  return 0;
}

コンパイラ エラー メッセージは次のとおりです。

[1888/1930] cxxprogram:  -> build/scratch/data/data
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In
function `_start': (.text+0x20): undefined reference to `main'
collect2: ld returned 1 exit status Build failed  -> task in 'data'
failed (exit status 1):   {task 43470800: cxxprogram  -> data}

NS-3 コード (コード行のために追加しなかった部分と、ファイルを読み取る部分を追加する前にすべてが完全に機能したため、ファイルを読み取った後の部分の両方が機能することを確信しています。

4

2 に答える 2

0

私が持っている唯一のコードは

このコードを関数に入れなかったかmain(必要です)、コンパイラ/リンカーの呼び出しが正しくありません。あなたが提供した詳細を考えると、どちらかを言うことは不可能です。

また、質問のタイトルが正しくありません。ファイルの読み取りに問題はありません。プログラムのリンクに問題があります(ファイルを読み取ることを目的としています)。

于 2012-11-25T04:21:25.323 に答える
0

プログラムには 1 つの関数が含まれている必要main()があります。

#include <stdio.h>
#include <string.h>
int main(int argc, char *argv[])
{
    FILE *f = fopen("data/file.dat", "rb");
    fseek(f, 0, SEEK_END);
    long pos = ftell(f);
    fseek(f, 0, SEEK_SET);

    char *bytes = malloc(pos);
    fread(bytes, pos, 1, f);
    fclose(f);
    free(bytes);
    return 0;
}
于 2012-11-25T04:28:10.113 に答える