alandefreitas / matplotplusplus

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

Linking error with MSYS2 MinGW g++ #305

Closed andreadimax closed 1 year ago

andreadimax commented 1 year ago

Bug category

Describe the bug I have installed building from source succesfully Matplot++ through the guide at this link: https://alandefreitas.github.io/matplotplusplus/integration/install/build-from-source/build-and-install/ and I have checked all the dependencies. When I try to compile a simple example program with g++ I receive many linking errors and I don't understand how to solve. I read other related issues like #260 but I am not able to solve the same. I'd like to continue using g++. Can you help me?

Steps to Reproduce Trying to compile the following code:

#include <cmath>
#include <matplot/matplot.h>

int main() {
    using namespace matplot;
    std::vector<double> x = linspace(0, 2 * pi);
    std::vector<double> y = transform(x, [](auto x) { return sin(x); });

    plot(x, y, "-o");
    hold(on);
    plot(x, transform(y, [](auto y) { return -y; }), "--xr");
    plot(x, transform(x, [](auto x) { return x / pi - 1.; }), "-:gs");
    plot({1.0, 0.7, 0.4, 0.0, -0.4, -0.7, -1}, "k");

    show();
    return 0;
}

with the following command: g++ -I 'D:\Matplot++\include' -L 'D:\Matplot++\lib' -lmatplot -std=c++20 -g prova_matplot.cpp -o matplot

D:\Matplot++\ is the target folder for cmake install

Output This is the output:

D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\andre\AppData\Local\Temp\ccH3lQzD.o: in function `main':
D:\Documenti\Progetto tesi/prova_matplot.cpp:6: undefined reference to `matplot::linspace(double, double)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\Documenti\Progetto tesi/prova_matplot.cpp:7: undefined reference to `matplot::transform(std::vector<double, std::allocator<double> > const&, std::function<double (double)>)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\Documenti\Progetto tesi/prova_matplot.cpp:10: undefined reference to `matplot::hold(bool)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\Documenti\Progetto tesi/prova_matplot.cpp:11: undefined reference to `matplot::transform(std::vector<double, std::allocator<double> > const&, std::function<double (double)>)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:\Documenti\Progetto tesi/prova_matplot.cpp:12: undefined reference to `matplot::transform(std::vector<double, std::allocator<double> > const&, std::function<double (double)>)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\andre\AppData\Local\Temp\ccH3lQzD.o: in function `matplot::plot(std::vector<double, std::allocator<double> > const&, std::basic_string_view<char, std::char_traits<char> >)':
D:/Matplot++/include/matplot/freestanding/plot.h:52: undefined reference to `matplot::gca()'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Matplot++/include/matplot/freestanding/plot.h:52: undefined reference to `matplot::axes_type::plot(std::vector<double, std::allocator<double> > const&, std::basic_string_view<char, std::char_traits<char> >)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\andre\AppData\Local\Temp\ccH3lQzD.o: in function `matplot::show()':
D:/Matplot++/include/matplot/freestanding/plot.h:966: undefined reference to `matplot::gcf()'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Matplot++/include/matplot/freestanding/plot.h:966: undefined reference to `matplot::figure_type::show()'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\andre\AppData\Local\Temp\ccH3lQzD.o: in function `auto matplot::plot<std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >&, char const (&) [3]>(std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >&, char const (&) [3])':
D:/Matplot++/include/matplot/freestanding/plot.h:37: undefined reference to `matplot::gca()'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Matplot++/include/matplot/freestanding/plot.h:37: undefined reference to `matplot::axes_type::plot(std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&, std::basic_string_view<char, std::char_traits<char> >)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\andre\AppData\Local\Temp\ccH3lQzD.o: in function `auto matplot::plot<std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >, char const (&) [5]>(std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >&&, char const (&) [5])':
D:/Matplot++/include/matplot/freestanding/plot.h:37: undefined reference to `matplot::gca()'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Matplot++/include/matplot/freestanding/plot.h:37: undefined reference to `matplot::axes_type::plot(std::vector<double, std::allocator<double> > const&, std::vector<double, std::allocator<double> > const&, std::basic_string_view<char, std::char_traits<char> >)'
collect2.exe: error: ld returned 1 exit status

Platform

Environment Details:

alandefreitas commented 1 year ago

There are no instructions to build it with MSYS2 in https://alandefreitas.github.io/matplotplusplus/integration/install/build-from-source/build-and-install/

Maybe you need something like

cmake .. -D CMAKE_BUILD_TYPE=Release -D CMAKE_CXX_FLAGS="-O2" -D CMAKE_C_COMPILER=/path/to/mingw    -D CMAKE_CXX_COMPILER=/path/to/mingw
andreadimax commented 1 year ago

I have built with the following command: cmake .. -G "MinGW Makefiles" -DCMAKE_BUILD_TYPE=Release -D CMAKE_CXX_FLAGS="-O2" -D CMAKE_C_COMPILER=P:/ath/to/gcc.exe -D CMAKE_CXX_COMPILER=P:/ath/to/g++.exe -D CMAKE_MAKE_PROGRAM=P:/ath/to/make.exe -DBUILD_EXAMPLES=OFF -DBUILD_TESTS=OFF and build process went correctly. MinGW compilers were recognized correctly. The problem is not in building but during linking

andreadimax commented 1 year ago

@alandefreitas I have repeated the same operations with the version 11.2.0 of MinGW g++ compiler and it all works fine. I have executed the same previous command (except for specifying CMAKE variable to that version of compiler) and then built. So I think the problem it is just the 12.2.0 version.

Steps to reproduce: If you build with the command in my previous comment (with version 12.2.0) of compiler and then execute: cmake --build . --parallel 8 --config Release build process is ok until 100% and then you obtain a tons of underfined reference.

NOTE: c++ standard I specified is 17

A little extract:

[ 86%] Building CXX object matplotplusplus/source/matplot/CMakeFiles/matplot.dir/axes_objects/surface.cpp.obj
[ 88%] Building CXX object matplotplusplus/source/matplot/CMakeFiles/matplot.dir/freestanding/axes_functions.cpp.obj
[ 91%] Building CXX object matplotplusplus/source/matplot/CMakeFiles/matplot.dir/axes_objects/vectors.cpp.obj
[ 93%] Building CXX object matplotplusplus/source/matplot/CMakeFiles/matplot.dir/freestanding/histcounts.cpp.obj
[ 95%] Linking CXX static library libmatplot.a
[ 95%] Built target matplot
[ 97%] Building CXX object CMakeFiles/prova_matplot.dir/prova_matplot.cpp.obj
[100%] Linking CXX executable prova_matplot.exe
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text+0xd8): undefined reference to `std::ios_base::Init::~Init()'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.xdata$_ZN7matplot4plotISt6vectorIdSaIdEEJS3_RA5_KcEEEDaT_DpOT0_+0x10): undefined reference to `__gxx_personality_seh0'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text$_ZNSt6vectorIdSaIdEEC1ERKS1_[_ZNSt6vectorIdSaIdEEC1ERKS1_]+0x38): undefined reference to `operator new(unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text$_ZNSt6vectorIdSaIdEEC1ERKS1_[_ZNSt6vectorIdSaIdEEC1ERKS1_]+0x89): undefined reference to `std::__throw_bad_array_new_length()'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.rdata+0x10): undefined reference to `vtable for __cxxabiv1::__class_type_info'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.rdata+0x30): undefined reference to `vtable for __cxxabiv1::__class_type_info'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.rdata+0x50): undefined reference to `vtable for __cxxabiv1::__class_type_info'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x157): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x1fc): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x219): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x2ca): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x2e7): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x363): undefined reference to `operator new(unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x442): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x4a6): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x4bd): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x603): undefined reference to `operator delete(void*, unsigned long long)'
D:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles\prova_matplot.dir/objects.a(prova_matplot.cpp.obj):prova_matplot.:(.text.startup+0x61a): undefined reference to `operator delete(void*, unsigned long long)'

Hope this can help. Let me know if I have to consider issue closed

alandefreitas commented 1 year ago

Great. I'm glad it worked.