2012-11-25 70 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