c++ - Mac 库链接

标签 c++ macos opencv gcc linker

我正在尝试编译一个使用 opencv 3.0 的给定程序。我已经通过 macports 安装了 opencv 2.x,并且一直在我的代码中使用它,没有任何问题。然而,给定的程序需要 opencv 3.0,所以我不得不自己将它安装在一些不同的位置,因为这个版本无法通过 macports 获得。在编辑 opencv 路径后,我尝试使用给定的 makefile 编译给定的程序。问题是我得到的编译错误似乎是链接问题,因为它查看位于 macports 安装的 2.x 版本路径中的头文件,而不是在指定位置。 makefile 中有一条注释,将 opencv 路径包含在 LD_LIBRARY_PATH 中。我有 Mac OS X 10.7.5,发现它的等价物是 DYLD_LIBRARY_PATH,但是设置这个环境变量并没有改变编译器输出中的错误。我试图寻找解决方案,但没有找到任何解决方案。请帮我解决这个链接问题。提前致谢。


这里是opencv 3.0的安装位置:

/用户/名称/Desktop/opencv-master/release/opt/local/

这里是macports安装opencv 2.x的地方:

/选择/本地/


编译错误信息:(第一个错误出现在"/opt/local/include/opencv2/nonfree/features2d.hpp",说明链接有问题。 )

In file included from /Users/name/Desktop/opencv-master/release/opt/local/include/opencv2/core.hpp:52:0,
             from /Users/name/Desktop/opencv-master/release/opt/local/include/opencv2/opencv.hpp:46,
             from src/Evaluation.h:35,
             from src/Evaluation.cpp:32:
/Users/name/Desktop/opencv-master/release/opt/local/include/opencv2/core/base.hpp:251:35: warning: unknown option after '#pragma GCC diagnostic' kind [-Wpragmas]
 #   pragma GCC diagnostic ignored "-Winvalid-noreturn"
                               ^
In file included from /opt/local/include/opencv2/nonfree/nonfree.hpp:46:0,
             from src/Evaluation.h:36,
             from src/Evaluation.cpp:32:
/opt/local/include/opencv2/nonfree/features2d.hpp:73:21: error: 'vector' has not been declared
                 vector<KeyPoint>& keypoints) const;
                 ^
/opt/local/include/opencv2/nonfree/features2d.hpp:73:27: error: expected ',' or '...' before '<' token
                 vector<KeyPoint>& keypoints) const;
                       ^
/opt/local/include/opencv2/nonfree/features2d.hpp:77:21: error: 'vector' has not been declared
                 vector<KeyPoint>& keypoints,
                 ^
/opt/local/include/opencv2/nonfree/features2d.hpp:77:27: error: expected ',' or '...' before '<' token
                 vector<KeyPoint>& keypoints,
                       ^
/opt/local/include/opencv2/nonfree/features2d.hpp:76:10: error: 'void cv::SIFT::operator()(cv::InputArray, cv::InputArray, int) const' cannot be overloaded
 void operator()(InputArray img, InputArray mask,
      ^
/opt/local/include/opencv2/nonfree/features2d.hpp:72:10: error: with 'void cv::SIFT::operator()(cv::InputArray, cv::InputArray, int) const'
 void operator()(InputArray img, InputArray mask,
      ^
/opt/local/include/opencv2/nonfree/features2d.hpp:83:49: error: 'vector' has not been declared
 void buildGaussianPyramid( const Mat& base, vector<Mat>& pyr, int nOctaves ) const;
                                             ^
/opt/local/include/opencv2/nonfree/features2d.hpp:83:55: error: expected ',' or '...' before '<' token
 void buildGaussianPyramid( const Mat& base, vector<Mat>& pyr, int nOctaves ) const;
                                                   ^
/opt/local/include/opencv2/nonfree/features2d.hpp:84:33: error: 'vector' does not name a type
 void buildDoGPyramid( const vector<Mat>& pyr, vector<Mat>& dogpyr ) const;
                             ^
/opt/local/include/opencv2/nonfree/features2d.hpp:84:39: error: expected ',' or '...' before '<' token
 void buildDoGPyramid( const vector<Mat>& pyr, vector<Mat>& dogpyr ) const;
                                   ^
/opt/local/include/opencv2/nonfree/features2d.hpp:85:39: error: 'vector' does not name a type
 void findScaleSpaceExtrema( const vector<Mat>& gauss_pyr, const vector<Mat>& dog_pyr,
                                   ^
/opt/local/include/opencv2/nonfree/features2d.hpp:85:45: error: expected ',' or '...' before '<' token
 void findScaleSpaceExtrema( const vector<Mat>& gauss_pyr, const vector<Mat>& dog_pyr,
                                         ^
/opt/local/include/opencv2/nonfree/features2d.hpp:89:40: error: 'vector' has not been declared
 void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                    ^
/opt/local/include/opencv2/nonfree/features2d.hpp:89:46: error: expected ',' or '...' before '<' token
 void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                          ^
/opt/local/include/opencv2/nonfree/features2d.hpp:90:41: error: 'vector' has not been declared
 void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                     ^
/opt/local/include/opencv2/nonfree/features2d.hpp:90:47: error: expected ',' or '...' before '<' token
 void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                           ^
/opt/local/include/opencv2/nonfree/features2d.hpp:125:28: error: 'vector' has not been declared
                 CV_OUT vector<KeyPoint>& keypoints) const;
                        ^
/opt/local/include/opencv2/nonfree/features2d.hpp:125:34: error: expected ',' or '...' before '<' token
                 CV_OUT vector<KeyPoint>& keypoints) const;
                              ^
In file included from /opt/local/include/opencv2/nonfree/nonfree.hpp:46:0,
             from src/Evaluation.h:36,
             from src/Evaluation.cpp:32:
/opt/local/include/opencv2/nonfree/features2d.hpp:128:28: error: 'vector' has not been declared
                 CV_OUT vector<KeyPoint>& keypoints,
                        ^
/opt/local/include/opencv2/nonfree/features2d.hpp:128:34: error: expected ',' or '...' before '<' token
                 CV_OUT vector<KeyPoint>& keypoints,
                              ^
/opt/local/include/opencv2/nonfree/features2d.hpp:127:10: error: 'void cv::SURF::operator()(cv::InputArray, cv::InputArray, int) const' cannot be overloaded
 void operator()(InputArray img, InputArray mask,
      ^
In file included from /opt/local/include/opencv2/nonfree/nonfree.hpp:46:0,
             from src/Evaluation.h:36,
             from src/Evaluation.cpp:32:
/opt/local/include/opencv2/nonfree/features2d.hpp:124:10: error: with 'void cv::SURF::operator()(cv::InputArray, cv::InputArray, int) const'
 void operator()(InputArray img, InputArray mask,
      ^
In file included from /opt/local/include/opencv2/nonfree/nonfree.hpp:46:0,
             from src/Evaluation.h:36,
             from src/Evaluation.cpp:32:
/opt/local/include/opencv2/nonfree/features2d.hpp:142:40: error: 'vector' has not been declared
 void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                    ^
/opt/local/include/opencv2/nonfree/features2d.hpp:142:46: error: expected ',' or '...' before '<' token
 void detectImpl( const Mat& image, vector<KeyPoint>& keypoints, const Mat& mask=Mat() ) const;
                                          ^
/opt/local/include/opencv2/nonfree/features2d.hpp:143:41: error: 'vector' has not been declared
 void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                     ^
/opt/local/include/opencv2/nonfree/features2d.hpp:143:47: error: expected ',' or '...' before '<' token
 void computeImpl( const Mat& image, vector<KeyPoint>& keypoints, Mat& descriptors ) const;
                                           ^

Makefile 内容:(我在这里更改了 opencv 路径变量,并添加了更多包含和库路径)

CC = g++
CFLAGS = -c 
PFLAGS = -fopenmp
WFLAGS = -Wall  -Warray-bounds
NFLAGS = -std=gnu++11
OPTS = -O1
OFLAGS =  -g

#LFLAGS = -Lgui -lcpptk -ltcl8.5 -ltk8.5
#LFLAGS = -llapack -lm
LFLAGS = -llapack

# ENTER YOUR EXECUTABLE NAME HERE 
EXECUTABLE = someName

#Opencv Includes Go here

####Change the OPENCV_PATH here#####
OPENCV_PATH= /Users/name/Desktop/opencv-master/release/opt/local


OPENCV_LIBS = -lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_objdetect -lopencv_ts -lopencv_video -lopencv_nonfree -lopencv_videostab
#OPENCV_LIBS = -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_highgui -lopencv_imgproc -lopencv_video
OPENCV_INCPATH =  -I$(OPENCV_PATH)/include
OPENCV_INCPATH1 = -I$(OPENCV_PATH)/include/opencv
OPENCV_INCPATH2 = -I$(OPENCV_PATH)/include/opencv2
#/opencv2/ -I$(OPENCV_PATH)/include/opencv/
OPENCV_LIBPATH = -L$(OPENCV_PATH)/lib
OPENCV_LIBPATH2 = -L$(OPENCV_PATH)
OPENCV_LIBPATH3 = -L$(OPENCV_PATH)/include/opencv
OPENCV_LIBPATH4 = -L$(OPENCV_PATH)/include/opencv2

#Boost Libs Go Here
BOOST_LIBS = -lboost_system  -lboost_filesystem  -lboost_program_options 

CPP_FILES := $(wildcard src/*.cpp)
OBJ_FILES := $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))


SUBMIT_DIR = $(shell whoami)
BACKUP_DIR = $(shell date "+%b_%d_%Y_%I_%M")
BACKUP_REPO = ./Backups
BACKUP_PATH = $(BACKUP_REPO)/$(BACKUP_DIR)

all: $(EXECUTABLE)


$(EXECUTABLE): $(OBJ_FILES)
    $(CC) $(NFLAGS) $(PFLAGS) $(WFLAGS) $(OPTS) $(LFLAGS) $(OPENCV_LIBS) $(OPENCV_INCPATH) $(OPENCV_INCPATH1) $(OPENCV_INCPATH2) $(OPENCV_LIBPATH) $(OPENCV_LIBPATH2) $(OPENCV_LIBPATH3) $(OPENCV_LIBPATH4) $(BOOST_LIBS) $^ -o $@   $(LFLAGS)

obj/%.o: src/%.cpp
    mkdir -p ./obj
    $(CC) $(NFLAGS) $(PFLAGS) $(CFLAGS) $(WFLAGS) $(OPTS) $(OFLAGS) $(OPENCV_LIBS) $(OPENCV_INCPATH) $(OPENCV_INCPATH1) $(OPENCV_INCPATH2) $(OPENCV_LIBPATH) $(OPENCV_LIBPATH2) $(OPENCV_LIBPATH3) $(OPENCV_LIBPATH4) $(BOOST_LIBS) -c -o $@ $<  $(LFLAGS)



clean:
    rm -f $(OBJ_FILES)
    rm -f *.out
    rm -f *~
    rm -f $(EXECUTABLE) 


#Create a Backup directory with <Month>_<Date>_<Year>_<Hr>_<Min>_<Sec>.tar
backup: 
    mkdir -p $(BACKUP_REPO)
    mkdir -p $(BACKUP_PATH)
    mkdir -p $(BACKUP_PATH)/src
    cp -r ./src/*.h ./$(BACKUP_PATH)/src
    cp -r ./src/*.cpp ./$(BACKUP_PATH)/src
    cp Makefile $(BACKUP_PATH)/
    #cp TestScript.sh $(BACKUP_PATH)/
    tar -zcvf $(BACKUP_REPO)/$(BACKUP_DIR).tar $(BACKUP_PATH)/
    rm -rf $(BACKUP_PATH)

我写道:

export DYLD_LIBRARY_PATH=/Users/name/Desktop/opencv-master/release/opt/local/lib

ma​​ke命令之前在终端中,但问题依然存在。


感谢您的帮助。

最佳答案

正如 berak 在之前的评论中所指出的,问题出在编译器上。通过更改使用的编译器解决了此问题。这是通过编辑 makefile 完成的:(CC = g++-mp-4.8)。

然后我得到了一些关于 boost 库文件的错误,但是通过添加它的路径并附加“-mt”来提升文件名来解决这个问题,如 ( library not found for -lboost_system ) 中的回答。

再次感谢,感谢您的帮助。

关于c++ - Mac 库链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25941111/

相关文章:

c++ - 创建和发送崩溃报告

c++ - x86 和 x64 堆栈帧

c++ - 从数组创建或 reshape OpenCV 3 channel 垫

swift - 仅当 Preferences.app 打开时发送 settingsChangedNotification (Swift - OSX)

windows-vista - openCV 支持 vista 64x

opencv - 使用OpenCV Python和Hough变换进行线检测

c++ - 基类指针只得到一个派生类变量值而不是基类变量值为什么?

Windows 上的 C++ 在启动后立即关闭程序

objective-c - 最佳方式以及如何在 mac osx cocoa 应用程序中显示多个 View ?

c++ - 使用 OpenCV 描述符匹配 findFundamentalMat