1
votes

For my c++ code I need a custom processor macro that will be defined during compilation. According to what I have read I concluded in the following Makefile:

CC := g++ 

# Folders
SRCDIR := src
BUILDDIR := build
TARGETDIR := bin

# Targets
EXECUTABLE := app
TARGET := $(TARGETDIR)/$(EXECUTABLE)

SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -Dcustom_macro -g -c -Wall -std=c++14
INC := -I include

$(TARGET): $(OBJECTS)
    @echo " Linking..."
    $(CC) $^ -o $(TARGET) $(LIB)


$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
    @mkdir -p $(BUILDDIR)
    $(CC) $(CFLAGS) $(INC) -c -o $@ $<



clean:
    @echo " Cleaning..."; 
    $(RM) -r $(BUILDDIR) $(TARGET)

The build is successful but when I run my app the code inside #ifdef custom_macro #endif is not executed but only when I change it to #ifndef . I also tried to set -Dcustom_macro=1 but I had the same behavior. I also tried to insert this with the CPPFLAGS macro as following :

CC := g++ 

# Folders
SRCDIR := src
BUILDDIR := build
TARGETDIR := bin

# Targets
EXECUTABLE := app
TARGET := $(TARGETDIR)/$(EXECUTABLE)

SRCEXT := cpp
SOURCES := $(shell find $(SRCDIR) -type f -name *.$(SRCEXT))
OBJECTS := $(patsubst $(SRCDIR)/%,$(BUILDDIR)/%,$(SOURCES:.$(SRCEXT)=.o))
CFLAGS := -g -c -Wall -std=c++14
CPPFLAGS := -Dcustom_macro
INC := -I include

$(TARGET): $(OBJECTS)
    @echo " Linking..."
    $(CC) $^ -o $(TARGET) $(LIB)


$(BUILDDIR)/%.o: $(SRCDIR)/%.$(SRCEXT)
    @mkdir -p $(BUILDDIR)
    $(CC) $(CPPFLAGS) $(CFLAGS) $(INC) -c -o $@ $<



clean:
    @echo " Cleaning..."; 
    $(RM) -r $(BUILDDIR) $(TARGET)

But nothing changed. The parameters that are passed to make according to compiler are:
The parameteres that are passed to make are the following:

 g++  -Dcustom_macro -g -c -Wall -std=c++14 -I include -c -o build/main.o src/main.cpp

Does anyone has any idea what I'm doing wrong?

1
"Does anyone has any idea what I'm doing wrong?" first of all there is special variable for c++ compiler, it is not a good idea to use CC and CFLAGS for that - Slava
when you run make what do you see (what parameters passed to g++) - Slava
@Slava thanks for the recommendation. The parameteres that are passed to make are the following: g++ -Dcustom_macro -g -c -Wall -std=c++14 -I include -c -o build/main.o src/main.cpp - dk13
Another recommendation - always use macro in UPPERCASE, and for the same reason do not use that for language constants or enums. - Slava
In which file that macro is used? - Slava

1 Answers

1
votes

Seems like you missed a space before macro name

CPPFLAGS := -D custom_macro