Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

[SOLVED]Makefile - srcs and build in different paths ?

Programming languages, Coding, Executables, Package Creation, and Scripting.
Post Reply
Message
Author
giorgik
Posts: 29
Joined: 2017-05-01 18:32

[SOLVED]Makefile - srcs and build in different paths ?

#1 Post by giorgik »

I need to create a Makefile. I have a test project called Funzioni and it is structured as follows:
Funzioni: Makefile
|
---- Build: calcolo, esempio.o, main.o
|
---- src: esempio.c, main.c
|
---- include: esempio.h
I don't know how to tell the Makefile to put the object files example.o and main.o and the compute executable in the build directory and to read the sources placed in src. The Makefile I wrote is:

Code: Select all

INC_PATH = -I./include -I/usr/local/include
LIB_PATH = -L./lib
LIBS = 
CC = gcc
CFLAGS = -Wall -g -O2 -D_DEBUG
SOURCES = $(wildcard src/*.c)
OBJECTS = $(patsubst %.c, %.o, $(SOURCES))

vpath %.c src
vpath %.o build

EXECUTABLE = calcolo

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LIB_PATH) $(LIBS)

%.o: %.c
	$(CC) $(CFLAGS) -c $(INC_PATH) $< $(LIB_PATH) $(LIBS)
Can you tell me what changes I have to make to achieve the purpose illustrated at the beginning ?
Last edited by giorgik on 2021-03-14 19:20, edited 1 time in total.

LE_746F6D617A7A69
Posts: 932
Joined: 2020-05-03 14:16
Has thanked: 7 times
Been thanked: 65 times

Re: Makefile - srcs and build in different paths: how to do

#2 Post by LE_746F6D617A7A69 »

giorgik wrote:I don't know how to tell the Makefile to put the object files example.o and main.o and the compute executable in the build directory and to read the sources placed in src (...)
Can you tell me what changes I have to make to achieve the purpose illustrated at the beginning ?
... and that's the very first problem with make, and the very first reason for *NOT* using make ;)

You should use autoconf and automake for Your project(s). Make scripts are problematic and broken in several ways, one of them is already visible in Your example Makefile: static paths.
With Autoconf + Automake (Autotools) You don't have to care about object files, because they are handled automagically ;)
Besides, using autotools makes Your project portable (it's just sufficient to stick to a few very simple rules)

Regards
Bill Gates: "(...) In my case, I went to the garbage cans at the Computer Science Center and I fished out listings of their operating system."
The_full_story and Nothing_have_changed

giorgik
Posts: 29
Joined: 2017-05-01 18:32

Re: Makefile - srcs and build in different paths: how to do

#3 Post by giorgik »

so no other solution than the one you tell me ...

reinob
Posts: 1189
Joined: 2014-06-30 11:42
Has thanked: 97 times
Been thanked: 47 times

Re: Makefile - srcs and build in different paths: how to do

#4 Post by reinob »

giorgik wrote:I need to create a Makefile. I have a test project called Funzioni and it is structured as follows:
Funzioni: Makefile
|
---- Build: calcolo, esempio.o, main.o
|
---- src: esempio.c, main.c
|
---- include: esempio.h
I don't know how to tell the Makefile to put the object files example.o and main.o and the compute executable in the build directory and to read the sources placed in src. The Makefile I wrote is:

Code: Select all

INC_PATH = -I./include -I/usr/local/include
LIB_PATH = -L./lib
LIBS = 
CC = gcc
CFLAGS = -Wall -g -O2 -D_DEBUG
SOURCES = $(wildcard src/*.c)
OBJECTS = $(patsubst %.c, %.o, $(SOURCES))

vpath %.c src
vpath %.o build

EXECUTABLE = calcolo

all: $(EXECUTABLE)

$(EXECUTABLE): $(OBJECTS)
	$(CC) $(CFLAGS) -o $@ $(OBJECTS) $(LIB_PATH) $(LIBS)

%.o: %.c
	$(CC) $(CFLAGS) -c $(INC_PATH) $< $(LIB_PATH) $(LIBS)
Can you tell me what changes I have to make to achieve the purpose illustrated at the beginning ?
I think your %.o: %.c rule needs to include an explicit "-o $@", as otherwise the object files will be left in the current directory, rather than on the defined VPATH for .o files.

So you'd need:

Code: Select all

%.o: %.c
	$(CC) $(CFLAGS) -c $(INC_PATH) $< -o $@
(I also removed the $LIBS and $LIB_PATH since you don't need those when building object files).

There may be other things you need. I think you should start with the basics (like not mixing executables with objects, etc.) and maybe reading https://www.gnu.org/prep/standards/html ... asics.html will certainly help.

My make-fu is very rusty, so there may be other things that need to be fixed.

giorgik
Posts: 29
Joined: 2017-05-01 18:32

Re: Makefile - srcs and build in different paths: how to do

#5 Post by giorgik »

Thanks reinob, for the reply. Then I also found the GnuMake document and reading it well I found the solution you posted to me.

Post Reply