Skip to content

Instantly share code, notes, and snippets.

@wenchy
Last active January 16, 2024 15:36
Show Gist options
  • Star 34 You must be signed in to star a gist
  • Fork 6 You must be signed in to fork a gist
  • Save wenchy/64db1636845a3da0c4c7 to your computer and use it in GitHub Desktop.
Save wenchy/64db1636845a3da0c4c7 to your computer and use it in GitHub Desktop.
Compile all .cpp files into one target under the current directory.
CC := g++
CFLAGS := -Wall -g
TARGET := test
# $(wildcard *.cpp /xxx/xxx/*.cpp): get all .cpp files from the current directory and dir "/xxx/xxx/"
SRCS := $(wildcard *.cpp)
# $(patsubst %.cpp,%.o,$(SRCS)): substitute all ".cpp" file name strings to ".o" file name strings
OBJS := $(patsubst %.cpp,%.o,$(SRCS))
all: $(TARGET)
$(TARGET): $(OBJS)
$(CC) -o $@ $^
%.o: %.cpp
$(CC) $(CFLAGS) -c $<
clean:
rm -rf $(TARGET) *.o
.PHONY: all clean
@ezragol
Copy link

ezragol commented May 16, 2020

Thanks for this!
Helped me out as a novice with make

@awerebea
Copy link

awerebea commented Nov 23, 2020

Thanks! Very useful.

Copy link

ghost commented Jan 12, 2021

After compiling , when all .o are ready from all cpp. It throws error undefined reference for all files when creating final binary.

my modified script is

CC := g++
CFLAGS := -Wall -g
TARGET := proton
export REL_PATH := $(shell pwd)
INC :=-I$(REL_PATH)/include
LIBS += -lz -lrt -lpthread -lbluetooth -lssl -lcrypt -lcrypto

$(wildcard .cpp /xxx/xxx/.cpp): get all .cpp files from the current directory and dir "/xxx/xxx/"

SRCS := $(wildcard *.cpp)

$(patsubst %.cpp,%.o,$(SRCS)): substitute all ".cpp" file name strings to ".o" file name strings

OBJS := $(patsubst %.cpp,%.o,$(SRCS))

all: $(TARGET)
#gcc -o ProC_Utility.o -c ProC_Utility.c $(INC) -L$(LIBS)
$(TARGET): $(OBJS)
$(CC) -o $@ $^ $(INC) -L$(LIBS)

%.o: %.cpp
$(CC) $(CFLAGS) -c $&lt; $(INC) -L$(LIBS)
clean:
rm -rf $(TARGET) *.o

.PHONY: all clean
------------------------------------------------------------------------------------------------------------------------------------ ERROR shown below -----------
CMDFTPUp.o: In function CMDFTPUp::RunBlocking(char*, char*)': /home/workstation21/LDF_Integration_03/proton17dec/test/proton/CMDFTPUp.cpp:111: undefined reference to mkdir_r'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment