Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Makefile with relative paths?

Tags:

linux

makefile

I have a problem with my Makefiles on Gentoo Linux.

Here is my folder hierarchy:

Development
 -> GLTools  
   -> include    
   -> src

->Triangle    
  ->triangle.cpp    
  ->Makefile

and my Makefile:

MAIN = triangle
SRCPATH = ./
SHAREDPATH = ../GLTools/src/
SHAREDINCPATH = ../GLTools/include/
LIBDIRS = -L/usr/local/lib
INCDIRS = -I/usr/include -I/usr/local/include -I/usr/include/GL \    
              -I$(SHAREDINCPATH)  -I$(SHAREDINCPATH)GL

CC = g++
CFLAGS = $(COMPILERFLAGS) -g $(INCDIRS)
LIBS = -lglut -lGL -lGLU -lm

prog : $(MAIN)

$(MAIN).o : $(SRCPATH)$(MAIN).cpp
glew.o    : $(SHAREDPATH)glew.c
GLTools.o    : $(SHAREDPATH)GLTools.cpp
GLBatch.o    : $(SHAREDPATH)GLBatch.cpp
GLTriangleBatch.o    : $(SHAREDPATH)GLTriangleBatch.cpp
GLShaderManager.o    : $(SHAREDPATH)GLShaderManager.cpp
math3d.o    : $(SHAREDPATH)math3d.cpp

$(MAIN) : $(MAIN).o glew.o
     $(CC) $(CFLAGS) -o $(SRCPATH)$(MAIN) $(LIBDIRS) $(SRCPATH)$(MAIN).cpp \
         $(SHAREDPATH)glew.c $(SHAREDPATH)GLTools.cpp $(SHAREDPATH)GLBatch.cpp\ 
         $(SHAREDPATH)GLTriangleBatch.cpp $(SHAREDPATH)GLShaderManager.cpp \
         $(SHAREDPATH)math3d.cpp $(LIBS)

clean:
    rm -f *.o

My problem is that get the following error:

demonking@Master ~/Development/Triangle $ make
g++    -c -o triangle.o triangle.cpp
triangle.cpp:4:50: error: GLTools.h: No such file or directory
triangle.cpp:5:56: error: GLShaderManager.h: No such file or directory

But when I copy my Makefile to the folder Development (a folder up one level) and edit my paths it compiles without any errors.

Why do I get an error when my triangle.cpp and Makefile are in one folder and I try to access GLTools in the parent folder?

like image 384
demonking Avatar asked Jul 31 '11 15:07

demonking


People also ask

How do I get the current relative directory of my makefile?

You can use shell function: current_dir = $(shell pwd) .

What does $() mean in makefile?

The $@ and $< are called automatic variables. The variable $@ represents the name of the target and $< represents the first prerequisite required to create the output file.

What is Abspath in makefile?

That's what abspath does. It creates an absolute path. That means it must be anchored at the root.

What is add prefix in makefile?

$(addprefix prefix , names …) The argument names is regarded as a series of names, separated by whitespace; prefix is used as a unit. The value of prefix is prepended to the front of each individual name and the resulting larger names are concatenated with single spaces between them.


2 Answers

The default rule for compiling c++ uses CXXFLAGS rather than CFLAGS, and you haven't set it so it does not include INCDIRS.

Add

CXXFLAGS = $(COMPILERFLAGS) -g $(INCDIRS)

and try again.

The way you would have spotted this yourself is by reading the output of make. Notice the first line in the output you exhibit:

g++    -c -o triangle.o triangle.cpp

No include flags. No -g. None of the stuff that you took all the trouble to set up.

like image 162
dmckee --- ex-moderator kitten Avatar answered Sep 22 '22 23:09

dmckee --- ex-moderator kitten


Try set flags for g++

CXXFLAGS = $(CFLAGS)
like image 25
Michał Šrajer Avatar answered Sep 24 '22 23:09

Michał Šrajer