I'm getting the next error message:
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
The only code I have is:
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);
Now, I come from a Java background but I've been Googling around, it says I might be missing a reference but I don't know what could it be, I even added the #include <stdio.h>
and I read something about adding an extern
but I have no idea where since I have no other files unless I need to refer to the .dat file?
EDIT I also tried in a point to cast the byte array (char*)malloc(pos);
but it didn't help either.
EDIT 2 the whole code is using the NS-3 framework but everything compiled perfectly until I added these lines. It looks something like this:
#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;
}
The compiler error message is:
[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}
I am extremely sure the NS-3 code (both the part I didn't add due to code lines and the the one after reading the file works because before adding the part to read the file everything worked perfectly.
The only code I have is
Eiher you didn't put this code into main
function (which you are required to have), or your compiler/linker invocation is incorrect. It's impossible to say which, given the details you've supplied.
Also, your question has incorrect title: you don't have a problem reading a file, you have a problem linking your program (that intends to read a file).
Your program must contain one main()
function.
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.