Thread: How would you make pros create a makefile for this scenario?

  1. #1
    Registered User
    Join Date
    Jul 2015
    Posts
    2

    Question How would you make pros create a makefile for this scenario?

    Greetings, all.

    It's high time I began using a single Makefile for compiling a C app across various platforms.

    I've been relearning C while working on a project, so have not yet had time to delve into make; instead I have a single, long gcc command in some .bat / .sh files. Terrible I know; 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.


    Question Can someone give me an idea of how best to 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 Makefiles are built. Even just a starting point would be appreciated. This kind of thing has always been a weak point for me. Thanks in advance.

    Here is my project directory tree:

    Code:
    +---build
        |   +---lin64
        |   +---win32
        +---fonts
        +---include
        |   +---enet
        |   +---GL
        |   +---libxml
        |   \---ncursesw
        +---lib
        +---licenses
        +---shaders
        \---src
            +---my
            |   +---bitwise
            |   \---math
            \---glew
    And mingw `gcc` for Windows 32-bit compile:

    Code:
    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:
    Code:
    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.elf
    (worth diffing these.)

  2. #2
    Registered User
    Join Date
    Jul 2015
    Posts
    2
    Here's what I have so far:

    Code:
    CC = gcc
    CFLAGS = -Wall -Werror -Wmissing-prototypes -Wpointer-arith -Wcast-qual -Wstrict-prototypes -Wlogical-op -Wcast-align -Wconversion -Wpedantic -Wfloat-equal -w -std=c11 -m32 -fopenmp -O0
    
    ARCH := win
    BITS := 64
    
    SRCS := $(wildcard src/*.c)
    OBJS := $(addprefix obj/,$(notdir $(SRCS:.c=.o)))
    
    PROG = wa
    LOCALLIBDIR = lib
    LDFLAGS = -L$(LOCALLIBDIR)
    LDLIBS  = -lglfw3-64 -lopengl32 -lgdi32 -lenet-64 -lws2_32 -lwinmm -llibxml2-64 -lwin64/libncursesw6
    
    all: $(PROG)
    
    $(PROG): $(OBJS)
        $(CC) $(OBJS) $(LDFLAGS) $(LDLIBS) -o
    
    $(???): $(SRCS)
        $(CC) $(SRCS) $(CFLAGS) -c
    clean:
        rm -f wa.exe
    Am not sure what to do with $(???). Am trying to split the compile and link phases. Anyone?
    Last edited by Arcane; 07-03-2015 at 08:26 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. create a symbolic link in a MakeFile
    By zahid990170 in forum Tech Board
    Replies: 1
    Last Post: 05-17-2011, 07:54 AM
  2. Replies: 2
    Last Post: 04-27-2011, 04:14 PM
  3. automake does not create Makefile.in
    By useless79 in forum Tech Board
    Replies: 0
    Last Post: 02-20-2011, 04:05 AM
  4. create dir in makefile
    By rotis23 in forum Linux Programming
    Replies: 5
    Last Post: 11-15-2002, 04:10 AM
  5. using MAKE with makefile to create executable file
    By sballew in forum C Programming
    Replies: 1
    Last Post: 11-19-2001, 12:49 PM

Tags for this Thread