2014-01-14 41 views
0

我想編譯一個我爲了在MATLAB中運行預先存在的C程序而編寫的mex文件。我收到以下錯誤:編譯MEX文件時出現故障鏈接

>> 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在我的代碼。我如何讓編譯器找到它們?

我使用Xcode版本4.6.3在Mac OS X 10.8上運行MATLAB R2013a。

感謝您的任何建議!

UPDATE1:

@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發行版的一部分。有任何想法嗎?謝謝。

+0

你在'src0'文件夾中有哪些庫(編譯過的文件)? – Shai

+0

您「#include」的函數是否被編譯到共享庫中?他們的源代碼在哪裏?你有編譯他們的源代碼嗎? – Shai

+0

#included函數的源代碼位於子文件夾「include」中,我在命令行中用'-Iinclude'指出。我需要單獨編譯它們嗎?我認爲mex會自動編譯任何#included源代碼以及主函數。 – dannyhmg

回答

0

答案由Amro提供 - 有必要指向庫的位置(在本例中使用-L/usr/local/lib標誌)並告訴編譯器鏈接到它(使用標誌-lmpfr)。感謝Shai幫助解決這個問題。