0

I'm trying to create a Makefile for my C++ project, which has the following structure:

root
├── include/
|   └──external
|       └── stb_image.h
│   └── all .h files here
├── src/
|   └── main.cpp
│   └── all .cpp files here
└── Makefile

To compile this project, I'm trying to use the Makefile proposed in this answer, which is for gcc, but I have added CC:=g++ so it should work (I think):

SRC_DIR := src
OBJ_DIR := obj
BIN_DIR := bin

EXE := $(BIN_DIR)/color
SRC := $(wildcard $(SRC_DIR)/*.cpp)
OBJ := $(SRC:$(SRC_DIR)/%.cpp=$(OBJ_DIR)/%.o)

CPPFLAGS := -Iinclude -MMD -MP
CFLAGS   := -g -std=c++2a -Wall
LDFLAGS  := -Llib
LDLIBS   := -lpthread
CC := g++

.PHONY: all clean

all: $(EXE)

$(EXE): $(OBJ) | $(BIN_DIR)
    $(CC) $(LDFLAGS) $^ $(LDLIBS) -o $@

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

$(BIN_DIR) $(OBJ_DIR):
    mkdir -p $@

clean:
    @$(RM) -rv $(BIN_DIR) $(OBJ_DIR)

-include $(OBJ:.o=.d)

But when running make I obtain a very long error which I do not understand. I do not know if this error comes from having main.cpp without a main.h.

I need this makefile because I want to separate the declarations and the implementations of my project. Before doing that, I had everything done in headers files and I could compile my project with the following command:

g++ -g -std=c++2a -Wall -Isrc/include -o bin/color.exe  src/main.cc -lpthread

Any idea what am I doing wrong? I still do not know a lot about Makefiles, so maybe I'm doing something weird.

Antonio Gamiz Delgado
  • 1,871
  • 1
  • 12
  • 33
  • 1
    It won't make any difference to your errors but just as a point of information, the usual variable for holding the C++ compiler is `CXX` and the variable for holding C++ compiler flags is `CXXFLAGS`. These are just conventions though. – MadScientist Feb 20 '21 at 13:55
  • 1
    It's hard to say for sure but these errors look to me like you've included the `scene.cpp` source file in your `main.cpp` source file (via `#include "scene.cpp"` or something like that) – MadScientist Feb 20 '21 at 13:58
  • Hum, I do not have any include of that kind. This is the project I'm talking about, in case you want to see the full context: https://github.com/antoniogamiz/tfg – Antonio Gamiz Delgado Feb 20 '21 at 18:21
  • Maybe the problem is that I have more files in the include directory than in the source directory? – Antonio Gamiz Delgado Feb 20 '21 at 18:27
  • Well, then, you must be defining these functions/symbols in some header file which is being included in multiple source files. C++ requires that a given symbol can only be _defined_ once i the lifetime of your project. By including a header with the definition of a symbol into multiple source files you're defining it multiple times, exactly as if you'd copied into each source file separately (that's really exactly what an `#include` does). That's illegal. You can _declare_ a symbol as many times as you want, but you can only _define_ it one time per linked output. – MadScientist Feb 20 '21 at 19:17
  • Just to say: it has nothing at all to do with whether you have the same number of header files as source files. That is not going to help you get to the bottom of this so forget about it. – MadScientist Feb 20 '21 at 20:21

1 Answers1

0

Given that before you only had headers and now you want to split it up into cpp and h files my best bet is that you dont have the correct include guards, be that #pragma once or

#ifndef MYHEADER_H
#define MYHEADER_H
...
#endif

before you had exactly one source file (main.cpp) now you have multiple ones and so you have multiple translation units which all include certain headers this will ultimately lead to multiple definition of ... errors

So make sure the header guards exist.

disservin
  • 83
  • 1
  • 6