2

On OS X Mavericks i am trying to build a project using the following Makefile:

CC=g++
EXECUTABLE=minigi
SRC_DIR=src
INTERM_DIR=obj

INCLUDES=-I $(SRC_DIR) -I /usr/local/Cg/examples/OpenGL/glew/include/
LIBS=-L/usr/local/lang/NVIDIA_GPU_Computing_SDK/sdk/C/common/lib/linux/ -lpng -stdc++ -lGL -lGLU -lGLEW -lSDLmain -lSDL -lgomp
CFLAGS_COMMON=$(INCLUDES)
CFLAGS=$(CFLAGS_COMMON) -O3 -DNDEBUG -fopenmp
#CFLAGS=$(CFLAGS_COMMON) -g -O0 -D_DEBUG 

SOURCE_FILES=$(shell find $(SRC_DIR) -iname '*.cpp')
DEP_FILES=$(SOURCE_FILES:$(SRC_DIR)/%.cpp=./$(INTERM_DIR)/%.dep)
OBJ_FILES=$(SOURCE_FILES:$(SRC_DIR)/%.cpp=./$(INTERM_DIR)/%.o)

all: $(EXECUTABLE)

clean:
    rm -rf obj $(EXECUTABLE)

.PHONY: clean all

.SUFFIXES:
.SUFFIXES:.o .dep .cpp .h

$(INTERM_DIR)/%.dep: $(SRC_DIR)/%.cpp
    mkdir -p `dirname $@`
    printf `dirname $@`/ > $@
    $(CC) $(CFLAGS_COMMON) $< -MM | sed -r -e 's,^(.*)\.o\s*\:,\1.o $@ :,g' >> $@

ifneq ($(MAKECMDGOALS),clean)
-include $(DEP_FILES)
endif

$(INTERM_DIR)/%.o: $(SRC_DIR)/%.cpp
    mkdir -p $(INTERM_DIR)
    $(CC) $(CFLAGS) -c $< -o $@

$(EXECUTABLE): $(OBJ_FILES)
    $(CC) $^ $(LIBS) -o $@

However, when I type make I get the following error:

obj/app/sdl_gl_appliacation.dep:1: *** missing separator.  Stop.

The file obj/app/sdl_gl_application.dep looks as follows:

-n  obj/app/

As I know very little about makefiles (and did not write the posted one) every help would be appreciated.

P.S. I modified the line printf dirname $@/ > $@ . In the original file there was an echo -n but that is not working on OS X.

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Heribert
  • 613
  • 1
  • 9
  • 27
  • Well, files that are `include`d into a `makefile` need to be in `makefile` syntax, which your `.dep` file most certainly seems to not be. There's no way we can know what it's supposed to look like, though... – twalberg May 15 '14 at 19:43
  • try running `make clean` to clean up broken .dep files... – Chris Dodd May 15 '14 at 23:37

1 Answers1

3

Well, let's clean this up a little bit.

The way the dependencies are handled really is ugly, GCC can do it for you automatically.

EXECUTABLE  :=  minigi

SRC_DIR     :=  src
OBJ_DIR     :=  obj

SRC_FILES   :=  $(wildcard $(SRC_DIR)/*.cpp)
OBJ_FILES   :=  $(SRC_FILES:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)
DEP_FILES   :=  $(OBJ_FILES:.o=.d)

LDLIBS      :=  -lpng -lstdc++ -lGL -lGLU -lGLEW -lSDLmain -lSDL -lgomp
LDFLAGS     :=  -L/usr/local/lang/NVIDIA_GPU_Computing_SDK/sdk/C/common/lib/linux/

CPPFLAGS    :=  -MMD -MP -DNDEBUG -fopenmp -I $(SRC_DIR) -I /usr/local/Cg/examples/OpenGL/glew/include/
CXXFLAGS    :=  -O3

.PHONY: all clean

all:    $(EXECUTABLE)

clean:
    $(RM) -r $(OBJ_DIR) $(EXECUTABLE)

$(EXECUTABLE):  $(OBJ_FILES)
    $(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@

$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp | $(OBJ_DIR)
    $(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ -c $<

$(OBJ_DIR):
    mkdir $@

ifeq "$(MAKECMDGOALS)" ""
-include $(DEP_FILES)
endif

Some quick notes :

  • You're using C++, so you should use $(CXX) instead of $(CC) which is used for C code.

  • Along with this, you should use $(CXXFLAGS) instead of $(CFLAGS).

  • $(CPPFLAGS) is meant for preprocessor flags (-I, -D, or -fopenmp which is a compile-time flag).

  • -MMD -MP preprocessor flags will auto-generate dependency files when compiling. Learn more.

  • $(LDFLAGS) is meant for linker flags such as -L flags.

  • $(LDLIBS) is meant for linker libs such as -l flags

  • Avoid using the $(shell ) function since it will be executed every time the variable is expanded when assigned with the = operator instead of the := operator. $(wildcard ) is more suited for the job of listing files.

Chnossos
  • 9,971
  • 4
  • 28
  • 40
  • First of all thanks a lot for the answer and detailed explanation. It helped understanding the file a lot. If I now try to build I get `fatal error: 'omp.h' file not found'`. I found this [link](http://stackoverflow.com/questions/20321988/error-enabling-openmp-ld-library-not-found-for-lgomp-and-clang-errors) which should be the problem. I installed gcc using brew and added `CC := gcc-4.8` to the makefile. However, the same error as before occurs upon building the project. – Heribert May 16 '14 at 06:23
  • As said above in my answer, you need to use the `CXX` variable, not the `CC` one. Also, since your project is using C++, you need to use `g++`, not `gcc`. To let me help you more easily, you should edit you question, and add several informations such as your OS/distribution, where you took your Makefile in the first place, and add the whole output when you use `make`. – Chnossos May 16 '14 at 17:55
  • Again you were right :) Replacing CC by CXX and gcc-4.8 by g++-4.8 did the trick. Then I only had the problem that GL/gl.h was not found. However, as this [link](http://stackoverflow.com/questions/10866331/freeglut-on-mac-os-x-lion) explained a simple replacement of GL by OpenGL was needed on Mac. Now except for some compile errors the project builds and therefore I consider this make problem as solved. Thanks again for your help :) – Heribert May 17 '14 at 08:56