How to use C++ modules with a Makefile?

closed account (EUoNwbRD)
I have a Makefile which is minimal, yet complete. It is the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
OUT = example
INSTALL_DIR = /usr/local/bin

OBJECT = ./obj
SOURCE = ./src

SRC := $(shell find $(SOURCE) -name *.cc)
OBJ := $(SRC:%=$(OBJECT)/%.o)
DEPS := $(OBJ:.o=.d)

INC_DIRS := $(shell find $(SOURCE) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))

CC = gcc
CFLAGS = -pipe -fmodules-ts -std=c++2a
DEBUG_FLAGS := $(CFLAGS) -g -Wall -Wextra
RELEASE_FLAGS := $(CFLAGS) -O3 -flto

debug: $(OBJ)
	@echo "Building the DEBUG binary..."
	@$(CC) $(OBJ) -o $(OUT) $(DEBUG_FLAGS)
	@echo "The binary was built successfully!"

release: $(OBJ)
	@echo "Building the RELEASE binary..."
	@$(CC) $(OBJ) -o $(OUT) $(RELEASE_FLAGS)
	@echo "The binary was built successfully!"

install: $(OUT)
	@cp $(OUT) $(INSTALL_DIR)

uninstall:
	@rm $(INSTALL_DIR)/$(OUT)

$(OBJECT)/%.cc.o: %.cc
	@mkdir -p $(dir $@)
	@echo "Building $@..."
	@$(CC) $(CFLAGS) -c $< -o $@

.PHONY: clean

clean:
	rm -rf $(OBJECT) $(OUT)

-include $(DEPS)


There is a directory called "src" which includes the source files. When I'm running make, I will get a compilation error about having to first create modules before using them. If I do create them manually, then I will be able to use "Make" and it will work then it will work. Is there a way to automatically create them?
Last edited on
You're using a C compiler to compile your C++20 code.

Replace:
1
2
3
4
CC = gcc
CFLAGS = -pipe -fmodules-ts -std=c++2a
DEBUG_FLAGS := $(CFLAGS) -g -Wall -Wextra
RELEASE_FLAGS := $(CFLAGS) -O3 -flto

with:
1
2
3
4
CXX = g++
CXXFLAGS = -pipe -fmodules-ts -std=c++2a
DEBUG_FLAGS := $(CXXFLAGS) -g -Wall -Wextra
RELEASE_FLAGS := $(CXXFLAGS) -O3 -flto

Also:
 
@$(CC) $(OBJ) -o $(OUT) $(DEBUG_FLAGS)
with:
 
@$(CXX) $(OBJ) -o $(OUT) $(DEBUG_FLAGS) $(LDFLAGS)

Also:
 
@$(CC) $(OBJ) -o $(OUT) $(RELEASE_FLAGS)
with:
 
@$(CXX) $(OBJ) -o $(OUT) $(RELEASE_FLAGS) $(LDFLAGS)

Also:
 
@$(CC) $(CFLAGS) -c $< -o $@
with:
 
@$(CXX) $(CXXFLAGS) -c $< -o $@

You likely don't need that CXX=g++ as it's defaulted to CXX=c++

INSTALL_DIR should be renamed to PREFIX and initialized with:
 
PREFIX ?= /usr/local/bin

That way you can install into you home directory with:
 
make PREFIX=$HOME

or /usr with:
 
make PREFIX=/usr


It's possible to auto-generate the header dependencies ... but that's something else.
Last edited on
closed account (EUoNwbRD)
@kbw

Thanks for the time you took to review my question.

You're using a C compiler to compile your C++20 code

Actually, GCC can compile C++ code (file with the extension ".cc", ".cxx", ".cpp") and it will behave just like G++ without including the C++ library and the exceptions library (I don't know how it is called). I saw that somewhere and I don't need these in my project so GCC is fine for me

The "LDFLAGS" variable is it supposed to get defined and used inside the Makefile or is it common that people define it outside the Makefile? If the first thing is the case, I think that I can skip it

INSTALL_DIR should be renamed to PREFIX and initialized with

That's sweet, thanks!!

As you have pointed out things that have nothing to do with me problem but you checked them anyway (big thanks), you may also noticed that the "DEBUG_FLAGS" and "RELEASE" flags won't get used because the are only used in the link stage. I have noticed that but the problem with the modules is my first priority so I will fix them once I'm done with that.
Last edited on
Actually, GCC can compile C++ code
Maybe, but don't even try it. Two wrongs don't make a right. Not everyone uses GCC on Posix.

There's a bunch of built in rules and values defined in GNU Make. To see them all, run
 
make -p


Your makefile doesn't observe the GNU conventions, so doesn't benefit from any of those rules.

Anyway, back to the original problem, it would be helpful if you showed the actual errors.
Last edited on
Topic archived. No new replies allowed.