我得到一個錯誤信息:爲什麼鏈接器找不到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