alandefreitas / matplotplusplus

Matplot++: A C++ Graphics Library for Data Visualization 📊🗾
https://alandefreitas.github.io/matplotplusplus/
MIT License
4.11k stars 313 forks source link

Bundled CImg compile error C2220 #210

Closed aliaksei135 closed 2 years ago

aliaksei135 commented 2 years ago

Bug category

Describe the bug Compile warning treated as error resulting in failure to compile. See below

Steps to Reproduce Including in my CMake project using FetchContent as per the following snippet

cmake_minimum_required(VERSION 3.16)
set(CMAKE_CXX_STANDARD 17)
...
    set(USE_SYSTEM_CIMG ON) #does not make a difference
    FetchContent_Declare(
        matplotplusplus
        GIT_REPOSITORY https://github.com/alandefreitas/matplotplusplus
        GIT_TAG v1.1.0 #origin/master results in different errors as well as this same one
    )
    FetchContent_GetProperties(matplotplusplus)
    if(NOT matplotplusplus_POPULATED)
        FetchContent_Populate(matplotplusplus)
        add_subdirectory(${matplotplusplus_SOURCE_DIR} ${matplotplusplus_BINARY_DIR} EXCLUDE_FROM_ALL)
    endif()
    target_link_libraries(${PROJECT_NAME} PUBLIC Matplot++::matplot)
...

Output

CMake configuration output: ```console 1> [CMake] Setting CXX flags to default for Debug mode (/DWIN32 /D_WINDOWS /GR /EHsc /O0) 1> [CMake] -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 1> [CMake] -- Could NOT find LAPACK (missing: LAPACK_LIBRARIES) 1> [CMake] -- Could NOT find PkgConfig (missing: PKG_CONFIG_EXECUTABLE) 1> [CMake] Warning: std::filesystem doesn't set INTERFACE_INCLUDE_DIRECTORIES. No include_directories set. 1> [CMake] Setting matplotplusplus compiler options ``` Compiler output: ```console C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(57158): error C2220: the following warning is treated as an error C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(57094): note: while compiling class template member function 'const cimg_library::CImg &cimg_library::CImg::_save_png(FILE *const ,const char *const ,const unsigned int) const' C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(57085): note: see reference to function template instantiation 'const cimg_library::CImg &cimg_library::CImg::_save_png(FILE *const ,const char *const ,const unsigned int) const' being compiled C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(65349): note: see reference to class template instantiation 'cimg_library::CImg' being compiled C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(57158): warning C4611: interaction between '_setjmp' and C++ object destruction is non-portable C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(52535): warning C4324: 'cimg_library::CImg::_cimg_error_mgr': structure was padded due to alignment specifier C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(52565): note: see reference to class template instantiation 'cimg_library::CImg::_cimg_error_mgr' being compiled C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(52549): note: while compiling class template member function 'cimg_library::CImg &cimg_library::CImg::_load_jpeg(FILE *const ,const char *const )' C:\Users\Aliak\.conan\data\cimg\2.9.9\_\_\package\5ab84d6acfe1f23c4fae0ab88f26e3a396351ac9\include\CImg.h(52513): note: see reference to function template instantiation 'cimg_library::CImg &cimg_library::CImg::_load_jpeg(FILE *const ,const char *const )' being compiled ```

Platform

Environment Details:

Additional context

aliaksei135 commented 2 years ago

So not sure whether this was CImg or MSVC fault, but have worked around by just disabling the warnings that trigger the error. I have copied over CImg.h into my project extern folder and added around L134:

#pragma warning( disable : 4611 )
#pragma warning( disable : 4324 )

Then used this version as the system CImg:

set(WITH_SYSTEM_CIMG ON)
set(CIMG_INCLUDE_DIR "${EXTERN_DIR}/CImg")
target_include_directories(${PROJECT_NAME} PUBLIC ${CIMG_INCLUDE_DIR})

find_package(Matplot++ QUIET)
if(NOT Matplot++_FOUND)
    # Put your FetchContent or CPM.cmake script here
    FetchContent_Declare(
        matplotplusplus
        GIT_REPOSITORY https://github.com/alandefreitas/matplotplusplus
        GIT_TAG origin/master
    )
    FetchContent_GetProperties(matplotplusplus)
    if(NOT matplotplusplus_POPULATED)
        FetchContent_Populate(matplotplusplus)
        add_subdirectory(${matplotplusplus_SOURCE_DIR} ${matplotplusplus_BINARY_DIR} EXCLUDE_FROM_ALL)
    endif()
    target_link_libraries(${PROJECT_NAME} PUBLIC Matplot++::matplot)
endif()

Compiles fine after this.

alandefreitas commented 2 years ago

That's interesting. CImg is linked as a system library, so there should be no compilation errors or warnings.

https://github.com/alandefreitas/matplotplusplus/blob/3fd7593f3bf98ca60fdf3db17073ea8bbe161263/source/matplot/CMakeLists.txt#L104

This needs some debugging.