views:

64

answers:

2

I have 4 .c files hello.c,here.c,bye.c and main.c. One header file mylib.h

The contents are as follows

hello.c

#include<stdio.h>

void hello()
{
    printf("Hello!\n");
}

here.c

#include<stdio.h>

void here()
{
     printf("I am here \n");
}

bye.c

#include<stdio.h>

void bye()
{
    printf("Bye,Bye");
}

main.c

#include<stdio.h>
#include "mylib.h"

int main()
{ 

  hello();
  here();
  bye();
  return 1;
}

mylib.h

#ifndef _mylib_
#define _mylib_

void hello();
void here();
void bye();

#endif

The makefile for creating a static lib is : Makefile

all:    myapp

#Macros

#Which Compiler
CC = gcc

#Where to install
INSTDIR = /usr/local/bin

#Where are include files kept
INCLUDE = .

#Options for developement
CFLAGS = -g -Wall -ansi

#Options for release
#CFLAGS = -O -Wall -ansi

#Local Libraries
MYLIB = mylib.a

myapp:  main.o $(MYLIB)
        $(CC) -o myapp main.o $(MYLIB)

$(MYLIB):       hello.o here.o bye.o
                ar rcs $@ $^

main.o:         main.c mylib.h
hello.o:        hello.c
here.o:         here.c
bye.o:          bye.c

clean:
    -rm main.o hello.o here.o bye.o $(MYLIB)

install:        myapp
    @if [ -d $(INSTDIR) ]; \
    then \
            cp myapp $(INSTDIR);\
            chmod a+x $(INSTDIR)/myapp;\
            chmod og-w $(INSTDIR)/myapp;\
            echo "Installed in $(INSTDIR)";\
    else \
            echo "Sorry, $(INSTDIR) does not exist";\
    fi

Problem: When I execute the command

make -f Makefile all 

I get the error: gcc -o myapp main.o mylib.a

main.o: In function `main':

/home/usr/molly/main.c:7: undefined reference to `hello()'

/home/usr/molly/main.c:8: undefined reference to `here()'

/home/usr/molly/main.c:9: undefined reference to `bye()'

main.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'

collect2: ld returned 1 exit status

make: *** [myapp] Error 1

Questions : How do I resolve this? Why is there an undefined reference

A: 

This works for me with the caveot that you are not specifying an 'all' target:

xxxx@xxxx-desktop:~/Desktop$ make -f Makefile
cc -g -Wall -ansi   -c -o main.o main.c
cc -g -Wall -ansi   -c -o hello.o hello.c
cc -g -Wall -ansi   -c -o here.o here.c
cc -g -Wall -ansi   -c -o bye.o bye.c
ar rcs mylib.a hello.o here.o bye.o
cc -o myapp main.o mylib.a
xxxx@xxxx-desktop:~/Desktop$ ./myapp 
Hello!
I am here 
Bye,Byexxxx@xxxx-desktop:~/Desktop$ 
agent0range
@agentOrange- Tried that dosent work
Eternal Learner
A: 

This actually works for me. Try rm mylib.a and then make

nategoose
Better, `make clean; make`. The quoted error message still suggests there are left over C++ files around, so let's make sure to get rid of all *.o and *.a files before trying again.
aschepler
@aschelper-+1 cleaning and then building works. How do we figure out this , by looking at which error message?
Eternal Learner
@aschelper: I didn't suggest `make clean; make` because there was no explicit rule in the Makefile posted for `clean`.@Eternal Learner: The error message suggested that the library file did not actually contain things that the Makefile suggested it should. I knew about your previous question, where the Makefile wouldn't properly build the library, so I thought maybe an improperly built library did get generated and is still around (which would keep `make` from realizing that it needed to be rebuilt.
nategoose