使用 CImg 库的 std::min 和 std::max 的编译问题

Compilation issue with std::min and std::max using CImg library

本文关键字:std 编译 问题 max min CImg 库的 使用      更新时间:2023-10-16

我目前正在尝试使用fftw,libtiff,Jsoncpp和openMP库在项目中实现cimg库。每个库都已通过正确的路径添加到 .pro 中。我正在使用MacOS High Sierra 10.13.6版本在QtCreator上制作这个项目。

这是 .pro 文件的副本:

TEMPLATE = app
CONFIG += console c++11
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp 
HEADERS += 
cimg.h 
INCLUDEPATH += /usr/include
INCLUDEPATH += /opt/X11/include
INCLUDEPATH += /usr/local/include
INCLUDEPATH += /usr/local/Cellar/jsoncpp/1.9.3/include
INCLUDEPATH += /usr/local/Cellar/libtiff/4.1.0/include
INCLUDEPATH += /usr/local/Cellar/libomp/10.0.0/include
DEPENDPATH += /usr/include
LIBS += -L/usr/lib -lpthread -lm -ldl
LIBS += -L/opt/X11/lib -lX11
LIBS += /usr/local/lib/libfftw3.a
LIBS += -L/usr/local/Cellar/jsoncpp/1.9.3/lib -ljsoncpp
LIBS += -L/usr/local/Cellar/libtiff/4.1.0/lib -ltiff
LIBS += -L/usr/local/Cellar/libomp/10.0.0/lib -lomp

QMAKE_CXXFLAGS += -Xpreprocessor -fopenmp -O4 -J8
DISTFILES += 
config.json
copydata.commands = $(COPY_DIR) $$PWD/config.json $$OUT_PWD
first.depends = $(first) copydata
export(first.depends)
export(copydata.commands)
QMAKE_EXTRA_TARGETS += first copydata

但是,当我尝试编译时,每次调用 std::min 或 std::max 时,CImg 标头都会返回大量错误,也就是说一千次,错误未命中:

error : no member named 'max' in namespace 'std'
error : no member named 'min' in namespace 'std'

我认为数学和 Cimg 库之间可能存在冲突,因为 Cimg 代码中的那些行让我思考:

// Check if min/max/PI macros are defined.
//
// CImg does not compile if macros 'min', 'max' or 'PI' are defined,
// because it redefines functions min(), max() and const variable PI in the cimg:: namespace.
// so it '#undef' these macros if necessary, and restore them to reasonable
// values at the end of this file.
#ifdef min
#undef min
#define _cimg_redefine_min
#endif
#ifdef max
#undef max
#define _cimg_redefine_max
#endif
#ifdef PI
#undef PI
#define _cimg_redefine_PI
#endif

但是,我仍然无法解决这个问题,这就是为什么我问你它的来源是什么。

为了解决这个问题,我尝试进行MRE并重现相同的错误。这是代码:

//libraries used in my main
#include <json/value.h>
#include <json/json.h>
#include <fstream>
#include <iostream>
#include <tiffio.h>
#include <math.h>
//#include <complex>
//#include <random>
//#define cimg_use_fftw3
//#define cimg_use_tiff

//libraries used in Cimg.h
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
#include <cstring>
//#include <cmath>
#include <cfloat>
#include <climits>
#include <ctime>
#include <exception>
#include <algorithm>
//// Include OS-specific headers.
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include <fnmatch.h>
//// Look for C++11 features.
#if cimg_use_cpp11==1
#include <initializer_list>
#include <utility>
#endif
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <X11/keysym.h>
#include <pthread.h>
extern "C" {
#define uint64 uint64_hack_
#define int64 int64_hack_
#include "tiffio.h"
#undef uint64
#undef int64
}
extern "C" {
#include "fftw3.h"
}
int main()
{
std::cout << std::min(1,9999) << std::endl;
std::cout<<"hello world !" << std::endl;
return 0;
}

这段代码编译得很好,完全没有问题。我添加了 Cimg.h 中使用的每个库,以及我在项目主中使用的库。

但是,我不得不评论 3 个库才能使其工作:cmath,随机且复杂,cmath 用于 cimg.h (cimh.h 暂时不包括在内(。 如果我不这样做,则会发生以下错误:

:-1: 错误: [main.o] 错误 1

带有一些消息,这些消息重定向到上述MRE中已注释的文件。仅包含标头导致此类错误的原因可能是什么?

我不知道这是否是我首先谈到的std::min错误的原因。事实上,我认为这些与 cimg.h 的cimg_library命名空间有关,该命名空间尚未添加到此 MRE 的标头中。

我猜测你在项目中的某个地方using namespace cimg_library::cimg;

cimg_library::cimg命名空间文档中:

警告切勿在源代码中写入using namespace cimg_library::cimg;cimg:: namespace中的许多函数与可能在全局命名空间中定义的标准 C 函数具有相同的名称::

cimg命名空间中有很多minmax重载。