[OT] make and two output that use the same library differently compiled

Discussion in 'C Programming' started by pozz, Nov 8, 2013.

  1. pozz

    pozz Guest

    I'm sorry for the OT, but I don't know where to ask thos kind of
    questions. Anyway it's related to the compilation process of C source code.

    I have a library that can be compiled in two different ways depending on
    the definition of two symbols through #define.
    What I'm using is defining the symbol with -D option of the compiler.

    So, for the library compiled in MODE1:
    cc -DMODE1 mylib.c -o mylib.o
    and for the library compiled in MODE2:
    cc -DMODE2 mylib.c -o mylib.o

    I have a project that should generates two outputs (executables) with
    the library statically linked. The output out1 should link the library
    in MODE1, the second out2 should link the library in MODE2.

    If I issue "make out2" after "make out1" (or the contrary) I have a
    problem, because the second compilation doesn't recompile mylib.c,
    because it hasn't change since the creation time of mylib.o of the first
    compilation. So out2 is wrongly linked to mylib in MODE1.

    The only method to compile both outputs is:
    make clean
    make out1
    make clean
    make out2
    Of course, it's annoying.

    Is there a better approach?
     
    pozz, Nov 8, 2013
    #1
    1. Advertisements

  2. pozz

    Philip Lantz Guest


    I just did something like this a few weeks ago. The key is to generate
    the different built versions in separate object files:

    cc -DMODE1 mylib.c -o mylib1.o
    cc -DMODE2 mylib.c -o mylib2.o

    To achieve this, put something like this in your makefile:

    MODE ?= 1

    %$(MODE).o: %.c
    $(CC) $(CFLAGS) -DMODE$(MODE) -o [email protected] -c $<

    out$(MODE) : objs... mylib(MODE).o


    Then you just say make MODE=1 or make MODE=2.

    I'm sure you can make this more sophisticated, so you can simply do
    make out1 or make out2.
     
    Philip Lantz, Nov 8, 2013
    #2
    1. Advertisements

  3. pozz

    Noob Guest

    comp.unix.programmer is a good place to ask makefile questions.

    There's also the gmake mailing list.
     
    Noob, Nov 8, 2013
    #3
  4. pozz

    Jorgen Grahn Guest

    Nitpick: you talk about static libraries and linking, but only show
    compilation of one source file to an object file.
    This is a bad idea; you should try to find some other solution. It's
    hard to think of a "shape-shifting" library which is different things
    depending on the mode, and may have different semantics in the two
    cases ... and it gets worse if the include files are also
    shape-shifters.

    You don't say what the difference is, so it's hard to suggest
    alternatives. Still, there are some things you can do:
    Yes, that's one effect. Make doesn't know about modes, and treats
    mylib.a as /one/ thing.
    It defeats the purpose of make. You have to keep the dependencies and
    rules in your head, just like people did before make was invented in
    the 1970s.
    Try building the two distinct libraries in distinct directories, e.g.

    # assumes Gnu make, I think
    mode1/%.o: CPPFLAGS+=-DMODE1
    mode2/%.o: CPPFLAGS+=-DMODE2

    With this way of looking at it, it's just a coincidence that they are
    partly built from the same sources.

    /Jorgen

    PS. Didn't look at the other solution posted.
     
    Jorgen Grahn, Nov 9, 2013
    #4
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.