当我尝试使用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命令:
gcc命令:
源代码: