Skip to content

Instantly share code, notes, and snippets.

@Wenchy

Wenchy/Makefile

Last active Jan 12, 2021
Embed
What would you like to do?
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
@ezra40b

This comment has been minimized.

Copy link

@ezra40b ezra40b commented May 16, 2020

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

@awerebea

This comment has been minimized.

Copy link

@awerebea awerebea commented Nov 23, 2020

Thanks! Very useful.

@racusta

This comment has been minimized.

Copy link

@racusta racusta 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 $< $(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
You can’t perform that action at this time.