Mac OS c ++链接OpenCV和libtiff

时间:2017-06-26 06:14:46

标签: c++ macos opencv compilation

我最近写了一个代码来加载TIFF图像(使用libtiff)并用opencv显示它(我知道opencv可以读取tiff,但我需要读取它以获得更低级别的控制)。

当我刚用libtiff阅读TIFF并编译它时。没关系。编译命令如下:

g++ -L /usr/local/lib/ -I /usr/local/include/ -ltiff libtiff_test.cpp -o libtiff_test

然而,当我把它们(OpenCV和Libtiff)放在一起时,会出现问题。编译命令如下:

g++ -L /usr/local/lib/ -I /usr/local/include/ -ltiff -lopencv_core -lopencv_highgui -lopencv_imgcodecs libtiff_test.cpp -o libtiff_test

编译器报告:架构x86_64的未定义符号。

The detailed information is as below(longer):
Undefined symbols for architecture x86_64:
  "ThermalImage::grfmt_tiff_err_handler_init", referenced from:
      ThermalImage::ThermalImage(char*) in ThermalImage-6b82a6.o
  "_cblas_cgemm", referenced from:
      lapack_gemm32fc(float const*, unsigned long, float const*, unsigned long, float, float const*, unsigned long, float, float*, unsigned long, int, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
      lapack_gemm64fc(double const*, unsigned long, double const*, unsigned long, double, double const*, unsigned long, double, double*, unsigned long, int, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
  "_cblas_dgemm", referenced from:
      lapack_gemm64f(double const*, unsigned long, double const*, unsigned long, double, double const*, unsigned long, double, double*, unsigned long, int, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
  "_cblas_sgemm", referenced from:
      lapack_gemm32f(float const*, unsigned long, float const*, unsigned long, float, float const*, unsigned long, float, float*, unsigned long, int, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
      lapack_gemm64f(double const*, unsigned long, double const*, unsigned long, double, double const*, unsigned long, double, double*, unsigned long, int, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
  "_cblas_zgemm", referenced from:
      lapack_gemm64fc(double const*, unsigned long, double const*, unsigned long, double, double const*, unsigned long, double, double*, unsigned long, int, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
  "_clBuildProgram", referenced from:
      cv::ocl::Program::Impl::Impl(cv::ocl::ProgramSource const&, cv::String const&, cv::String&) in libopencv_core.a(ocl.cpp.o)
  "_clCreateBuffer", referenced from:
      cv::ocl::OpenCLAllocator::allocate(cv::UMatData*, int, cv::UMatUsageFlags) const in libopencv_core.a(ocl.cpp.o)
      cv::ocl::OpenCLBufferPoolImpl::_allocateBufferEntry(cv::ocl::CLBufferEntry&, unsigned long) in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Image2D::Impl::init(cv::UMat const&, bool, bool) in libopencv_core.a(ocl.cpp.o)
  "_clCreateCommandQueue", referenced from:
      cv::ocl::Queue::Impl::Impl(cv::ocl::Context const&, cv::ocl::Device const&) in libopencv_core.a(ocl.cpp.o)
  "_clCreateContext", referenced from:
      cv::ocl::Context::Impl::setDefault() in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Context::Impl::Impl(int) in libopencv_core.a(ocl.cpp.o)
  "_clCreateImage", referenced from:
      cv::ocl::Image2D::Impl::init(cv::UMat const&, bool, bool) in libopencv_core.a(ocl.cpp.o)
  "_clCreateImage2D", referenced from:
      cv::ocl::Image2D::Impl::init(cv::UMat const&, bool, bool) in libopencv_core.a(ocl.cpp.o)
  "_clCreateKernel", referenced from:
      cv::ocl::Kernel::Impl::Impl(char const*, cv::ocl::Program const&) in libopencv_core.a(ocl.cpp.o)
  "_clCreateProgramWithBinary", referenced from:
      cv::ocl::Program::Impl::Impl(cv::String const&, cv::String const&) in libopencv_core.a(ocl.cpp.o)
  "_clCreateProgramWithSource", referenced from:
      cv::ocl::Program::Impl::Impl(cv::ocl::ProgramSource const&, cv::String const&, cv::String&) in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueCopyBuffer", referenced from:
      cv::ocl::OpenCLAllocator::copy(cv::UMatData*, cv::UMatData*, int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*, bool) const in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueCopyBufferRect", referenced from:
      cv::ocl::OpenCLAllocator::copy(cv::UMatData*, cv::UMatData*, int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*, bool) const in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Image2D::Impl::init(cv::UMat const&, bool, bool) in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueCopyBufferToImage", referenced from:
      cv::ocl::Image2D::Impl::init(cv::UMat const&, bool, bool) in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueCopyImageToBuffer", referenced from:
      cv::ocl::convertFromImage(void*, cv::UMat&) in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueMapBuffer", referenced from:
      cv::ocl::OpenCLAllocator::deallocate(cv::UMatData*) const in libopencv_core.a(ocl.cpp.o)
      cv::ocl::OpenCLAllocator::map(cv::UMatData*, int) const in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueNDRangeKernel", referenced from:
      cv::ocl::Kernel::run(int, unsigned long*, unsigned long*, bool, cv::ocl::Queue const&) in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueReadBuffer", referenced from:
      cv::ocl::OpenCLAllocator::deallocate(cv::UMatData*) const in libopencv_core.a(ocl.cpp.o)
      cv::ocl::OpenCLAllocator::map(cv::UMatData*, int) const in libopencv_core.a(ocl.cpp.o)
      cv::ocl::OpenCLAllocator::download(cv::UMatData*, void*, int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*) const in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueReadBufferRect", referenced from:
      cv::ocl::OpenCLAllocator::download(cv::UMatData*, void*, int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*) const in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueTask", referenced from:
      cv::ocl::Kernel::runTask(bool, cv::ocl::Queue const&) in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueUnmapMemObject", referenced from:
      cv::ocl::OpenCLAllocator::deallocate(cv::UMatData*) const in libopencv_core.a(ocl.cpp.o)
      cv::ocl::OpenCLAllocator::unmap(cv::UMatData*) const in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueWriteBuffer", referenced from:
      cv::ocl::OpenCLAllocator::unmap(cv::UMatData*) const in libopencv_core.a(ocl.cpp.o)
      cv::ocl::OpenCLAllocator::upload(cv::UMatData*, void const*, int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*) const in libopencv_core.a(ocl.cpp.o)
  "_clEnqueueWriteBufferRect", referenced from:
      cv::ocl::OpenCLAllocator::upload(cv::UMatData*, void const*, int, unsigned long const*, unsigned long const*, unsigned long const*, unsigned long const*) const in libopencv_core.a(ocl.cpp.o)
  "_clFinish", referenced from:
      cv::ocl::Queue::finish() in libopencv_core.a(ocl.cpp.o)
      cv::ocl::attachContext(cv::String const&, void*, void*, void*) in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Queue::operator=(cv::ocl::Queue const&) in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Queue::~Queue() in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Queue::create(cv::ocl::Context const&, cv::ocl::Device const&) in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Queue::~Queue() in libopencv_core.a(ocl.cpp.o)
      cv::ocl::Kernel::run(int, unsigned long*, unsigned long*, bool, cv::ocl::Queue const&) in libopencv_core.a(ocl.cpp.o)
      ...
  "_clFlush", referenced from:
      cv::ocl::Image2D::Impl::init(cv::UMat const&, bool, bool) in libopencv_core.a(ocl.cpp.o)
  "_clGetDeviceIDs", referenced from:
      cv::ocl::Context::Impl::setDefault() in 
      ...
  "_ippicvsFlip_64f_I", referenced from:
      void cv::sort_<unsigned char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<signed char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<unsigned short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<int>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<float>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<double>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      ...
  "_ippicvsFlip_8u_I", referenced from:
      void cv::sort_<unsigned char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<signed char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<unsigned short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<int>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<float>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<double>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      ...
  "_ippicvsMaxEvery_16u", referenced from:
      cv::hal::max16u(unsigned short const*, unsigned long, unsigned short const*, unsigned long, unsigned short*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsMaxEvery_32f", referenced from:
      cv::hal::max32f(float const*, unsigned long, float const*, unsigned long, float*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsMaxEvery_64f", referenced from:
      cv::hal::max64f(double const*, unsigned long, double const*, unsigned long, double*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsMaxEvery_8u", referenced from:
      cv::hal::max8u(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsMinEvery_16u", referenced from:
      cv::hal::min16u(unsigned short const*, unsigned long, unsigned short const*, unsigned long, unsigned short*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsMinEvery_32f", referenced from:
      cv::hal::min32f(float const*, unsigned long, float const*, unsigned long, float*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsMinEvery_64f", referenced from:
      cv::hal::min64f(double const*, unsigned long, double const*, unsigned long, double*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsMinEvery_8u", referenced from:
      cv::hal::min8u(unsigned char const*, unsigned long, unsigned char const*, unsigned long, unsigned char*, unsigned long, int, int, void*) in libopencv_core.a(arithm.cpp.o)
  "_ippicvsSortAscend_8u_I", referenced from:
      void cv::sort_<unsigned char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<signed char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<unsigned short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<int>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<float>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<double>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      ...
  "_ippicvsSortDescend_8u_I", referenced from:
      void cv::sort_<unsigned char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<signed char>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<unsigned short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<short>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<int>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<float>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      void cv::sort_<double>(cv::Mat const&, cv::Mat&, int) in libopencv_core.a(matrix.cpp.o)
      ...
  "_sgels_", referenced from:
      lapack_QR32f(float*, unsigned long, int, int, int, float*, unsigned long, float*, int*) in libopencv_core.a(hal_internal.cpp.o)
      lapack_QR64f(double*, unsigned long, int, int, int, double*, unsigned long, double*, int*) in libopencv_core.a(hal_internal.cpp.o)
  "_sgeqrf_", referenced from:
      lapack_QR32f(float*, unsigned long, int, int, int, float*, unsigned long, float*, int*) in libopencv_core.a(hal_internal.cpp.o)
      lapack_QR64f(double*, unsigned long, int, int, int, double*, unsigned long, double*, int*) in libopencv_core.a(hal_internal.cpp.o)
  "_sgesdd_", referenced from:
      lapack_SVD32f(float*, unsigned long, float*, float*, unsigned long, float*, unsigned long, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
      lapack_SVD64f(double*, unsigned long, double*, double*, unsigned long, double*, unsigned long, int, int, int) in libopencv_core.a(hal_internal.cpp.o)
  "_sgesv_", referenced from:
      lapack_LU32f(float*, unsigned long, int, float*, unsigned long, int, int*) in libopencv_core.a(hal_internal.cpp.o)
      lapack_LU64f(double*, unsigned long, int, double*, unsigned long, int, int*) in libopencv_core.a(hal_internal.cpp.o)
  "_sgetrf_", referenced from:
      lapack_LU32f(float*, unsigned long, int, float*, unsigned long, int, int*) in libopencv_core.a(hal_internal.cpp.o)
      lapack_LU64f(double*, unsigned long, int, double*, unsigned long, int, int*) in libopencv_core.a(hal_internal.cpp.o)
  "_sposv_", referenced from:
      lapack_Cholesky32f(float*, unsigned long, int, float*, unsigned long, int, bool*) in libopencv_core.a(hal_internal.cpp.o)
      lapack_Cholesky64f(double*, unsigned long, int, double*, unsigned long, int, bool*) in libopencv_core.a(hal_internal.cpp.o)
  "_spotrf_", referenced from:
      lapack_Cholesky32f(float*, unsigned long, int, float*, unsigned long, int, bool*) in libopencv_core.a(hal_internal.cpp.o)
      lapack_Cholesky64f(double*, unsigned long, int, double*, unsigned long, int, bool*) in libopencv_core.a(hal_internal.cpp.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

有人可以告诉我如何将这两个库结合在一起吗?谢谢!

1 个答案:

答案 0 :(得分:0)

您没有说明您安装的OpenCV版本(即版本2.x或3.x),也没有说明您安装的版本(例如来自源代码,或使用自制程序),也没有说如何构建它,也没有显示任何代码,所以任何答案都只能是通用的。

恕我直言,一般来说,最简单的方法是使用pkgconfig来确定正确的包含文件路径,库路径和库。因此,如果您安装 OpenCV 版本2,使用自制程序,您将执行以下操作以获取包含文件路径和库:

pkg-config --libs --cflags opencv

示例输出

-I/usr/local/include/opencv -I/usr/local/include -L/usr/local/lib \
-lopencv_ml -lopencv_objdetect -lopencv_shape -lopencv_stitching  \
-lopencv_superres -lopencv_videostab -lopencv_calib3d             \
-lopencv_features2d -lopencv_highgui -lopencv_videoio             \
-lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_imgproc \
-lopencv_flann -lopencv_core

将它放在你的编译命令中:

g++ ... $(pkg-config --libs --cflags opencv) ...

如果您安装了 OpenCV 3.x ,则可能需要

pkg-config --libs --cflags opencv3

如果您安装了其他方式,则可能需要从源代码转到构建 OpenCV 的位置,然后找到pkgconfig这样的文件:

find . -name \*.pc

然后您可以针对该特定文件运行pkgconfig,如下所示:

pkg-config --libs --cflags $(find . -name \*.pc)

您可以为libtiff应用相同的逻辑。首先,找到pkgconfig用于libtiff的名称,列出所有已知的包并为tif加注:

pkg-config --list-all | grep -i tif

示例输出

libtiff-4                           libtiff - Tag Image File Format (TIFF) library.

现在,使用新发现的名称(libtiff-4)来获取标志:

pkg-config --libs --cflags libtiff-4

示例输出

-I/usr/local/Cellar/libtiff/4.0.8/include \
-L/usr/local/Cellar/libtiff/4.0.8/lib -ltiff