c++ - 无法链接 libtiff

标签 c++ libtiff

我正在尝试使用 Microsoft Visual Studio 2017 构建 C++ 程序。

#include "stdafx.h"
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
extern "C" {
     #include "libtiff/libtiff/tiffio.h"
}

using namespace cv;
using namespace std;

int main(int argc, char** argv)
{
string imageName("410.tif"); // start with a default

                             // If there is an argument, read it in as the name of the image
if (argc > 1)
{
    imageName = argv[1];
}

// Open the TIFF file using libtiff
TIFF* tif = TIFFOpen(imageName.c_str(), "r");

// Create a matrix to hold the tif image in
Mat image;

// check the tif is open
if (tif) {
    do {
        unsigned int width, height;
        uint32* raster;

        // get the size of the tiff
        TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
        TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);

        uint npixels = width * height; // get the total number of pixels

        raster = (uint32*)_TIFFmalloc(npixels * sizeof(uint32)); // allocate temp memory (must use the tiff library malloc)
        if (raster == NULL) // check the raster's memory was allocaed
        {
            TIFFClose(tif);
            cerr << "Could not allocate memory for raster of TIFF image" << endl;
            return -1;
        }

        // Check the tif read to the raster correctly
        if (!TIFFReadRGBAImage(tif, width, height, raster, 0))
        {
            TIFFClose(tif);
            cerr << "Could not read raster of TIFF image" << endl;
            return -1;
        }

        image = Mat(width, height, CV_8UC4); // create a new matrix of w x h with 8 bits per channel and 4 channels (RGBA)

                                             // itterate through all the pixels of the tif
        for (uint x = 0; x < width; x++)
            for (uint y = 0; y < height; y++)
            {
                uint32& TiffPixel = raster[y*width + x]; // read the current pixel of the TIF
                Vec4b& pixel = image.at<Vec4b>(Point(y, x)); // read the current pixel of the matrix
                pixel[0] = TIFFGetB(TiffPixel); // Set the pixel values as BGRA
                pixel[1] = TIFFGetG(TiffPixel);
                pixel[2] = TIFFGetR(TiffPixel);
                pixel[3] = TIFFGetA(TiffPixel);
            }

        _TIFFfree(raster); // release temp memory
                           // Rotate the image 90 degrees couter clockwise
        image = image.t();
        flip(image, image, 0);
        imshow("TIF Image", image); // show the image
        waitKey(0); // wait for anykey before displaying next
    } while (TIFFReadDirectory(tif)); // get the next tif
    TIFFClose(tif); // close the tif file
}
    return 0;
}

如您所见,我包含了 libtiff,但是当我尝试编译时出现以下错误:

LNK2019 reference to the external symbol _TIFFmalloc not resolved in the function

错误扩展为“_TIFFfree、TIFFClose、TIFFGetField、TIFFReadDirectory、TIFFReadRGBAImage、TIFFOpen”(为了更简单,我只发布了一个)。

我试着在谷歌上搜索这个问题,我认为这是由链接问题引起的。很多人告诉我应该在 Visual Studio 项目属性的“输入”部分添加“libtiff.lib”,但我从官方来源下载的库中没有 libtiff.lib。

我使用的是 4.0.4 版本的库。

最佳答案

TIFF 是一个 C 库,而您的程序是 C++,C++ 和 C 具有不同的链接 ABI。

当你使用来自 C++ 源代码的 C 库时,你通常应该这样做:

extern "C" {
    #include "libtiff/libtiff/tiffio.h"
}

... 向 C++ 发出该符号遵循 C ABI 的信号。

注意:许多广泛使用的 C 库在其 header 中包含 C++ 条件预处理器检查以避免此问题,例如:

#ifdef __cplusplus
extern "C" {
#endif
/* Actual header content */
extern int do_something(void); 
extern int do_something_else(const char*); 
/* ... */
#ifdef __cplusplus
}
#end

libtiff 不是这种情况。

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

相关文章:

c++ - 使用 boost::bind ...我做错了什么

c++ - 尝试加入时线程崩溃

c++ - 对等体断开连接后 SSL 内存未释放

c++ - Tiff Image : Private Tags 上的自定义标签

objective-c - 如何使用元数据生成 tiff 文件

c++ - 如何使用 CMake 指定在何处查找共享库?

c++ - 在 Mac 上编译 C++11 代码?

c++ - Libtiff:如何获取像素值或如何将 TIFF 文件转换为文本文件

javascript - javascript 中的 >>> 0 是什么?

c++ - ROS 的 libtiff4 错误