OS X编译错误:架构x86_64的未定义符号:

时间:2018-06-15 09:57:07

标签: macos makefile clang

我下载了Inria的密集轨迹项目,我尝试在我的mac上编译它。 https://lear.inrialpes.fr/people/wang/download/dense_trajectory_release_v1.2.tar.gz

使用make时出现问题我的错误是:

=== linking: release/DenseTrack ===
c++ -L/opt/lib -pipe -Wall -O3 -ggdb -o release/DenseTrack  -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc -lavformat -lavdevice -lavutil -lavcodec -lswscale
ld: warning: directory not found for option '-L/opt/lib'
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [release/DenseTrack] Error 1

我按照自述文件安装了opencv和ffmpeg。

有人知道如何处理这种错误吗?

**编辑**

第一个警告是由于编译器搜索我的opencv lib目录,但是仍然存在未定义符号的错误:

=== linking: release/DenseTrack ===
c++ -L/usr/local/Cellar/opencv/3.4.1_5/lib -pipe -Wall -O3 -ggdb -o release/DenseTrack  -lopencv_core -lopencv_highgui -lopencv_video -lopencv_imgproc -lavformat -lavdevice -lavutil -lavcodec -lswscale
Undefined symbols for architecture x86_64:
  "_main", referenced from:
     implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [release/DenseTrack] Error 1

Inria的makefile:

# set the binaries that have to be built
TARGETS := DenseTrack Video

# set the build configuration set 
BUILD := release
#BUILD := debug

# set bin and build dirs
BUILDDIR := .build_$(BUILD)
BINDIR := $(BUILD)

# libraries 
LDLIBS = $(addprefix -l, $(LIBS) $(LIBS_$(notdir $*)))
LIBS := \
    opencv_core opencv_highgui opencv_video opencv_imgproc \
    avformat avdevice avutil avcodec swscale

# set some flags and compiler/linker specific commands
CXXFLAGS = -pipe -D __STDC_CONSTANT_MACROS -D STD=std -Wall $(CXXFLAGS_$(BUILD)) -I. -I/opt/include
CXXFLAGS_debug := -ggdb
CXXFLAGS_release := -O3 -DNDEBUG -ggdb
LDFLAGS = -L/usr/local/Cellar/opencv/3.4.1_5/lib -pipe -Wall $(LDFLAGS_$(BUILD))
LDFLAGS_debug := -ggdb
LDFLAGS_release := -O3 -ggdb

include make/generic.mk

generic.mk是:

#
# Copyright (C) 2009 Alexander Kl"aser
# 
# This piece is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
# 
# This software has been downloaded from:
# http://lear.inrialpes.fr/people/klaeser/software
# 
#
# Variables that need to be set in the Makefile that includes this file:
#   TARGETS   all files that are exectuables without there .cpp extension
#   BUILDDIR  temporary dir where things are compiled to (optional, by default ".build")
#   BINDIR    dir where executables are linked to (optional, by default "bin")
#   SRCDIRS   list of directories in which source files are located
#             this variable needs to be set if you do not have your source and
#             include files located in the same directory!
#
# Variables used for compiling/linking:
#   CXXFLAGS  flags for compiling
#   LDFLAGS   flags used for linking
#   LDLIBS    list of libraries to be linked
#   CXX       compiler linker (should be g++ by default)
#

# set paths for the dependency tool and gcc
DEP = make/dep.py

# set some standard directories in case they have not been set
BUILDDIR ?= .build
BINDIR ?= bin

# all include files
INCLUDES := $(addprefix $(BUILDDIR)/,$(TARGETS:=.l))


#
# some general rules
#

.PHONY: all clean
.PRECIOUS: $(BUILDDIR)/%.d

all: $(BINDIR) $(addprefix $(BINDIR)/,$(notdir $(TARGETS))) 
    @echo "=== done ==="

$(INCLUDES): $(BUILDDIR)

clean:
    @echo "=== cleaning up ==="
    @rm -rf $(BUILDDIR)

$(BUILDDIR) $(BINDIR):
    @echo "=== creating directory: $@ ==="
    @mkdir -p $@


#
# rules for creating dependency files
#

# dependencies of .cpp files on other files
$(BUILDDIR)/%.d: %.cpp
    @echo "=== creating dependency file: $@ ==="
    @test -e $(dir $@) || mkdir -p $(dir $@)
    g++ $(CXXFLAGS) -MM -MT $(BUILDDIR)/$*.o -MT $(BUILDDIR)/$*.d -MF $@ $<

# dependencies for the linking
%.so.l %.l: %.d
    @echo "=== creating dependency file: $@ ==="
    @test -e $(dir $@) || mkdir -p $(dir $@)
    $(DEP) "$(BINDIR)/$(@F:.l=)" $*.l $(BUILDDIR) $< $(SRCDIRS) > $@


#
# rules for compiling and linking 
# (link dependencies are defined in .l files)
#

# compiling
$(BUILDDIR)/%.o: %.cpp
    @echo "=== compiling: $@ ==="
    @test -e $(dir $@) || mkdir -p $(dir $@)
    $(CXX) -fPIC $(CXXFLAGS) -c -o $@ $<

# linking for shared libraries 
$(BINDIR)/%.so:
    @echo "=== linking: $@ ==="
    @rm -f $@
    $(CXX) -shared $(LDFLAGS) -o $@ $(filter %.o, $^) $(LDLIBS)

# linking
$(BINDIR)/%:
    @echo "=== linking: $@ ==="
    @rm -f $@
    $(CXX) $(LDFLAGS) -o $@ $(filter %.o, $^) $(LDLIBS)

%: %.o
%.h: ;
%.hpp: ;
%.c: ;
%.cpp: ;


#
# include dependency files
#

ifneq ($(MAKECMDGOALS),clean)
-include $(INCLUDES)
endif

当我确实出现错误时,因为没有找到main,因此当我创建DenseTrack.cpp时,不会在已发布的目录中创建二进制文件 没有错误但没有做任何事情。

感谢。

1 个答案:

答案 0 :(得分:1)

首先,找到实际安装OpenCV库的位置。所以,让我们来libopencv_core.dylib并在一堆可能的地方寻找它,即$HOME/usr/opt

find $HOME /usr /opt -name libopencv_core.dylib

示例输出

/Users/mark/OpenCV/lib/libopencv_core.dylib

现在我们知道它在哪里(在我的系统上),所以我们可以告诉链接器:

c++ -L/Users/mark/OpenCV/lib ...