bigcodeszzer
bigcodeszzer

Reputation: 940

Modify Makefile to include a library and use C++11

I have the following makefile:

VER=cblas
GCC_VERSION = 7.2.0
PREFIX = /usr/local/gcc/${GCC_VERSION}/bin/
CC = ${PREFIX}gcc
CPP = ${PREFIX}g++

w2.${VER}: w2.${VER}.o
    $(CPP) -ow2.${VER} w2.${VER}.o

w2.${VER}.o: w2.${VER}.cpp
    $(CPP) -c -O2 -std=c++17 w2.${VER}.cpp 

clean:
    rm *.o

I need to modify this makefile such that it will include the gsl library. I was able compile directly with the commands:

g++ -Wall -I/home/path/gsl/include -c w2.cblas.cpp
g++ -L/home/path/gsl/lib w2.cblas.o -lgsl -lgslcblas -lm

How can I modify this makefile to include the gsl library (and c++11)?

Upvotes: 1

Views: 1176

Answers (1)

jfMR
jfMR

Reputation: 24738

Instead of defining your own rules (i.e.: remove the following):

w2.${VER}: w2.${VER}.o
    $(CPP) -ow2.${VER} w2.${VER}.o

w2.${VER}.o: w2.${VER}.cpp
    $(CPP) -c -O2 -std=c++17 w2.${VER}.cpp 

you can simply rely on the already-defined implicit rules. You just have to properly set up the variables these implicit rules work with. Therefore, in order to find the header files for the compilation:

CPPFLAGS := -I/home/path/gsl/include

For the optimization and the specification of the C++ standard:

CXXFLAGS := -O2 -std=c++11

Finally, for the linking:

LDFLAGS := -L/home/path/gsl/lib
LDLIBS := -lgsl -lgslcblas -lm
LD = $(CXX)

Note that those rules rely on the CXX variable for specifying the compiler and not CPP, so you don't want:

CPP = ${PREFIX}g++

but:

CXX = ${PREFIX}g++

By the way, CPP in make stands for C Pre-Processor (and not C++, that would be CXX).


Makefile

Putting everything explained above together, your makefile would then look like the following:

ER=cblas
GCC_VERSION = 7.2.0
PREFIX = /usr/local/gcc/${GCC_VERSION}/bin/

CC = ${PREFIX}gcc
CXX = ${PREFIX}g++

CPPFLAGS := -I/home/path/gsl/include
CXXFLAGS := -O2 -std=c++11

LDFLAGS := -L/home/path/gsl/lib
LDLIBS := -lgsl -lgslcblas -lm
LD = $(CXX)

clean:
    rm *.o

Note that clean is the only explicit rule in the makefile above.

Considering that you have source file called w2.cblas.cpp, then you should call make this way:

make w2.cblas

Upvotes: 4

Related Questions