我得到一个错误信息:为什么链接器找不到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码(无论是部分我没加因代码行和阅读该文件后的工作原理,因为在添加零件读取文件之前一切都很完美。
您的代码是否在int main(){...}函数中? –
你的代码中有'main'吗?像'int main()'或'int main(int argc,char ** argv)'或类似的东西。你可以编译一个没有'main'的程序,但是你不能'链接'它。 – user93353
是一个'int main(int argc,char * argv []){}' – Tsundoku