0

I have to "release" some C++ code. Basically this code needs to be compiled a C++11 compiler and some external library (specifically the gmp,gsl,mpfr libraries).

So basically the makefile to be used has to be properly configured before to be used (because the actual structure depends on where the compiler is installed and where the other libraries are).

Is there a way to "preconfigure" the makefile before use the makefile with the make command?

user8469759
  • 2,522
  • 6
  • 26
  • 50

1 Answers1

0

Autoconf and automake are heavy duty tools that can do what you're describing very well, but there may be a simpler solution. To start, make sure you're using variables where appropriate:

%.o:%.c
    $(CC) $(CFLAGS) $^ -o $@ 

program: $(OBJS)
    $(LD) $(LDFLAGS) ...

And then populate the CC and LDFLAGS. You can require the user to override them on the command line as so:

make CC=/opt/toolchains/xxx/yyy/gcc

Notice that even if you define CC in your makefile, the value you specified on the command line will take precedence. Alternatively, you can do an editable configure file, and have lines like this in your makefile:

 CONFIG_FILE := .configure
 TOOLCHAIN_DIR := $(sed -n "s/^\s*TOOLCHAIN:\s*\(\S*\).*$$/\1/p" < $(CONFIG_FILE))
 LIB_DIRS := $(sed -n "s/^\s*LIBS:\s*\(\S*\).*$$/\1/p" < $(CONFIG_FILE))

 CC:=$(TOOLCHAIN_DIR)/gcc
 LDFLAGS += $(addprefix -L,$(LIB_DIRS))

And, on top of that you can create a configure rule in your makefile to generate the .configure file based on user input. If you need to go beyond that, you may want to consider autoconf.

John
  • 3,400
  • 3
  • 31
  • 47