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 对外部符号 _TIFFmalloc 的引用未在函数中解析
该错误扩展为“_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 库在其头文件中包含 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 的情况。
推荐阅读
- automation - 在 sikulix 中,我如何制作一个 while 循环,条件是它在倒计时结束后不会重复?
- python - 如何使用 python 解析 xsd 并从中创建 XML?
- regex - 在 Google Data Studio 中提取最后一个 / 之后的字符串(REGEXP 问题)
- r - R中的数值导数?
- elasticsearch - Elasticsearch 查询 geo_polygon 是否在多多边形内
- javascript - 基于来自 javascript 的变量的 HTML 中的条件复选标记或交叉标记
- javascript - 为什么我的 window.scroll 函数仅在声明变量时触发两次?
- javascript - 重新加载页面时,localStorage 返回 undefined
- python - 如何进行下面的稀疏矩阵减法?
- docker - 在 Dockerfile 中下载并解压文件