我的目标是使用单个Makefile
在各种平台上编译C app。
我在做项目时一直忙着重新学习C,结果还没来得及深入研究make
;相反,我在一些gcc
/ .bat
文件中有一个长.sh
命令。不用说,每次我更改一个这样的文件,我都必须在其他文件中进行相同的更改。因此,make
。需要处理的条件:
Makefile
中的信息,可执行文件应该进入/build
下的不同文件夹:ie /win32
,/win64
,/lin32
, /lin64
(等等,将来)。 问题:有人可以告诉我如何使用以下信息构建Makefile
吗?并解释每个部分如何工作/为什么我的特定情况需要它?我了解构建Makefile
的先决条件方法。提前谢谢。
这是我的项目目录树:
+---bin
+---include
| +---enet
| +---GL
| +---libxml
| \---ncursesw
+---lib
+---src
+---fonts
\---shaders
mingw gcc
用于Windows 32位编译:
gcc -Iinclude ./src/main.c ./src/MainCtrl.c ./src/MainView.c ./src/PerspectiveView.c ./src/LogView.c ./src/LobbyView.c ./src/TerminalView.c ./src/World.c ./src/Chunk.c ./src/CellPattern.c ./src/CellPlan.c ./src/GridPoint.c ./src/Entity.c ./src/InfluenceRadial.c ./src/AoIList.c ./src/AoI.c ./src/ChunkRenderable.c ./src/PrimitiveRenderable.c ./src/Geometry.c ./src/glew/glew.c ./src/stb/stb_image_aug.c ../curt/list_generic.c ../curt/map_generic.c ../curt/intMap.c ../curt/floatMap.c ../hedgehog/hedgehog.c ./src/Inputs.c ./src/InputResponse.c ./src/Network.c ../Disjunction/c/disjunction.c ./src/my/math/Range.c ./src/my/math/Point.c -Llib -lglfw3-32 -lopengl32 -lgdi32 -lenet-32 -lws2_32 -lwinmm -llibxml2-32 -L. -ldll32/libncursesw6 -std=c11 -m32 -w -Wall -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wstrict-prototypes -Wlogical-op -Wcast-align -Wconversion -Wpedantic -Wfloat-equal -fopenmp -O0 -o ./build/win32/wa
用于Linux 64位编译的gcc
:
gcc -Iinclude -I/usr/include/libxml2 ./src/main.c ./src/MainCtrl.c ./src/MainView.c ./src/PerspectiveView.c ./src/LogView.c ./src/LobbyView.c ./src/TerminalView.c ./src/World.c ./src/Chunk.c ./src/CellPattern.c ./src/CellPlan.c ./src/GridPoint.c ./src/Entity.c ./src/InfluenceRadial.c ./src/AoIList.c ./src/AoI.c ./src/ChunkRenderable.c ./src/PrimitiveRenderable.c ./src/Geometry.c ./src/glew/glew.c ./src/stb/stb_image_aug.c ../curt/list_generic.c ../curt/map_generic.c ../curt/intMap.c ../curt/floatMap.c ../hedgehog/hedgehog.c ./src/Inputs.c ./src/InputResponse.c ./src/Network.c ../disjunction/c/disjunction.c ./src/my/math/Range.c ./src/my/math/Point.c -Llib -L/usr/lib -l:libglfw.so.3 -lncurses -lGL -lxml2 -lenet -lm -std=c11 -m64 -w -Wall -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wmissing-prototypes -Wstrict-prototypes -Wlogical-op -Wcast-align -Wconversion -Wpedantic -Wfloat-equal -fopenmp -g -o ./wa-64.elf
(值得区分这些。)
答案 0 :(得分:0)
试验和错误导致以下......正在进行中,但基础知识仍然存在。特性:
src
中的所有相关来源)make BITS=32
)感谢Yanick Rochon,Samuel和Trevor Robinson的答案以及撰写GNU Makefile examples的人。
CC = gcc
#Directories
HDRDIR = include
SRCDIR = ./src
OBJDIR = ./obj
BINDIR = ./bin
LIBDIR = lib
#File lists
HDRS := $(wildcard $(HDRDIR)/*.h)
SRCS := $(wildcard $(SRCDIR)/*.c)
OBJS := $(SRCS:$(SRCDIR)/%.c=$(OBJDIR)/%.o)
#Autodetect OS / architecture
OS =
BITS = 32
#ifeq ($(shell echo "check_quotes"),"check_quotes")
ifeq ($(OS),Windows_NT)
OS := win
EXT := .exe
rm := del /q
cp := copy /y
ifeq ($(PROCESSOR_ARCHITECTURE),AMD64)
CFLAGS += -D AMD64
endif
ifeq ($(PROCESSOR_ARCHITECTURE),x86)
CFLAGS += -D IA32
endif
else
UNAME_S := $(shell uname -s)
ifeq ($(UNAME_S),Linux)
OS := lin
EXT := .elf
rm := rm -f
cp := cp
CCFLAGS += -D LINUX
BITS = $(shell getconf LONG_BIT)
endif
ifeq ($(UNAME_S),Darwin)
OS := osx
EXT :=
rm := rm -f
cp := cp
CCFLAGS += -D OSX
BITS = $(shell getconf LONG_BIT)
endif
UNAME_P := $(shell uname -p)
ifeq ($(UNAME_P),x86_64)
CCFLAGS += -D AMD64
endif
ifneq ($(filter %86,$(UNAME_P)),)
CCFLAGS += -D IA32
endif
ifneq ($(filter arm%,$(UNAME_P)),)
CCFLAGS += -D ARM
endif
endif
#Define flags for compile & link
CFLAGS = -I$(HDRDIR) -std=c11 -m64 -fopenmp -Wall -Werror -Wmissing-prototypes -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wlogical-op -Wcast-align -Wconversion -Wpedantic -Wfloat-equal -w -O0 -DGLEW_STATIC
LFLAGS = -L$(LIBDIR)
#Do we want verbose linker messages?
LVERBOSE := false
LLIBS =
ifeq ($(OS),win)
LLIBS += -lglfw3-$(BITS) -lglew32s -lglew32 -lopengl32 -lgdi32 -lwinmm
endif
ifeq ($(OS),lin)
LLIBS += -lglew32s -lglew32 -lGL -lm
endif
ifeq ($(LVERBOSE),true)
LFLAGS += -Wl,--verbose
endif
TARGET = program$(EXT)
#Rules
$(BINDIR)/$(TARGET): $(OBJS)
@$(CC) -o ./$@ $(OBJS) $(LFLAGS) $(LLIBS)
@echo Linking complete.
@$(cp) $(LIBDIR)\glfw3.dll $(BINDIR)
@echo DLLs accompanying executable.
$(OBJS): $(OBJDIR)/%.o : $(SRCDIR)/%.c
@$(CC) $(CFLAGS) -c $< -o ./$@
@echo Compiled $<.
.PHONEY: clean
clean:
ifeq ($(OS),win)
@$(rm) $(subst /,\\,$(OBJS))
@$(rm) $(subst /,\\,$(BINDIR)/*.*)
else
@$(rm) $(OBJS)
@$(rm) $(BINDIR)/*
endif
@echo Cleanup complete.
#$(subst /,\\, $(SOMETHING)) replaces all (make-native) forward slashes with backslashes, for Windows.