【问题标题】:Trouble Linking when compiling a MEX file编译 MEX 文件时出现链接问题
【发布时间】:2014-01-14 16:24:31
【问题描述】:

我正在尝试编译我编写的 mex 文件,以便在 MATLAB 中运行预先存在的 C 程序。我收到以下错误:

>> mex src/main.c -Iinclude -I/Users/my_name/mpfr-3.1.1/src/ -I/Users/my_name/gmp-5.0.5 -Lsrc0 -output cpdetect_c
Undefined symbols for architecture x86_64:
  "_binomial_main", referenced from:
      _cpdetect in main.o
  "_gaussian1_main", referenced from:
      _cpdetect in main.o
  "_gaussianU_main", referenced from:
      _cpdetect in main.o
  "_mpfr_clears", referenced from:
      _cpdetect in main.o
  "_mpfr_exp10", referenced from:
      _cpdetect in main.o
  "_mpfr_inits2", referenced from:
      _cpdetect in main.o
  "_mpfr_set_d", referenced from:
      _cpdetect in main.o
  "_poisson_main", referenced from:
      _cpdetect in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

我很困惑,因为这些函数都是我在代码中#Include 的文件的一部分。如何让编译器找到它们?

我在 Mac OS X 10.8 上运行 MATLAB R2013a,Xcode 版本为 4.6.3。

感谢您的建议!

更新1:

@Shai:我的 src0 文件夹的内容:

>> ls src0
Makefile            gaussian1.o         test_gaussian1.c        test_tools.c
Makefile.am         loadtraj.c          test_gaussian1_point_calc.c test_tools.o
Makefile.in         loadtraj.o          test_load           tools.c
binomial.c          main.c              test_load.c         tools.o
binomial.o          main.o              test_load.o
cpdetect            poisson.c           test_poisson.c
gaussian1.c         poisson.o           test_tools

这个 C 程序(最初打算在命令行上运行)来自一个朋友,并且已经存在 src0 文件夹。我在写mex函数的时候修改了'src'文件夹中的代码文件,但是没有碰src0文件夹。

更新 2: 我意识到我指向了 mpfr 的错误位置。我现在正在使用以下编译命令:

>> mex src/main.c src/binomial.c src/gaussian1.c src/gaussianU.c src/poisson.c -Iinclude -I/usr/local/include -lmpfr -output cpdetect_c

它给出了以下错误:

Undefined symbols for architecture x86_64:
  "_mpfr_mul_d", referenced from:
      _gaussian1_calc_constant_part in gaussian1.o
      _gaussianU_calc_constant_part in gaussianU.o
  "_mpfr_printf", referenced from:
      _find_gaussian1_change_point in gaussian1.o
      _gaussian1_point_calc in gaussian1.o
      _gaussianU_calc_constant_part in gaussianU.o
      _find_gaussianU_change_point in gaussianU.o
      _find_poisson_change_point in poisson.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

    mex: link of ' "cpdetect_c.mexmaci64"' failed.

这很奇怪。并不是编译器找不到任何 mpfr 函数——它似乎可以毫无问题地找到许多其他函数(如果我故意省略 -lmpfr 标志,我会得到一个更长的未定义符号列表) .我确实确认 mpfr_mul_d 和 mpfr_printf 是我安装的 mpfr 发行版的一部分。有任何想法吗?谢谢。

【问题讨论】:

  • 'src0' 文件夹中有哪些库(已编译的 so 文件)?
  • 你“#include”的函数,它们编译成共享库了吗?他们的源代码在哪里?你编译过他们的源代码吗?
  • #included 函数的源代码位于子文件夹“include”中,我在命令行中使用“-Iinclude”指向该子文件夹。我需要单独编译它们吗?我认为 mex 会自动编译任何 #included 源以及 main 函数。
  • 试试mex src/main.c src/binomial.c src/gaussian1.c src/poisson.c src/tools.c -I/Users/my_name/mpfr-3.1.1/src/ -I/Users/my_name/gmp-5.0.5 -output cpdetect_c
  • @Shai:谢谢,这有帮助 - 现在它找到了我所有的源文件,它只是在 mpfr 功能上有问题。我如何链接到那些?我尝试将 -L/Users/my_name/mpfr-3.1.1/src/ 添加到您上面的命令中,但没有帮助。

标签: c matlab compiler-errors linker mex


【解决方案1】:

答案由 Amro 提供 - 必须同时指向库的位置(在这种情况下使用标志 -L/usr/local/lib)并告诉编译器链接到它(使用标志-lmpfr)。还要感谢 Shai 帮助解决此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多