Professional Web Applications Themes

Makefile and lib dependencies - UNIX Programming

Hi, I have a project that depends on other projets. So I have a Makefile that calls Makefile of sub-projects to build the libs if needed. Once libs are built, the main Makefile link everything and make the executable. For dependencies of .h files, I solved the problem, but I still have a problem for dependencies of libs. For example if I change nothing in the subprojects, when I run the makefile, it calls all makefiles of subprojects and does nothing (that is totally normal) but it makes the link even when nothing has changed. Here is a part of ...

  1. #1

    Default Makefile and lib dependencies

    Hi,

    I have a project that depends on other projets. So I have a Makefile
    that calls Makefile of sub-projects to build the libs if needed.
    Once libs are built, the main Makefile link everything and make the
    executable.
    For dependencies of .h files, I solved the problem, but I still have a
    problem for dependencies of libs. For example if I change nothing in
    the subprojects, when I run the makefile, it calls all makefiles of
    subprojects and does nothing (that is totally normal) but it makes the
    link even when nothing has changed.

    Here is a part of the makefile :

    DBG=-g -ggdb -D_DEBUG
    OPTIONS=-Wall -pthread

    OUTDIR=Debug
    LIBDIR=Debug
    OUTFILE=libFoo
    CFG_INC=-I../../../ -I../../ -I../../lixbuild/
    CFG_LIB=../../lib2/$(LIBDIR)/lib2.a \
    ../../boost/boost_thread/$(LIBDIR)/libboost_thread.a \
    ../../boost/boost_re/$(LIBDIR)/libboost_re.a \
    ../../boost/boost_fs/$(LIBDIR)/libboost_fs.a
    CFG_OBJ=
    COMMON_OBJ=$(OUTDIR)/Processor.o $(OUTDIR)/RequestStack.o (and more
    objects...)
    OBJ=$(COMMON_OBJ) $(CFG_OBJ)
    DEPS=$(COMMON_OBJ:.o=.d)

    COMPILE=g++ -c -MMD $(OPTIONS) $(DBG) $(OPTIM) -o $ $(CFG_INC) $<

    LINK_SHARED=g++ $(DBG) $(OPTIM) -Wall -shared
    -Wl,-soname,./$(OUTFILE).so -o $(OUTDIR)/$(OUTFILE).so $(OBJ)
    $(CFG_LIB) -lpthread
    LINK_STATIC=ar -rs $(OUTDIR)/$(OUTFILE).a $(OBJ) $(CFG_LIB)

    # Pattern rules
    $(OUTDIR)/%.o : ../%.cpp
    $(COMPILE)

    # Build rules
    all: deps $(OUTFILE)

    # Build dependencies
    deps:
    (cd ../../boost/boost_fs/;make -f boost_fs.mak CFG=$(CFG))
    (cd ../../boost/boost_re/;make -f boost_re.mak CFG=$(CFG))
    (cd ../../boost/boost_thread/;make -f boost_thread.mak CFG=$(CFG))
    (cd ../../lib2/;make -f lib2.mak CFG=$(CFG))

    $(OUTFILE): $(OUTDIR) $(OBJ)
    $(LINK_SHARED)
    $(LINK_STATIC)

    $(OUTDIR):
    mkdir -p "$(OUTDIR)"


    Is there a mistake in my makefile, I can't see what's wrong.

    Thanks,

    Gérald
    G?rald Guest

  2. #2

    Default Re: Makefile and lib dependencies

    %% fr (G?rald) writes:

    g> For example if I change nothing in the subprojects, when I run the
    g> makefile, it calls all makefiles of subprojects and does nothing
    g> (that is totally normal) but it makes the link even when nothing
    g> has changed.

    g> # Build rules
    g> all: deps $(OUTFILE)

    Relying on the order of rules in a prerequisites list is not a good idea
    if you ever want to handle parallel or distributed builds. You should
    always declare all your dependencies explicitly, not make them implicit
    through the order of the prerequisites.

    g> # Build dependencies
    g> deps:
    g> (cd ../../boost/boost_fs/;make -f boost_fs.mak CFG=$(CFG))
    g> (cd ../../boost/boost_re/;make -f boost_re.mak CFG=$(CFG))
    g> (cd ../../boost/boost_thread/;make -f boost_thread.mak CFG=$(CFG))
    g> (cd ../../lib2/;make -f lib2.mak CFG=$(CFG))

    This is not good. First, putting these into parens () is not useful;
    every line in a command script is invoked in a separate shell
    automatically; you don't need (). Also, you should never use "make"
    when invoking make recursively. Always use $(MAKE) instead.

    g> $(OUTFILE): $(OUTDIR) $(OBJ)
    g> $(LINK_SHARED)
    g> $(LINK_STATIC)

    This is your problem. You never (except in very weird situations) want
    a target to depend on a directory.

    The modification time of a directory (in UNIX, anyway) is updated every
    time a file is added, removed, or renamed in that directory. If you say
    that your $(OUTFILE) depends on the directory then it will get rebuilt
    whenever any change happens in that directory, which is normally not
    what you want.

    In GNU make you can use $(shell ...) to create directories: this is
    discussed in the GNU make manual.


    You should have something like:

    $(shell [ -d $(OUTDIR) ] || mkdir -p $(OUTDIR))

    all: $(OUTFILE)

    $(OUTFILE): $(CFG_LIB) $(OBJ)
    <...>

    ../../lib2/$(LIBDIR)/lib2.a:
    cd ../../lib2 && $(MAKE) -f lib2.mak CFG=$(CFG)
    ../../boost/boost_thread/$(LIBDIR)/libboost_thread.a:
    cd ../../boost/boost_thread/ && $(MAKE) -f boost_thread.mak CFG=$(CFG)

    etc.

    --
    -------------------------------------------------------------------------------
    Paul D. Smith <org> Find some GNU make tips at:
    http://www.gnu.org http://make.paulandlesley.org
    "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
    Paul Guest

  3. #3

    Default Re: Makefile and lib dependencies

    Paul D. Smith a écrit :
     

    and $(MAKE) is defined by default, or do I have to add MAKE=make or
    smthg like that?
     

    Ahhh ok ok.. Thanks.
     

    I'll have a look.
     

    Ok, it seems to be a better syntax. I try that and I tell you.

    Thanks!

    --
    GéraLd : http://gerald.fauvelle.free.fr
    | Photos : http://www.gg.free.fr
    | myStats : Système de statistiques
    | Version 1.0.8 - http://emcity.nexenservices.com/mystats
    Gg Guest

  4. #4

    Default Re: Makefile and lib dependencies

    "Paul D. Smith" <org> wrote in message news:<engeast.baynetworks.com>...
     

    Ok, this line works, but the following doesn't
     

    g++: ../../lib2/Debug/lib2.a: No such file or directory

    It seems that it doesn't know about the rule for the libs.
    Is there smthg I missed?

    Thanks

    Gérald
    G?rald Guest

  5. #5

    Default Re: Makefile and lib dependencies

    %% fr (G?rald) writes:
     [/ref]

    g> g++: ../../lib2/Debug/lib2.a: No such file or directory

    I'm assuming, of course, that you kept all the variable settings,
    etc. from before.

    Did the build ever try to go to the other directory and build anything?

    You should use "make -p" and examine the ruleset that make knows about,
    and you can use "make -d" to get some debugging info (but there's a lot
    of it... redirect it to a file or something).

    g> It seems that it doesn't know about the rule for the libs.
    g> Is there smthg I missed?

    Without seeing your actual makefile and more context about what make
    really did, there's little else I can say.

    I should point out that there's a problem with the above makefile as
    written: if the library exists then a remake won't be attempted so make
    won't ever discover if it's out of date. I think you can get around
    this using the FORCE trick (see the GNU make manual), but don't use
    ..PHONY or those targets will always be considered out of date.

    --
    -------------------------------------------------------------------------------
    Paul D. Smith <org> Find some GNU make tips at:
    http://www.gnu.org http://make.paulandlesley.org
    "Please remain calm...I may be mad, but I am a professional." --Mad Scientist
    Paul Guest

  6. #6

    Default Re: Makefile and lib dependencies

    Paul D. Smith a écrit :
     [/ref]
    >
    > g> g++: ../../lib2/Debug/lib2.a: No such file or directory
    >
    > I'm assuming, of course, that you kept all the variable settings,
    > etc. from before.
    >
    > Did the build ever try to go to the other directory and build anything?[/ref]

    Actually I had forgotten the $(CFG_LIB), now it works.
     

    Yes, I've seen that today.
     

    Well, I used the method I told first, with calling make on each
    sub-projects, and with removing the $(OUTDIR) from $(OUTFILE) rule, it
    seems to work, that means when nothing has changed, it doesn't build
    anything.

    I'll see that FORCE trick to see if I can have a simpler makefile.

    Thank you for your help.

    --
    GéraLd : http://gerald.fauvelle.free.fr
    | Photos : http://www.gg.free.fr
    | myStats : Système de statistiques
    | Version 1.0.8 - http://emcity.nexenservices.com/mystats
    Gg Guest

Similar Threads

  1. [RFC] Devel::Dependencies
    By Jean-Louis Leroy in forum PERL Modules
    Replies: 4
    Last Post: January 17th, 04:48 PM
  2. Using Yaromat for Dependencies
    By Bohak1 in forum Macromedia Exchange Dreamweaver Extensions
    Replies: 4
    Last Post: July 13th, 07:21 PM
  3. Libcurl dependencies
    By Eric in forum Mac Programming
    Replies: 13
    Last Post: December 12th, 02:13 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139