Search code examples
c++compilationmakefileundefined-symbol

C++ Undefined symbols when using folder structure


I have am currently trying to understand OpenGL and found this tutorial. I have reached a point where I would like to make something out of what I've learned so I've tired to setup a small project folder with a (somewhat) homemade Makefile. The structure of the project is as following:

project_root:
 -- Makefile
 -- src/
       main.cpp
       imageloader.cpp
 -- obj/
       main.o (no compiletime error)
       imageloader.o (no compiletime error)
 -- include/
       imageloader.h
 -- textures/
       some texture files

The Makefile:

CC = g++

SRC_DIR = ./src
OBJ_DIR = ./obj
INC_DIR = ./include

INCLUDE_PATHS = -I/usr/local/include -I/opt/X11/include -I$(INC_DIR)
LIBRARY_PATHS = -L/usr/local/lib -I/opt/X11/lib
LINKER_FLAGS = -framework OpenGL -lGLUT
COMPILER_FLAGS = -w

SRC_FILES = main.cpp imageloader.cpp
HEADER_FILES = imageloader.h

HEADERS = $(HEADER_FILES:%.h=$(INC_DIR)/%.h)
SRC = $(SRC_FILES:%.cpp=$(SRC_DIR)/%.cpp)

OBJS = $(SRC_FILES:%.cpp=$(OBJ_DIR)/%.o)

EXEC = main

$(OBJ_DIR)/%.o: $(SRC)
        $(CC) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS) -c -o $@ $<

all: $(OBJS) $(HEADERS)
        $(CC) $(OBJS) $(INCLUDE_PATHS) $(LIBRARY_PATHS) $(COMPILER_FLAGS)         $(LINKER_FLAGS) -o $(EXEC)

run:
    ./main

print:
        @echo "C++ objcet files: "$(OBJS)
        @echo "C++ source files: "$(SRC)
        @echo "C++ header files: "$(HEADERS)

clean:
       rm -fv $(EXEC)
       rm -fv $(OBJS)

As I try to compile this I get the following output:

g++ -I/usr/local/include -I/opt/X11/include -I./include -L/usr/local/lib -I/opt/X11/lib -w -c -o obj/main.o src/main.cpp
g++ -I/usr/local/include -I/opt/X11/include -I./include -L/usr/local/lib -I/opt/X11/lib -w -c -o obj/imageloader.o src/main.cpp
g++ ./obj/main.o ./obj/imageloader.o -I/usr/local/include -I/opt/X11/include -I./include -L/usr/local/lib -I/opt/X11/lib -w -framework OpenGL -lGLUT -o main
Undefined symbols for architecture x86_64:
  "loadBMP(char const*)", referenced from:
      initRendering()     in main.o
      initRendering()     in imageloader.o
  "Image::~Image()", referenced from:
      initRendering()     in main.o
      initRendering()     in imageloader.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [all] Error 1

What bothers me is that the code is taken just from tutorial site with no modifications, and when i compile it all in the same folder (all .cpp and .h files in same place) it compiles just fine. I have a feeling that I've made an obvious mistake but I can't seem to figure out what causes the error?

Can someone point out the reason for why this doesn't compile?

Thanks in advance : )


Solution

  • You're compiling main.cpp twice:

    g++ -I/usr/local/include -I/opt/X11/include -I./include -L/usr/local/lib -I/opt/X11/lib -w -c -o obj/main.o src/main.cpp <--- main.cpp
    g++ -I/usr/local/include -I/opt/X11/include -I./include -L/usr/local/lib -I/opt/X11/lib -w -c -o obj/imageloader.o src/main.cpp <--- main.cpp, instead imageloader.cpp