簡體   English   中英

是否使用適當的Makefile替換gcc編譯/鏈接運行?

[英]Appropriate Makefile to replace a gcc compile/link run?

我的目標是使用單個Makefile在各種平台上編譯C應用程序。

我在從事項目工作時一直在忙於學習C,因此結果還沒有時間去研究make 相反,我在某些.bat / .sh文件中只有一個很長的gcc命令。 不用說,每次我更改一個這樣的文件時,我都必須在其他文件中進行相同的更改。 因此, make 需要處理的條件:

  • 適用於多種操作系統和體系結構,即目前(至少)適用於Windows和Linux的32位和64位版本,因此我需要訪問Makefile中的信息,並且可執行文件應放入/build下的不同文件夾中,即/win32/win64 /lin32/lin32/lin64 (等等)。
  • 必需的DLL應該與生成的可執行文件一起復制到其中。
  • 單獨的編譯和鏈接階段(感謝@EtanReisner)

問題:有人可以告訴我如何使用以下信息構建Makefile嗎? 並解釋每個部分的工作方式/為什么我的特殊情況需要它? 我對構建Makefile的前提方法有所了解。 提前致謝。

這是我的項目目錄樹:

+---bin
+---include
|   +---enet
|   +---GL
|   +---libxml
|   \---ncursesw
+---lib
+---src
+---fonts
\---shaders    

對於Windows 32位的mingw gcc進行編譯:

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

(值得一提。)

反復試驗導致以下結果:工作仍在進行中,但基礎知識已經存在。 特征:

  • 無需手動列出源/目標文件(所有相關源都應在src
  • 使用共享邏輯的.c / .o文件列表分離編譯和鏈接階段
  • 操作系統和體系結構自動檢測(WIP),具有對Arch的手動覆蓋(例如, make BITS=32
  • 適用於Win和* nixes的Clean
  • 在編譯時以.DLL復制(WIP提供要復制的DLL列表)

感謝Yanick RochonSamuelTrevor Robinson的回答,以及那些編寫GNU Makefile示例的人

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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM