1

I have multiple files in my project which are compiled to object files using either -fPIC or without this option. When compiling those files for the use in a shared library, this option is necessary, else not. Thus when I compile the project into a shared library, this option is necessary.
Nevertheless I never am sure if the last compilation was done into the shared library or not. If not, and I want to compile it into the shared library, the compilation fails, and I have to delete the generated object files. The recompilation of those object files takes quite a lot of time, which I would like to avoid.
Is there a way for the makefile to detect if the object files have been compiled with or without this option right at the beginning, so that either the compilation can continue, or both have to be recompiled, without generating either an error or spending a lot of time in an unnecessary recompilation loop?

arc_lupus
  • 3,942
  • 5
  • 45
  • 81
  • 1
    Possible duplicate of [How can I tell, with something like objdump, if an object file has been built with -fPIC?](https://stackoverflow.com/questions/1340402/how-can-i-tell-with-something-like-objdump-if-an-object-file-has-been-built-wi) – Karsten Koop Jan 08 '18 at 12:27
  • @KarstenKoop: How can I include f.ex. the first answer into my makefile? – arc_lupus Jan 08 '18 at 12:33
  • What is causing `-fPIC` to appear/disappear between builds? Normally this would be something you'd change in the Makefile itself, triggering a full rebuild. – Sneftel Jan 08 '18 at 12:40
  • @arc_lupus you could make a shell script that checks the object files for -fPIC and then calls your makefile with an argument indicating whether or not to recompile with -fPIC or not (https://stackoverflow.com/questions/2826029/passing-additional-variables-from-command-line-to-make). – dmedine Jan 08 '18 at 12:42
  • @Sneftel: It depends on which Makefile I am calling – arc_lupus Jan 08 '18 at 12:43
  • That's not a good organization. If you do have multiple makefiles, they should not produce output in the same place. – Sneftel Jan 08 '18 at 12:45
  • 2
    What I usually do to differentiate files built with certain options is to make the build output folder's name depend of those options. Like `build_fPIC` and `build_no_fPIC` per example. – Tim Jan 08 '18 at 12:48
  • 1
    similarly for release vs. debug builds, etc. etc. – Useless Jan 08 '18 at 12:50

2 Answers2

2

Q: "Is there a way for the makefile to detect if the object files have been compiled with or without this option"

Short answer: No

Long answer: if source files can be build with different options and you have to have access to the different builds at the same time, then you must have several output folders. And the makefiles must generate them in the correct folder. Say something like this for a simple debug/release ("-g" flag) :

|
-src
-include
-BUILD
  |
  -obj
  | -debug
  | -release
  -bin
    |-debug
    |-release

Of course, this approach has limitations. For example, if you need to have both "debug/release" and "PIC/not PIC", then you will need 4 output folders.

You can also mix my approach with the one proposed by @BasileStarynkevitch (generating specific names).

kebs
  • 6,387
  • 4
  • 41
  • 70
1

A possible approach could be to have your own convention about object files and their naming. For example, file foo.c would be compiled into foo.pic.o when it is position-independent code, and into foo.o when it is not. And you could adapt that idea to other cases (e.g. foo.dbg.o for a file with DWARF debug information, compiled with -g). You could also use subdirectories, e.g. compile foo.c into PICOBJ/foo.o or PICOBJ/foo.pic.o for a PIC object file, but this might be less convenient with make (beware of recursive makes).

Writing appropriate rules for your Makefile is then quite easy.

Be aware of other build automation systems, e.g. ninja.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547