Thread: How to use C++ modules with a Makefile?

  1. #1
    Registered User
    Join Date
    Oct 2021

    How to use C++ modules with a Makefile?

    I have a Makefile which is minimal, yet complete. It is the following:

    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)
        @rm $(INSTALL_DIR)/$(OUT)
        @mkdir -p $(dir $@)
        @echo "Building $@..."
        @$(CC) $(CFLAGS) -c $< -o $@
    .PHONY: 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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    I am afraid that I have not yet had time to play around with modules, and what you described doesn't sound like how I expect them to work from what I recall reading on them. I suggest that you post a tiny program that demonstrates your problem. This way, at the very least other people can test things out to see if they can replicate the issue, or if not they might have some insight as to why you have to do what you did.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2009
    I am far from being an makefile expert I would say I am a Advanced Beginner.

    But, the line below implies to me that if the ".o" files do not exist the results would be different.
    OBJ := $(SRC:%=$(OBJECT)/%.o)
    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Oct 2021
    Thanks for your answers everyone! Actually I'm not using C++ anymore so I don't really care about this problem. Tho I've spent a couple of hours and found out that my problem is a classic and there is actually no official support for C++ modules with `Makefiles`.

    Btw @Salem, the link you posted is from the post created for my as I wanted to test a lot of sites to see which one will have more support and choose one. Thanks again!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. modules help
    By Krista Koeplin in forum C++ Programming
    Replies: 5
    Last Post: 02-20-2016, 03:00 PM
  2. Help with modules
    By Jacob Boyd in forum C++ Programming
    Replies: 3
    Last Post: 10-23-2012, 10:08 AM
  3. modules in C++
    By jamort in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2009, 02:07 AM
  4. difference makefile
    By Bargi in forum Linux Programming
    Replies: 7
    Last Post: 10-28-2007, 02:08 PM
  5. Modules
    By Hakim in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2006, 04:53 PM

Tags for this Thread