c - OpenCv Makefile Undefined symbols for architecture x86_64 错误

标签 c macos opencv makefile

对于我在运行 Makefile 时遇到的这个错误,我将不胜感激。在我将代码分成不同的文件之前,我的代码编译得很好。

我有 main.c 文件,其中包含主要功能和“color.h”。其他文件是 color.h 和 color.c 。 Color.c 也包含 color.h。这是我的第一个 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,正如我所说,在我分离文件之前一切正常。

非常感谢!

最佳答案

你几乎是对的:

您只需将输出文件名(程序名)添加到链接步骤:

main: $(DEPENDENCIES)
    $(CC) $(ARCH) $(LDFLAGS) -o $@ $(DEPENDENCIES) 
#                            ^^^^^

嘿嘿,你忘了。如果您查看过命令行输出,您会看到它打印出:

 gcc -arch x86_64 /opencv-ld-flags/ -o main.o color.o

所以它所做的就是尝试将 color.omain.o 内容输出到您想要的地方:

 gcc -arch x86_64 /opencv-ld-flags/ -o main main.o color.o

$@ 将评估构建规则语句中冒号 (:) 左侧的目标文件(在本例中为 main)。

更干净的 make 文件

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

关于c - OpenCv Makefile Undefined symbols for architecture x86_64 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17393105/

相关文章:

c - 将一个函数作为参数传递给另一个函数

c - printf 中的尾数值略有偏差

xcode - OSX 从 Xcode 中删除应用程序数据

python - 类型错误 : an integer is required (got type str) in python

c# - 如何使用 Emgu c# 加载 CascadeClassifier

python - 为什么三角点不能投影回 OpenCV 中的相同图像点?

iphone/objective c(简单)问题

macos - 您没有符合条件的 Mac OS X 应用程序包 ID。在这里注册一个

macos - 如何在 Mac 上安装 psycopg2(找不到 pg_config 可执行文件错误)

c - 哪个 NoSQL 数据库与 C 一起使用?