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

P

pozz

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?
 
P

Philip Lantz

pozz said:
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?


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 $@ -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.
 
N

Noob

pozz said:
I'm sorry for the OT, but I don't know where to ask those
kind of questions.

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

There's also the gmake mailing list.
 
J

Jorgen Grahn

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

Nitpick: you talk about static libraries and linking, but only show
compilation of one source file to an object file.
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.

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:
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.

Yes, that's one effect. Make doesn't know about modes, and treats
mylib.a as /one/ thing.
The only method to compile both outputs is:
make clean
make out1
make clean
make out2
Of course, it's annoying.

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.
Is there a better approach?

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.
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top