我正在使用Vagrant和Virtualbox运行Ubuntu 18.0.4虚拟机。我正在编写一个C程序来管理所说机器上的虚拟内存,它由许多C和头文件组成。每当我在虚拟机中运行make时,都会出现此错误:
make[1]: Entering directory '/vagrant'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/Scrt1.o: In
function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Makefile:45: recipe for target 'vm-sim' failed
make[1]: *** [vm-sim] Error 1
make[1]: Leaving directory '/vagrant'
Makefile:23: recipe for target 'all' failed
make: *** [all] Error 2
这是我的Makefile:
TARGET = vm-sim
CC = gcc
CFLAGS = -Wall -Wextra -Wsign-conversion -Wpointer-arith -Wcast-qual -
Wwrite-strings -Wshadow -Wmissing-prototypes -Wpedantic -Wwrite-strings -g -
std=gnu99 -lm
LFLAGS =
SRCDIR = *-src
INCDIR = $(SRCDIR)
BINDIR = .
SUBMIT_FILES = $(SRC) $(INC) Makefile
SUBMISSION_NAME = project3-vm
SRC := $(wildcard $(SRCDIR)/*.c)
INC := $(wildcard $(INCDIR)/*.h)
INCFLAGS := $(patsubst %/,-I%,$(dir $(wildcard $(INCDIR)/.)))
.PHONY: all
all:
@$(MAKE) release && \
echo "$$(tput setaf 3)$$(tput bold)Note:$$(tput sgr0) this project compiled
with release flags by default. To compile for debugging, please use $$(tput
setaf 6)$$(tput bold)make debug$$(tput sgr0)."
.PHONY: debug
debug: CFLAGS += -ggdb -g3 -DDEBUG
debug: $(BINDIR)/$(TARGET)
.PHONY: release
release: CFLAGS += -mtune=native -O2
release: $(BINDIR)/$(TARGET)
.PHONY: clean
clean:
@rm -f $(BINDIR)/$(TARGET)
@rm -rf $(BINDIR)/$(TARGET).dSYM
.PHONY: submit
submit:
@(tar zcfh $(SUBMISSION_NAME).tar.gz $(SUBMIT_FILES) && \
echo "Created submission archive $$(tput
bold)$(SUBMISSION_NAME).tar.gz$$(tput sgr0).")
$(BINDIR)/$(TARGET): $(SRC) $(INC)
@mkdir -p $(BINDIR)
@$(CC) $(CFLAGS) $(INCFLAGS) $(SRC) -o $@ $(LFLAGS)
我只定义了一个main()函数,但是先前在另一个文件中定义了一个不同的函数;我认为这可能是问题所在,但不知道如何解决。
答案 0 :(得分:0)
以下生成文件:
gcc
可直接用于生成那些文件-D name=executablename
现在,生成文件:
SHELL = /bin/sh
BINDIR := /home/user/bin
.PHONY: all
all : $(BINDIR)/$(name)
SRC := $(wildcard *.c)
OBJ := $(SRC:.c=.o)
DEP := $(SRC:.c=.d)
INC := $(SRC:.c=.h)
MAKE := /usr/bin/make
CC := /usr/bin/gcc
CP := cp
MV := mv
LDFLAGS := -L/usr/local/lib
DEBUG := -ggdb3
CCFLAGS := $(DEBUG) -Wall -Wextra -pedantic -Wconversion -std=gnu11
LIBS := -lssl -ldl -lrt -lz -lc -lm
#
# link the .o files into the executable
# using the linker flags
# -- explicit rule
#
$(name): $(OBJ)
#
# ======= $(name) Link Start =========
$(CC) $(LDFLAGS) -o $@ $(OBJ) $(LIBS)
# ======= $(name) Link Done ==========
#
# note:
# using MV rather than CP results in all executables being re-made everytime
$(BINDIR)/$(name): $(name)
#
# ======= $(name) Copy Start =========
sudo $(CP) $(name) $(BINDIR)/.
# ======= $(name) Copy Done ==========
#
#
#create dependancy files -- inference rule
# list makefile.mak as dependancy so changing makfile forces rebuild
#
%.d: %.c
#
# ========= START $< TO $@ =========
$(CC) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
# ========= END $< TO $@ =========
#
# compile the .c file into .o files using the compiler flags
# -- inference rule
#
%.o: %.c %.d
#
# ========= START $< TO $@ =========
$(CC) $(CCFLAGS) -c $< -o $@ -I.
# ========= END $< TO $@ =========
#
.PHONY: clean
clean:
# ========== CLEANING UP ==========
rm -f *.o
rm -f $(name).map
rm -f $(name)
rm -f *.d
# ========== DONE ==========
# include the contents of all the .d files
# note: the .d files contain:
# <filename>.o:<filename>.c plus all the dependancies for that .c file
# I.E. the #include'd header files
# wrap with ifneg... so will not rebuild *.d files when goal is 'clean'
#
ifneq "$(MAKECMDGOALS)" "clean"
-include $(DEP)
endif