无法链接 libtiff

Can't link libtiff

本文关键字:libtiff 链接      更新时间:2023-10-16

我正在尝试使用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对外部符号_TIFFalloc的引用未在中解析功能

错误扩展到"_TIFFfree,TIFFLose,TIFFGetField,TIFFReadDirectory,TIFFeadRGBAImage,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库在其头文件中包含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的情况并非如此。

使用以下库:http://gnuwin32.sourceforge.net/packages/tiff.htm我可以正确地包含libtiff,但问题是这个库是64位的,openCV是32位的。所以我得到以下错误:

LNK4272库计算机的"x86"类型与目标计算机的"x64"类型冲突ImageManipulation C:\Program Files(x86(\GnuWin32\lib\libtiff.lib 1

Visual Studio中的平台解决方案是在x64上设置的,因为如果我将其打开x86,openCV将无法工作。