2012-11-25 67 views
0

我得到一個錯誤信息:爲什麼鏈接器找不到main()?

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); 

現在,我來自一個Java的背景,但我一直在谷歌搜索它說我可能會缺少一個參考,但我不知道它會是什麼,我甚至添加了#include <stdio.h>,我讀了一些關於添加extern的內容,但我不知道在哪裏,因爲我沒有其他文件,除非我需要參考。 dat文件?

編輯我也試過在一個點上投出字節數組(char*)malloc(pos);但它也沒有幫助。

編輯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碼(無論是部分我沒加因代碼行和閱讀該文件後的工作原理,因爲在添加零件讀取文件之前一切都很完美。

+0

您的代碼是否在int main(){...}函數中? –

+0

你的代碼中有'main'嗎?像'int main()'或'int main(int argc,char ** argv)'或類似的東西。你可以編譯一個沒有'main'的程序,但是你不能'鏈接'它。 – user93353

+0

是一個'int main(int argc,char * argv []){}' – Tsundoku

回答

0

唯一的代碼我已經是

Eiher你沒有把這個代碼放到main功能(你需要有),或者你的編譯器/連接調用不正確。根據你提供的細節,不可能說出哪個。

此外,您的問題有不正確的標題:您沒有讀取文件的問題,您有問題鏈接您的程序(即打算讀取文件)。

+0

對不起,關於標題我真的不知道我不得不這樣做在C除此之外我有一個主要=/ – Tsundoku

0

您的程序必須包含一個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; 
} 
+0

我已經有主,但它仍然不工作= /即使返回0; – Tsundoku