我很感激我在运行Makefile时遇到的这个错误。在将代码分成不同的文件之前,我编写的代码正在编译。
我有main.c文件,其中包含main函数和“color.h”。其他文件是color.h和color.c。 Color.c还包含color.h。这是我的第一个Makefile,我已经阅读了很多主题和主题,所以如果我错了请纠正我。
这是Makefile
CC = g++
CPPFLAGS = `pkg-config opencv --cflags`
LDFLAGS = `pkg-config opencv --libs`
ARCH = -arch x86_64
DEPENDENCIES = main.o color.o
# FINAL OUTPUTS
main: $(DEPENDENCIES)
$(CC) $(ARCH) $(LDFLAGS) -o $(DEPENDENCIES)
# MODULES
main.o: main.c color.h
$(CC) $(ARCH) $(CPPFLAGS) -c main.c
colorOps.o: color.c
$(CC) $(ARCH) $(CPPFLAGS) -c colorOps.c
# CLEAN
clean:
-rm main $(DEPENDENCIES)
这是我得到的错误
Undefined symbols for architecture x86_64:
"_main", referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [main] Error 1
我在Mac OS X 10.8.4上运行通过Macports安装的OpenCv,正如我所说,在我分离文件之前一切正常。
非常感谢!
答案 0 :(得分:1)
你几乎是对的:
您只需要将输出文件名(程序名称)添加到链接步骤:
main: $(DEPENDENCIES)
$(CC) $(ARCH) $(LDFLAGS) -o $@ $(DEPENDENCIES)
# ^^^^^
你好,你忘记了。如果您查看了命令行输出,您会看到它打印出来:
gcc -arch x86_64 /opencv-ld-flags/ -o main.o color.o
所以它所做的就是尝试输出main.o
color.o
的内容,如你想要的那样:
gcc -arch x86_64 /opencv-ld-flags/ -o main main.o color.o
$ @将在构建规则语句中的冒号(:)左侧评估目标文件(在本例中为main)。
CC = g++
# CPPFLAGS are different from CFLAGS
# keep CPPFLAGS name only for pre-processing flags
CFLAGS = `pkg-config opencv --cflags`
LDFLAGS = `pkg-config opencv --libs`
ARCH = -arch x86_64
# DEPENDENCIES is a slightly confusing name
# You will see how I generate dependencies below
# So I replaced DEPENDENCIES with OBJ
SRC=main.c color.c
OBJ=$(SRC:.c=.o)
# INCLUDES flags should be of the form -Idir/path etc.
# Reason for separating this out is to help with
# generating dependencies below
CPPFLAGS=
INCLUDES=
TARGET=main
default: $(TARGET)
$(TARGET): $(OBJ)
$(CC) $(LDFLAGS) -o $@ $(OBJ)
%.o: %.c
$(CC) $(CPPFLAGS) $(CFLAGS) $(INCLUDES) -c $< -o $@
# CLEAN
clean:
-rm main $(OBJ)
# Create a rule to generate dependencies information for the makefile
.deps: $(SRC)
$(CC) -o .deps $(CPPFLAGS) $(INCLUDES) -M -E $(SRC)
#include dependencies
include .deps