compilation error : undefined reference to ‘main’

Question

I have been learning C back from basics. Recently i tried out a simple program myHello.c , which contained the following code :

1. //myHello.c

2. #include”hello.h”

3. int main(void)

4. {

5. hello(“WORLD”);

6. return 0;

7. }

next, i created hello.h that contained the following code :

1. //hello.h

2. void hello(const char* name);

Finally i created helloFunc.c that contained the declaration for hello() :

1. //helloFunc.c

2. #include<stdio.h>

3. #include”hello.h”

4. void hello(const char* name)

5. {

6. printf(“hello, %s \n”,name);

7. }

I compiled the C sources as follow :

gcc -Wall myHello.c helloFunc.c -o newHello

I received the following error :

/usr/lib/gcc/i486-linux-gnu-4.3.3/../../../../libcrt1.o: In function ‘_start’ :

/build/buildd/libc-2.9/csu/../sysdeps/i386/elf/start.S:115: undefined reference to ‘main’

collect2: ld returned 1 exit status

Solution:

just use a Makefile.

You need to build them using:

gcc -c helloFunc.c -o helloFunc.o

gcc -c myHello.c -o myHello.o

gcc myHello.o helloFunc.o -o hello

Advertisements

3 comments on “compilation error : undefined reference to ‘main’

  1. I don’t know If I said it already but …Great site…keep up the good work. 🙂 I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, 🙂

    A definite great read….

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s