【问题标题】:OpenCv Makefile Undefined symbols for architecture x86_64 errorOpenCv Makefile 架构 x86_64 错误的未定义符号
【发布时间】:2023-03-10 06:40:01
【问题描述】:

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

我有 main.c 文件,其中包含主要功能和“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,正如我所说,在我分离文件之前一切正常。

非常感谢!

【问题讨论】:

    标签: c macos opencv makefile


    【解决方案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
    

    【讨论】:

    • 非常感谢,太好了!
    • 如果您喜欢这个答案,请考虑投票并接受它:-)
    猜你喜欢
    • 1970-01-01
    • 2017-02-15
    • 1970-01-01
    • 2012-01-19
    • 2021-10-17
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2014-05-14
    相关资源
    最近更新 更多