0

我目前正在尝试在一个项目中实现 cimg 库,还使用 ​​fftw、libtiff、Jsoncpp 和 openMP 库。每个库都已使用正确的路径添加到 .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 的标头中。

4

1 回答 1

1

我猜测using namespace cimg_library::cimg;您的项目中有某个地方。

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

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

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

于 2020-06-24T11:05:22.017 回答