Luc Archambault
Luc Archambault

Reputation: 1

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

I am currently trying to implement the cimg library in a project also using fftw, libtiff, Jsoncpp and openMP libraries. Every library has been added to the .pro with the right path. I am making this project on QtCreator, using MacOS High Sierra 10.13.6 version.

Here is a copy of the .pro file :

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

However, when I try to compile, the CImg header returns lots of errors every time std::min or std::max is called, that is to say a thousand times, with the following error missage :

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

I think there might be a conflict between math and Cimg libraries, as those lines in the Cimg code let me think :

// 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

However, I still have not been able to solve this issue, and that is why I ask you what could be the source of it..

In order to solve the issue, I tried to make a MRE and reproduce the same error. Here is the code :

//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;
}

This code compile finely, with no issues at all. I added every library used in Cimg.h, plus the ones I use in the main of my project.

However, I had to comment 3 libraries to make it work : cmath, random and complex, with cmath being used in cimg.h (cimh.h not being included for now). If I do not do that, the following error occurs :

:-1: error : [main.o] Error 1

with a few messages which redirect to the files that have been commented in the MRE above. What could be the reason of such an error by only including headers ?

I do not know if it is the reason of the std::min errors I first spoke of though. Indeed, I think those ones are related to the cimg_library namespace of cimg.h, which has not been added to the headers of this MRE.

Upvotes: 0

Views: 563

Answers (1)

Ted Lyngmo
Ted Lyngmo

Reputation: 117308

I'm making a guess that you have using namespace cimg_library::cimg; somewhere in your project.

From the cimg_library::cimg namespace documentation:

Warning Never write using namespace cimg_library::cimg; in your source code. Lot of functions in the cimg:: namespace have the same names as standard C functions that may be defined in the global namespace ::

There are a lot of min and max overloads in the cimg namespace.

Upvotes: 1

Related Questions