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

Win10 Mingw64环境下MathGL示例程序链接报错(未定义引用)

Win10/64位MSYS2 Mingw64环境下MathGL示例编译链接失败解决

问题现象

在Win10 64位系统的MSYS2 Mingw64环境编译MathGL示例程序时,链接阶段失败,提示MathGL相关函数未定义引用。

错误信息

$ make
g++ -o test test.o
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: test.o:test.c:(.text+0x1a): undefined reference to `__imp_mgl_create_graph'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: test.o:test.c:(.text+0x43): undefined reference to `__imp_mgl_fplot'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: test.o:test.c:(.text+0x61): undefined reference to `__imp_mgl_write_frame'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld.exe: test.o:test.c:(.text+0x71): undefined reference to `__imp_mgl_delete_graph'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:8: test] Error 1

原Makefile内容

CC=g++
CFLAGS= -I. -lmgl -lodbc

%.o: %.c
    $(CC) -c -o $@ $< $(CFLAGS)

test: test.o
    $(CC) -o test test.o

原示例代码

#include <mgl2/mgl_cf.h>

int main(void) {

    HMGL gr = mgl_create_graph(600,400);
    mgl_fplot(gr,"sin(pi*x)","","";
    mgl_write_frame(gr,"test.png","";
    mgl_delete_graph(gr);

    return 0;

};

解决方法

1. 修正Makefile的链接参数

-lmgl和-lodbc是链接阶段需要的库参数,不能放在CFLAGS里(CFLAGS仅用于编译阶段选项)。需单独用LDFLAGS存放链接选项,并在链接命令中引用:

修改后的Makefile:

CC=g++
CFLAGS= -I.
LDFLAGS= -lmgl -lodbc

%.o: %.c
    $(CC) -c -o $@ $< $(CFLAGS)

test: test.o
    $(CC) -o test test.o $(LDFLAGS)

2. 修正示例代码的语法错误

原代码中mgl_fplot和mgl_write_frame的调用存在语法错误(字符串后多了分号,函数调用括号未闭合),需修正:

修正后的代码:

#include <mgl2/mgl_cf.h>

int main(void) {

    HMGL gr = mgl_create_graph(600,400);
    mgl_fplot(gr,"sin(pi*x)","","");
    mgl_write_frame(gr,"test.png","");
    mgl_delete_graph(gr);

    return 0;

};

完成以上修改后,重新执行make即可正常编译链接生成可执行文件。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 03:44:54