views:

46

answers:

3

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 = g++

#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):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)
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 following dependecy error:

make: Circular mylib.a <- mylib.a dependency dropped.
ar rv (hello.o) hello.o
/bin/sh: -c: line 0: syntax error near unexpected token `('
/bin/sh: -c: line 0: `ar rv (hello.o) hello.o'
make: *** [(hello.o)] Error 2

Questions : How do I resolve this? Which command is causing the cyclic dependency?

+2  A: 

Not positive, but:

$(MYLIB):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)

If I remember my makefile syntax correctly, that line says $(MYLIB) depends on $(MYLIB)... Which of course evaluates to: mylib.a: mylib.a...

Bryan
And just to clarify, I think what you want is: `$(MYLIB): main.o hello.o here.o bye.o`
Bryan
@Bryan: I dont think that is a problem. It is a way to let the library know what object files it should contain.
Eternal Learner
@Eternal Learner: The makefile syntax is `target: dependencies`... If your goal is to compile hello.o, here.o, and bye.o into a single .a file, that is not the correct way to do it. All that line is doing is saying which files $(MYLIB) depends on, and right now, $(MYLIB) depends on $(MYLIB) which is recursive.
Bryan
I guess the real question comes down to, what are you trying to do on that line? If you're just trying to say "mylib.a depends on the .o files I used to compile the .a", well... The .a will change anyway when you recompile, so the file itself will be out of date. Either way, I'm pretty sure that's where your recursive error comes from.
Bryan
@Bryan: I made the change suggested by you. and I get the file not found error:`gcc -o myapp main.o mylib.agcc: mylib.a: No such file or directorymake: *** [myapp] Error 1`
Eternal Learner
@Eternal Learner: It seems like from other comments you're trying to compile those .o files into a .a, which doesn't exist before that point in time. If that's the case, check out http://www.adp-gmbh.ch/cpp/gcc/create_lib.html
Bryan
+2  A: 
#Local Libraries
MYLIB = mylib.a

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

$(MYLIB):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)

It looks like the last rule is

mylib.a: mylib.a (hello.o) mylib.a (here.o) mylib.a (bye.o)

Which is a circular dependency.

The line should be

mylib.a: hello.o here.o bye.o

Without the parentheses.

nategoose
@nategoose:I dont think that is a problem. It is a way to let the library know what object files it should contain.
Eternal Learner
mylib.a can never be up to older than itself. It should only be on the left of the `:` Plus see edit.
nategoose
@natgoose: I made the change suggested and I get the following error gcc -o myapp main.o mylib.agcc: mylib.a: No such file or directorymake: *** [myapp] Error 1
Eternal Learner
@Eternal Learner: But you no longer the the `make` error. Follow up the line above with the action `ar rcs $@ $^`. This will build your library. The `$@` symbol turns into the target name for the current rule and the `$^` turns into the files needed to build that target (`$@` is left of `:` and `$^` is everything right of `:`).
nategoose
@nategoose : Added the line and get an error: "Undefined Reference" Here is the complete message : ar rcs mylib.a hello.o here.o bye.ogcc -o myapp main.o mylib.amain.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 statusmake: *** [myapp] Error 1
Eternal Learner
@Eternal Learner: You've figured out the problem you were asking about. I think that further problems, if you can't figure them out, should go in a new questions. If nothing else they would be easier to read than putting them in comments.
nategoose
+1  A: 

Drop the extraneous $(MYLIB) in the dependency list. That is:

$(MYLIB):       $(MYLIB)(hello.o) $(MYLIB)(here.o) $(MYLIB)(bye.o)

should be:

$(MYLIB):       hello.o here.o bye.o
John Franklin
@John - I dont think that is a problem. It is a way to let the library know what object files it should contain.
Eternal Learner