Reputation: 223
I've written a simple generic makefile for c++. It works great on Debian/Ubuntu. I would like to know if it could be running on MAC OS. I ask the question because i have no Mac OS at home and i can't test. Furthermore, if you have some advice, let's go. I'm not sure about these lines CXXFLAGS += -D OSX LDFLAGS := -stdlib=libstdc++ I precise that it for build only STL C++ programm for the moment. No dynamic libraries.
# sources (*.cpp)
SRC := sources
# headers (*.cpp)
INC := include
MAIN := kwic_personnel.cpp
EXEC := prog
# main file is in current directory or in sources file
ifneq ("$(wildcard $(MAIN))","")
sources := $(MAIN) $(wildcard $(SRC)/*.cpp)
else
sources := $(wildcard $(SRC)/*.cpp)
endif
# obj files
objects := $(sources:.cpp=.o)
# dep files
deps := $(objects:.o=.d)
CXX := g++
CXXFLAGS := -I $(INC) -MMD -MP -g -std=c++17 -Wall -pedantic -Weffc++ -Werror
# OS name
UNAME := $(shell uname -s)
# if linux
ifeq ($(UNAME), Linux)
# my own choices on compilator with Debian
LDFLAGS := -L /usr/lib/x86_64-linux-gnu
LDLIBS := -lcurl
# else but mac OS
else
# NOT SURE ABOUT THAT
CXXFLAGS += -D OSX
LDFLAGS := -stdlib=libstdc++
endif
# linking
$(EXEC) : $(objects)
@echo "Generation du fichier executable..."
$(CXX) $(LDFLAGS) $^ $(LDLIBS) -o $@
$(RM) $(objects) $(deps)
@echo "Compilation réussie !"
# compilation of cpp files
$(SRC)/%.o: $(SRC)/%.cpp
$(CXX) $(CPPFLAGS) $(CXXFLAGS) -c $^ -o $@
# remove exec
.PHONY: clean
clean:
$(RM) $(EXEC)
# dependencies
-include $(deps)
Upvotes: 0
Views: 2353
Reputation: 9058
I use gnu make on both Mac and Ubuntu. I do a bunch of things I'll explain.
First my Ubuntu is via a local Docker container, and it NFS mounts my local Mac work directory. So I can edit locally on my Mac but compile for Linux. This means if I build on both, I could have a mix of different targets being built. Clearly, this is problematic, so here are some tricks.
UNAME := $(shell uname)
MACAPPEND=
LIB_ARGS= -cvrU
ifeq ($(UNAME), Darwin)
MACAPPEND=-mac
CXX=clang++
LIB_ARGS= -cvr
endif
SRCDIR := src
OBJDIR := obj${MACAPPEND}
DEPDIR := .d
BINDIR := bin${MACAPPEND}
Then it's the same ${SRCDIR} for everything, but $OBJDIR} might resolve to obj or obj-mac. Same with bin and bin-mac. I compile with this:
${OBJDIR}/%.o : %.cpp
$(COMPILE.cc) ${CXXFLAGS} $(OUTPUT_OPTION) $<
Note that I do this stuff so much, that I produce a Makefile-Base and stuff it into /usr/local/etc and then can do this:
include /usr/local/etc/Makefile-Base
If I'm making a library:
LIBNAME=show${MACAPPEND}
LIB=lib/libshow${MACAPPEND}.a
lib: ${LIB}
${LIB}: ${LIB_OBJ}
@mkdir -p lib
ar ${LIB_ARGS} ${LIB} ${LIB_OBJ}
ranlib ${LIB}
See LIB_ARGS used here and defined near the top of this post.
Note that a LOT of what I'm doing is because of the way I share my directory across both environments and might be bulding in both. There are very few things that you need to do special on Mac.
Upvotes: 1