arc_lupus
arc_lupus

Reputation: 4114

Create Makefile to be used in different environments for C++

I would like to compile and run my program in two different environments. The libraries in both environments are installed on slightly different places, resulting in different makefile-lines:
In makefile A:

CXXFLAGS=-I$(DIR) -flto -fopenmp -O3 -g -march=native -std=gnu++17 -c -I/opt/interp2d/include -std=c++17 -I/opt/splinter/include -I/usr/include/eigen3

In makefile B:

CXXFLAGS=-I$(DIR) -nostindc++ -I~/local_opt/eigen/include/eigen3/ -I~/local_opt/boost/include -I~/local_opt/armadillo/include -flto -fopenmp -O3 -g -march=native -std=gnu++17 -c -I~/local_opt/interp2d/include -std=c++17 -I~/local_opt/splinterp/include -I/usr/include/eigen3

My problem now is that I am developing the program on the first machine, using makefile A, but also deploying it on the second machine. The deployment is done using git.
Every time I do a git pull on the second machine, I have to fix all the paths in the makefile in order to compile the program properly. Nevertheless I still would like to include the makefile in the git repository in order to keep both makefiles at the same level regarding compiling flags and linked libraries.
Thus, is there an easier way to still sync the makefile via git, while using different paths for the libraries and includes?

Upvotes: 1

Views: 609

Answers (3)

HardcoreHenry
HardcoreHenry

Reputation: 6377

I like this answer, however, I thought I'd mention this for completeness: If you have a lot of different hosts you can do something to the effect of:

include HostConfig_$(HOST).mk

And then create HostConfig_A.mk and HostConfig_B.mk which set host specific flags (Be it directories, etc). This is useful if you are managing a large project with lots of different host-specific variables.

As well, (for smaller projects), you could do something to the effect of:

CXX_INCLUDES_A = ...
CXX_INCLUDES_B = ...

CXX_FLAGS := -I$(DIR) -flto -fopenmp -O3 -g -march=native -std=gnu++17
CXX_FLAGS += $(CXX_INCLUDES_$(HOST))

Upvotes: 2

Goswin von Brederlow
Goswin von Brederlow

Reputation: 12322

The traditional answer to this problem is a configure script (see automake, autoconf for widely used framework). After checking out the source you run ./configure --with-eigen=~/local_opt/eigen/include/eigen3/ and it will adjust your Makefiles accordingly (usually generates Makefile from Makefile.in and only Makefile.in is in git).

Note: Properly done you only need to run configure on the first checkout, not on updates. make can generate Makefile again automatially as needed.

Upvotes: 1

jfMR
jfMR

Reputation: 24738

I think you could solve your problem by conditionally setting the variable CXXFLAGS in a common file (e.g.: config.mk) and by including that file in your makefiles.

The value used for setting the CXXFLAGS variable could, for example, depend on the value of the environment variable HOST:

ifeq ($(HOST),A)
   CXXFLAGS = ... # for machine A
else # B
   CXXFLAGS = ... # for machine B
endif

Then, include this config.mk makefile in both makefileA and makefileB:

include config.mk

Upvotes: 2

Related Questions