You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CMake编译matplot++时出现__imp__dupenv_s未定义引用错误求助

编译matplot++时MinGW链接错误:undefined reference to __imp__dupenv_s

问题场景

我尝试在Windows+MinGW环境下编译matplot++库(无适配该环境的预编译版本),之前曾成功用CMake编译过OpenCV,但本次CMake配置与生成操作均显示完成,执行cmake --build .或mingw32-make install构建到4%时出现链接错误。

错误信息

C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ../../../source/matplot/libmatplot.a(gnuplot.cpp.obj):gnuplot.cpp:(.text+0x506d): undefined reference to `__imp__dupenv_s'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [examples\line_plot\plot\CMakeFiles\example_plot_1.dir\build.make:101: examples/line_plot/plot/example_plot_1.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:4006: examples/line_plot/plot/CMakeFiles/example_plot_1.dir/all] Error 2
mingw32-make: *** [Makefile:165: all] Error 2

环境信息

  • CMake版本:3.26.3
  • g++ (Rev10, Built by MSYS2 project) 12.2.0
  • gcc (Rev10, Built by MSYS2 project) 12.2.0

完整构建输出

C:\Users\Daniele\Desktop\matplotplusplus-1.1.0\build>mingw32-make install
[  0%] Building CXX object source/3rd_party/CMakeFiles/nodesoup.dir/nodesoup/src/algebra.cpp.obj
[  0%] Building CXX object source/3rd_party/CMakeFiles/nodesoup.dir/nodesoup/src/fruchterman_reingold.cpp.obj
[  0%] Building CXX object source/3rd_party/CMakeFiles/nodesoup.dir/nodesoup/src/kamada_kawai.cpp.obj
[  0%] Building CXX object source/3rd_party/CMakeFiles/nodesoup.dir/nodesoup/src/layout.cpp.obj
[  0%] Building CXX object source/3rd_party/CMakeFiles/nodesoup.dir/nodesoup/src/nodesoup.cpp.obj
[  1%] Linking CXX static library libnodesoup.a
[  1%] Built target nodesoup
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/backend/backend_interface.cpp.obj
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/backend/gnuplot.cpp.obj
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/backend/backend_registry.cpp.obj
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/core/axes_type.cpp.obj
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/core/axes_object.cpp.obj
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/core/axis_type.cpp.obj
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/core/figure_type.cpp.obj
[  1%] Building CXX object source/matplot/CMakeFiles/matplot.dir/core/figure_registry.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/core/legend.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/core/line_spec.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/util/colors.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/util/common.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/util/contourc.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/util/world_cities.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/util/world_map_10m.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/util/world_map_50m.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/util/world_map_110m.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/bars.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/box_chart.cpp.obj
[  2%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/circles.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/contours.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/error_bar.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/filled_area.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/function_line.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/histogram.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/labels.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/line.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/matrix.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/network.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/parallel_lines.cpp.obj
[  3%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/stair.cpp.obj
[  4%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/string_function.cpp.obj
[  4%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/surface.cpp.obj
[  4%] Building CXX object source/matplot/CMakeFiles/matplot.dir/axes_objects/vectors.cpp.obj
[  4%] Building CXX object source/matplot/CMakeFiles/matplot.dir/freestanding/axes_functions.cpp.obj
[  4%] Building CXX object source/matplot/CMakeFiles/matplot.dir/freestanding/histcounts.cpp.obj
[  4%] Linking CXX static library libmatplot.a
[  4%] Built target matplot
[  4%] Building CXX object test/generate_examples/CMakeFiles/generate_examples.dir/main.cpp.obj
[  4%] Linking CXX executable generate_examples.exe
[  4%] Built target generate_examples
[  4%] Building CXX object examples/line_plot/plot/CMakeFiles/example_plot_1.dir/plot_1.cpp.obj
[  4%] Linking CXX executable example_plot_1.exe
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: ../../../source/matplot/libmatplot.a(gnuplot.cpp.obj):gnuplot.cpp:(.text+0x506d): undefined reference to `__imp__dupenv_s'
collect2.exe: error: ld returned 1 exit status
mingw32-make[2]: *** [examples\line_plot\plot\CMakeFiles\example_plot_1.dir\build.make:101: examples/line_plot/plot/example_plot_1.exe] Error 1
mingw32-make[1]: *** [CMakeFiles\Makefile2:4006: examples/line_plot/plot/CMakeFiles/example_plot_1.dir/all] Error 2
mingw32-make: *** [Makefile:165: all] Error 2

错误原因

__imp__dupenv_s是微软MSVC编译器特有的环境变量获取函数,MinGW的GCC标准库中并未提供该符号的实现。matplot++的gnuplot.cpp源码中直接调用了_dupenv_s,导致MinGW链接阶段无法找到对应的函数定义,从而报错。

解决方法

方法一:修改源码添加跨平台兼容逻辑(推荐)

  1. 打开matplot++源码目录下的source/matplot/backend/gnuplot.cpp文件
  2. 找到调用_dupenv_s的代码段(通常是用于读取系统环境变量的部分)
  3. 添加条件编译,针对MSVC和MinGW分别使用对应的环境变量获取函数:
#ifdef _MSC_VER
    // MSVC环境下保留原有_dupenv_s逻辑
    errno_t err = _dupenv_s(&path, &len, "PATH");
    // 原有错误处理和内存管理逻辑
#else
    // MinGW环境下使用标准C的getenv函数
    const char* path = getenv("PATH");
    // 注意:getenv返回的内存由系统管理,无需手动释放,按需调整后续逻辑
#endif
  1. 重新执行CMake配置和构建命令:
cmake .. -G "MinGW Makefiles"
mingw32-make install

方法二:强制链接MSVC运行时库(不推荐,存在兼容性风险)

在CMake配置阶段添加链接选项,强制链接MSVC的msvcrt库:

cmake .. -G "MinGW Makefiles" -DCMAKE_EXE_LINKER_FLAGS="-lmsvcrt"

之后再执行mingw32-make install完成构建。


内容的提问来源于stack exchange,提问作者puccj

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.24 16:07:00