LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-13-2018, 08:42 AM   #1
Gordonnnn
LQ Newbie
 
Registered: Apr 2018
Posts: 2

Rep: Reputation: Disabled
Makefile rules question


I have a Makefile that I am able to compile a project with, but only with the workaround where only some source files are compiled into object files.

In the Makefile below I have to split the source files into two variables and build with all of them, but only output object files from the two source files from the first variable. the second list of source files point to a library in another directory.

If I try to compile all the sources into object files I get an error:
make: *** No rule to make target 'imgui.o', needed by 'ok.exe'. Stop.

I'm wondering why I can't compile all the sources into object files, I know it likely has to do with the sources from the library being in a different directory, but I don't understand what is exactly wrong that I would need to change in the Makefile
This is the file:
Code:
# imgui example
target := ok.exe

src := main.cpp imgui_impl_glfw_gl3.cpp 
src2 := D:/Libraries/imgui/imgui.cpp D:/Libraries/imgui/imgui_demo.cpp D:/Libraries/imgui/imgui_draw.cpp D:/Libraries/gl3w/src/gl3w.c

objects := $(addsuffix .o, $(basename $(notdir $(src))))

cxx := g++
flags = -Wall

includes = -ID:/Libraries/imgui -ID:/Libraries/gl3w/include -ID:/Libraries/glfw/include 

libs = -LD:/Libraries/glfw/lib-mingw
libs += -lglfw3 -lgdi32 -lopengl32 -limm32

%.o: %.cpp
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@

all: $(target)

$(target): $(objects)
	$(cxx) $(includes) $(flags) $(src) $(src2) -o $(target) $(libs)

clean:
	rm -f $(objects) $(target)

print:
	@echo $(objects)
Any help is much appreciated. Thanks!
 
Old 04-13-2018, 09:22 AM   #2
BW-userx
LQ Guru
 
Registered: Sep 2013
Location: MID-SOUTH USA
Distribution: Slackware 14.2 / Slackware 14.2 current / Manjaro
Posts: 6,402

Rep: Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239Reputation: 1239
I've never seen on written like that before. Your usages of
Code:
:=
D:
are you stuck on pascal syntax, and Windows?
Code:
target := ok.exe
There are no EXE files in Unix, Linux..

example of a make file,
Code:
CC = gcc
OS = -DLINUX

VERSION = 1.0.2

LIBDIR = -L/usr/X11R6/lib
LIBDIR = -L/usr/X11R6/lib
INCDIR = -I/usr/X11R6/share/include

PREFIX = /usr
BINDIR = $(PREFIX)/bin
DOCDIR = $(PREFIX)/doc/wmcube-$(VERSION)
MANDIR = $(PREFIX)/man/man1
OBJDIR = $(PREFIX)/share/wmcube

# Edit OSLIBS as appropriate to include OS specific libraries.

OSLIBS =
LIBS   = -lXpm -lXext -lX11 -lm $(OSLIBS)
CFLAGS = -Wall -O2

OBJS =	wmcube.o \
	../wmgeneral/wmgeneral.o \
	../wmgeneral/misc.o \
	../wmgeneral/list.o

.c.o:
	$(CC) $(OS) $(CPPFLAGS) $(CFLAGS) $(INCDIR) -c $< -o $*.o

wmcube: $(OBJS)
	$(CC) $(LDFLAGS) -o wmcube $^ -lXext $(LIBDIR) $(LIBS)

all:: wmcube

clean::
	for i in $(OBJS) ; do \
		rm -f $$i ; \
	done
	rm -f wmcube
	rm -f *~

install:: wmcube
	cp -f wmcube $(BINDIR)
	chmod 755 $(BINDIR)/wmcube
	chown root:root $(BINDIR)/wmcube
	mkdir -p $(DOCDIR)
	cp -p ../CHANGES ../COPYING ../INSTALL ../README ../THANKS $(DOCDIR)
	cp -pr ../3D-objects $(DOCDIR)
	cp -p wmcube.1 $(MANDIR)
	mkdir -p $(OBJDIR)
	cp -p ../3D-objects/* $(OBJDIR)
	@echo "wmcube installation finished..."

uninstall::
	rm -f $(BINDIR)/wmcube
	rm -f $(DOCDIR)/3D-objects/*
	if [ -d $(DOCDIR)/3D-objects ]; then rmdir $(DOCDIR)/3D-objects; fi
	rm -f $(DOCDIR)/*
	if [ -d $(DOCDIR) ]; then rmdir $(DOCDIR); fi
	rm -f $(MANDIR)/wmcube.1
	rm -f $(OBJDIR)/*
	if [ -d $(OBJDIR) ]; then rmdir $(OBJDIR); fi
notice how the LIBS and INCLUDES are written, and everything else about it, in how it is set up. It includes an uninstall which is nice for the user installing the program.

Last edited by BW-userx; 04-13-2018 at 09:28 AM.
 
Old 04-13-2018, 01:17 PM   #3
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 3,505

Rep: Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080Reputation: 1080
Another possible approach:
Code:
CXXMODS := main imgui_impl_glfw_gl3 \
	D:/Libraries/imgui/imgui \
	D:/Libraries/imgui/imgui_demo \
	D:/Libraries/imgui/imgui_draw

CMODS   := D:/Libraries/gl3w/src/gl3w

SOURCES := $(foreach m,${CXXMODS},$m.cpp) \
	   $(foreach m,${CMODS},$m.c)

OBJECTS := $(foreach m,${CXXMODS} ${CMODS},$m.o)

test:
	@echo Sources: ${SOURCES}
	@echo Objects: ${OBJECTS}
 
Old 04-13-2018, 08:55 PM   #4
Gordonnnn
LQ Newbie
 
Registered: Apr 2018
Posts: 2

Original Poster
Rep: Reputation: Disabled
I fixed the problem: I had to do with not specifying the other directories to generate all the object files.

Since when linking the executable, the list of objects referred just to the project directory, but the secondary sources where in other directories, I had to add steps to compile those separately.

So this:
Code:
%.o: %.cpp
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@
Became this:
Code:
# compile object files:
%.o: %.cpp
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@
# 2nd source dir
%.o: $(srcdir_2)/%.cpp
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@
# 3rd
%.o: $(srcdir_3)/%.c
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@
I'm not sure if there is a better way to state the rule so I have a list of object files with the directories not stripped to compile with, and one with the directories stripped for the output, so I don't repeat those lines, but it does work. @BW-userx I'm actually using MinGW, which is why I have the exe

This is the whole file: I also wan't just linking objects in the final step, but recompiling, so that step is changed.
Code:
# imgui example
target = ok.exe

srcdir_2 = D:/Libraries/imgui
srcdir_3 = D:/Libraries/gl3w/src

src := main.cpp imgui_impl_glfw_gl3.cpp 
src += $(srcdir_2)/imgui.cpp $(srcdir_2)/imgui_demo.cpp $(srcdir_2)/imgui_draw.cpp 
src += $(srcdir_3)/gl3w.c

cxx := g++
flags = -Wall

includes = -ID:/Libraries/imgui -ID:/Libraries/gl3w/include -ID:/Libraries/glfw/include 

libs = -LD:/Libraries/glfw/lib-mingw
libs += -lglfw3 -lgdi32 -lopengl32 -limm32

objects = $(addsuffix .o, $(basename $(notdir $(src))))

# compile objects
%.o: %.cpp
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@

%.o: $(srcdir_2)/%.cpp
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@

%.o: $(srcdir_3)/%.c
	$(cxx) $(includes) $(flags) -c $< -o $@ $(libs)
	@echo $@

all: $(target)

# build exe
$(target): $(objects)
	$(cxx) $(flags) $^ -o $@ $(libs)

clean:
	rm -f $(objects) $(target)

cleanexe:
	rm -f $(target)

print:
	@echo $(objects)

Last edited by Gordonnnn; 04-13-2018 at 09:20 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] How do i write to /etc/udev/rules.d/ from with in a makefile? mechsteel Programming 5 07-25-2014 01:36 PM
Removing option from rules in makefile RichardS Programming 1 08-18-2007 08:07 AM
about makefile using implicit rules lswhbcb Programming 8 05-19-2007 10:36 PM
Makefile.rules not found during kernel compile anilbh Fedora 1 02-21-2006 09:27 AM
Help in understanding Makefile rules cranium2004 Programming 2 03-11-2005 06:06 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:59 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration