A
A
Alexey Lesovsky2015-05-17 20:32:13
GNU Make
Alexey Lesovsky, 2015-05-17 20:32:13

Is it correct to write a Makefile?

Hello!
I have a utility that is assembled by the simplest call to gcc

gcc -I/usr/pgsql-9.4/include -lncurses -lpq -L/usr/pgsql-9.4/lib -o hello hello.c

To build, I wrote this Makefile
PROGRAM_NAME = hello
SOURCE = hello.c
PREFIX = /usr
LIBS = -lncurses -lpq -L/usr/pgsql-9.4/lib
INCLUDE = -I/usr/pgsql-9.4/include

.PHONY: all clean install

all: hello

hello: hello.c
        gcc $(INCLUDE) $(LIBS) -o $(PROGRAM_NAME) $(SOURCE)

clean:
        rm -f $(PROGRAM_NAME)

install:
        install $(PROGRAM_NAME) $(PREFIX)/bin

However, here I had a problem, the paths to -I and -L in the future, on different machines, will be guaranteed to differ from those specified now. The question is how do I properly write a Makefile that can use actual paths?
PS In general, Makefile is an intermediate task, and the ultimate goal is to build an rpm and deb package to install the tool.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Dergachev, 2015-05-21
@lesovsky

It is advisable to use automatic parameter generators.
For ncurses, consider ncurses-config (available in the Debian repositories, for example). Sample output for this utility on my system:

[email protected]:~$ ncurses5-config --cflags

[email protected]:~$ ncurses5-config --libs
-lncurses -ltinfo
[email protected]:~$ ncurses5-config --libdir
/usr/lib/x86_64-linux-gnu
[email protected]:~$ ncurses5-config --includedir
/usr/include
[email protected]:~$

The full list of commands can be viewed by calling the utility with the "--help" flag.
A similar utility for Postgre is called pg-config .
In your Makefile, you execute these utilities and substitute the result of their work in variables. Syntax:
PGLIBS = $(shell pg-config --libs)
NCURSESLIBS = $(shell ncurses5-config --libs)

More details here
To be able to specify paths from other users, use already accepted and established variable names, like LDFLAGS, for example (you can search for them on the Internet). This will prevent users from digging through your utility's documentation to add flags when calling make.
For the future, you can explore utilities like CMake, which have their own technologies for finding library paths and substituting them.
You can build the deb and rpm package using the checkinstall utility (it is not installed by default, but is available in the standard Debian repositories).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question