简体   繁体   English

OpenCv Makefile架构x86_64错误的未定义符号

[英]OpenCv Makefile Undefined symbols for architecture x86_64 error

I would appreciate some help with this error which I get when I run my Makefile. 我希望在运行Makefile时得到此错误的帮助。 The code that I have was compiling fine before I separated it into different files. 在将代码分成不同的文件之前,我可以很好地编译这些代码。

I have main.c file in which is main function and "color.h" included. 我有main.c文件,其中是main函数,其中包括“ color.h”。 Other files are color.h and color.c . 其他文件是color.h和color.c。 Color.c has color.h included aswell. Color.c也包含color.h。 This is my first Makefile and I've read many threads and topics about that so please correct me if I'm wrong. 这是我的第一个Makefile,我已经阅读了很多有关该主题的主题和文章,所以如果我写错了,请纠正我。

This is the 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)

and this is the error that I get 这是我得到的错误

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

I'm running OpenCv installed via Macports on Mac OS X 10.8.4, and as I said, everything was working fine until I separated the files. 我正在运行通过Mac OS X 10.8.4上的Macports安装的OpenCv,并且正如我所说,在我分离文件之前,一切工作正常。

Thank you very much! 非常感谢你!

You almost have it right: 您几乎正确:

You just need to add the output filename (program name) to the linking step: 您只需要将输出文件名(程序名称)添加到链接步骤:

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

Hehe you just forgot that. 呵呵,你忘了那个。 If you had looked at the command-line output you'd have seen that it prints out: 如果您查看了命令行输出,则会看到它输出了:

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

So all it's doing is trying to OUTPUT to main.o contents of color.o where as what you intend is: 因此,所有它做的是试图输出到main.o内容color.o哪里,你打算是什么:

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

The $@ will evaluate to the target file (in this case main) on the left side of the colon (:) in the build rule statement. $ @将评估生成规则语句中冒号(:)左侧的目标文件(在本例中为main)。

Cleaner make file 清理文件

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM