LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-18-2008, 02:24 AM   #1
otacon 14112
Member
 
Registered: Apr 2004
Location: /
Distribution: ubuntu (gutsy)
Posts: 46

Rep: Reputation: 15
undefined reference to `main'


Hi guys, when I wrote the following program and ran it, I got the errors listed below.

Code:
#include <stdio.h>
#include <string.h>

int main(void)
{

  FILE* f_write;
  char buf[1001];

  f_write = fopen("hello.c", "w");

  strcpy(buf, "#include <stdio.h>\n\nint main(void)\n{\n  printf(\"Hello world!\");\n  return 0;\n}\n");
//  buf = "#include <stdio.h>\n\nint main(void)\n{\n  printf(\"Hello world!\";\n  return 0;\n}";

  fputs(buf, f_write);

  fflush(stdout);

  close();

  system("gcc -o hello hello.c");

  return 0;
}
/usr/lib/gcc/i486-linux-gnu/4.1.3/../../../../lib/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
collect2: ld returned 1 exit status

Does anyone know why it's giving me these errors? I was just messing around and wanted to try and just see if it would work, and now I'm actually really wanting to get it to work just to do it. I tried the compile line that I put in the program, and it works on the command line. Why doesn't it work in the program?

otacon
 
Old 03-18-2008, 05:18 AM   #2
ophirg
Member
 
Registered: Jan 2008
Location: Israel
Distribution: Kubuntu 13.10
Posts: 134

Rep: Reputation: 34
why fflush to stdout?
and you need to include stdlib.h
 
Old 03-18-2008, 11:24 AM   #3
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
First, add this to the top:

Code:
#include <unistd.h>
Try compiling the program again, you'll get an error.
I think close does not do what you think it's suppose to do. But I'll get back to that in a second... Your hello.c is empty when you compile it (try putting system("cat hello.c") right before the other call to system), so there is no main function defined in it (this is because you didn't actually close the file). _start is actually the entry point in an elf executable, the standard C library actually implements this, and does some set up/clean up before/after calling main. If it doesn't exist, then there will be an undefined reference to main in the C runtime library's _start function. Back to the actual problem; fclose is what you are looking for, and it takes a FILE pointer as an argument.

Code:
#include <stdio.h>
#include <string.h>

int main(void)
{

  FILE* f_write;
  char buf[1001];

  f_write = fopen("hello.c", "w");

  strcpy(buf, "#include <stdio.h>\n\nint main(void)\n{\n  printf(\"Hello world!\\n\");\n  return 0;\n}\n");
//  buf = "#include <stdio.h>\n\nint main(void)\n{\n  printf(\"Hello world!\";\n  return 0;\n}";

  fputs(buf, f_write);

  fclose(f_write);

  system("gcc -o hello hello.c");

  return 0;
}

Last edited by 95se; 03-18-2008 at 11:27 AM.
 
Old 03-18-2008, 12:27 PM   #4
otacon 14112
Member
 
Registered: Apr 2004
Location: /
Distribution: ubuntu (gutsy)
Posts: 46

Original Poster
Rep: Reputation: 15
Ok so my mistake was I should have put in fputs and stdlib. I should have remembered that stdlib solves alot of warnings. I never worked much with file handling in c. I just found an online example that made me want to try it, and I ended up modifying it to see what would happen. I should have known I would forget some stuff during my vacation from c. I've been messing with php and mysql a lot lately.

Thanks ophirg and 95se. But why would I need unistd.h? I skimmed over an online article about it and I didn't really see anything that would help my program unless I overlooked something.
 
Old 03-18-2008, 04:23 PM   #5
95se
Member
 
Registered: Apr 2002
Location: Windsor, ON, CA
Distribution: Ubuntu
Posts: 740

Rep: Reputation: 32
Quote:
Originally Posted by otacon 14112 View Post
Thanks ophirg and 95se. But why would I need unistd.h?
No problem. You don't need unistd.h, it was just my way of telling you why it would compile with the close() function call you had, but why it isn't working the way you thought (ie. the function exists, its just not the one you want).
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Getting undefined reference to main error?? Mistro116@yahoo.com Programming 14 07-29-2011 08:28 AM
Undefined reference to 'main' myhanguk Programming 5 07-05-2007 03:32 AM
undefined reference to main () ?. rameshs Linux - Newbie 3 01-11-2007 06:29 PM
undefined reference to main cjbrockwell Programming 3 09-06-2006 05:09 PM
emacs 77: undefined reference to `main' creznedmick Programming 2 09-16-2003 07:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:21 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration