My goal is to use a single Makefile
for compiling a C app across various platforms.
I've been busily relearning C while working on a project, so as a result have not yet had the time to delve into make
; instead I have a single, long gcc
command in some .bat
/ .sh
files. Needless to say that each time I change one such file, I have to make those same changes in the others. Hence, make
. Conditions that need to be handled:
- Builds for multiple OS & architecture, i.e. for now (at least) 32 & 64-bit for Windows & Linux, thus I need access to that info in the
Makefile
, and executables should go into distinct folders under/build
: i.e./win32
,/win64
,/lin32
,/lin64
(et cetera, in future). - Required DLLs should be copied in alongside the built executable.
- Separate compile & link phases (thanks @EtanReisner)
Question: Can someone show me how one would go about constructing a Makefile
using the info below? And explain how each part works / why it is needed for my particular case? I have an understanding of the prerequisites approach by which Makefile
s are built. Thanks in advance.
Here is my project directory tree:
+---bin
+---include
| +---enet
| +---GL
| +---libxml
| \---ncursesw
+---lib
+---src
+---fonts
\---shaders
And mingw gcc
for Windows 32-bit compile:
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
And the gcc
for Linux 64-bit compile:
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
(worth diffing these.)