无法使用make正确编译

时间:2019-04-28 12:01:31

标签: c makefile compilation

当我尝试使用make进行编译时,该程序无法运行,但是可以通过外壳程序使用gcc进行编译。 make不会给出任何错误或任何信息,并且可以正常运行,但只能用于一个源文件,但不能正常运行(playler.c),这很奇怪,以至于敌人的IA(用“ (A)“)的作品。正如我所看到的,display.c不能与make命令一起使用(敌人应该没有踪迹)。

使用shell命令进行编译时,必须包含.c而不是.h,但是make可以与.h一起正常使用,例如:

engine.c和shell命令:

#include <ncurses.h>
#include <unistd.h>

#include "./engine.h"
#include "./display.c"
#include "./enemy.c"
#include "./map.c"
#include "./player.c"
#include "../../config.h"

engine.c和make命令:

#include <ncurses.h>
#include <unistd.h>

#include "./engine.h"
#include "./display.h"
#include "./enemy.h"
#include "./map.h"
#include "./player.h"
#include "../../config.h"

Shell:

gcc -lncurses src/main.c -o WeirdRpg

我首先创建的makefile:

NAME=WeirdRpg
RM=rm -rf
CC=gcc
CLIBS= -lncurses
CFLAGS= -g -Wall -pedantic
SRC=src
OBJ=build/
OUT=build/$(NAME)

SOURCES=$(wildcard $(SRC)/*.c)
ENGINE=$(wildcard $(SRC)/engine/*.c)
OBJECTS=$(patsubst $(SRC)/%.c, $(OBJ)/%.o, $(SOURCES) $(ENGINE))

all: $(OUT)

$(OUT): $(OBJECTS)
    $(CC) $(CFLAGS) $(CLIBS) $^ -o $@

$(OBJ)/%.o: $(SRC)/%.c
    mkdir -p "$(shell dirname "$@")"
    $(CC) $(CFLAGS) -I$(SRC) -c $< -o $@

$(OBJ)/%.o: $(SRC)/%.c
    mkdir -p "$(shell dirname "$@")"
    $(CC) $(CFLAGS) -I$(SRC) -c $< -o $@

debug: CFLAGS+=-DDEBUG
debug: build

.PHONY: clean

clean:
    $(RM) $(OBJ) $(OUT)

从站点制作文件:

TARGET_EXEC ?= WeirdRpg

BUILD_DIR ?= ./build
SRC_DIRS ?= ./src
CLIBS= -lncurses
CC=clang

SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s)
OBJS := $(SRCS:%=$(BUILD_DIR)/%.o)
DEPS := $(OBJS:.o=.d)

INC_DIRS := $(shell find $(SRC_DIRS) -type d)
INC_FLAGS := $(addprefix -I,$(INC_DIRS))

CPPFLAGS ?= $(INC_FLAGS) -MMD -MP

$(BUILD_DIR)/$(TARGET_EXEC): $(OBJS)
        $(CC) $(CLIBS) $(OBJS) -o $@ $(LDFLAGS)

$(BUILD_DIR)/%.c.o: %.c
        $(MKDIR_P) $(dir $@)
        $(CC) $(CPPFLAGS) $(CFLAGS) -c $< -o $@

.PHONY: clean

clean:
        $(RM) -r $(BUILD_DIR)

-include $(DEPS)

MKDIR_P ?= mkdir -p

结果:

make命令:

make command

gcc命令:

gcc command

源代码:

https://github.com/KisuYami/WeirdRPG

0 个答案:

没有答案