2013-10-10 49 views
1

我想用MinGW在Windows上构建一个基本的FANN(快速人工神经网络)项目。但是,每当我尝试链接可执行文件时,我都会遇到一堆错误,包括undefined reference to错误。有趣的是,如果我根本不链接图书馆,我会得到更多的错误,这意味着至少有一些图书馆正在工作。对于我试图编译和链接文件中的代码是:使用FANN链接错误

#include "doublefann.h" 

int main() { 
    const unsigned int num_input_neurons = 9; 
    const unsigned int num_output_neurons = 1; 
    const unsigned int num_layers = 3; 
    const unsigned int num_hidden_neurons = 9; 
    const float desired_error = (const float) 0; 
    const unsigned int max_epochs = 500000; 
    const unsigned int epochs_between_reports = 1000; 

    struct fann *ann = fann_create_standard(num_layers, 
              num_input_neurons, 
              num_hidden_neurons, 
              num_output_neurons); 

    fann_set_activation_function_hidden(ann, FANN_SIGMOID_SYMMETRIC); 
    fann_set_activation_function_output(ann, FANN_SIGMOID_SYMMETRIC); 

    fann_train_on_file(ann, 
         "titanic-training.data", 
         max_epochs, 
         epochs_between_reports, 
         desired_error); 

    fann_save(ann, "titanic.net"); 

    fann_destroy(ann); 

    return 0; 
} 

和我使用的编译和链接命令是:

gcc -Wall -Ifann\src\include titanic-train.c -Lfann\bin -lfanndouble -o titanic-train.exe 

我恢复的错误是:

C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x7f): undefined reference to `fann_set_activation_function_hidden'   
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x93): undefined reference to `fann_set_activation_function_output'   
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xbf): undefined reference to `fann_train_on_file'        
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xd3): undefined reference to `fann_save'          
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xdf): undefined reference to `fann_destroy'         
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o: bad reloc address 0x64 in section `.rdata'                             
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation     
collect2.exe: error: ld returned 1 exit status 

如果我没有在所有的库链接,而不是我得到:

C:\Users\kunkelwe\AppData\Local\Temp\ccyOO3jL.o:titanic-train.c:(.text+0x67): undefined reference to `fann_create_standard' 
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x7f): undefined reference to `fann_set_activation_function_hidden'   
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0x93): undefined reference to `fann_set_activation_function_output'   
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xbf): undefined reference to `fann_train_on_file'        
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xd3): undefined reference to `fann_save'          
C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o:titanic-train.c:(.text+0xdf): undefined reference to `fann_destroy'         
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: C:\Users\kunkelwe\AppData\Local\Temp\ccsWQg66.o: bad reloc address 0x64 in section `.rdata'                             
c:/fragileprograms/mingw-native/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/bin/ld.exe: final link failed: Invalid operation     
collect2.exe: error: ld returned 1 exit status 

编辑:

按Haroogan的要求,我跑了nm fanndouble.lib。输出是相当广泛的,所以,而不是粘贴在这里,我已经通过pastebin在这里:http://pastebin.com/raw.php?i=vybFhEcX

我不熟悉nm,但它似乎缺少的符号确实存在于文件中。

编辑#2:

doublefann.h的内容是:http://pastebin.com/mrHKJi8C

和fann.h,它包括的内容是:http://pastebin.com/gTrHCYAg

请问问题仅仅是通过使用MinGW重新编译库来解决?

编辑#3:

进行更改是Haroogan提示工作!除了这些变化,我不得不通过增加修改FANN的文件的CMakeLists.txt:

if (WIN32) 
ADD_DEFINITIONS(-DFANN_DLL_EXPORTS) 
endif (WIN32) 

然后,在项目的根目录运行cmake -G "MinGW Makefiles"然后mingw32-make制作的文件,libdoublefann.dll,当链接并包含在.exe目录中,允许我,终于,运行我的程序。

+0

运行'nm libfanndouble。a',并查看缺失的符号是否实际上在库中定义。完成后用结果更新您的问题,以便我们可以考虑下一步该做什么。 –

+0

@Haroogan:完成。这是fanndouble.lib,顺便说一下,不是libfanndouble.a –

+0

这是** [name mangling](http://en.wikipedia.org/wiki/Name_mangling)**的经典问题,这在编译器中是不同的。显然,'fanndouble.lib'和'fanndouble.dll'都是由MSVC编译器生成的,因此现在由于不同的名称约束约定而无法直接与MinGW链接。但别担心,我们会解决它。首先,让我们看看你的'floatfann.h'。 –

回答

1

doublefann.h就行#116:

#if (_MSC_VER > 1300) 

变化:

#if (_MSC_VER > 1300) || defined(__MINGW32__) || defined(__MINGW64__) 

此外,上线#121:

#if defined(_MSC_VER) && (defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS)) 

变化:

#if (defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)) && \ 
    (defined(FANN_USE_DLL) || defined(FANN_DLL_EXPORTS)) 
+0

谢谢。我遇到了MinGW的一个(可能)无关的问题,所以在我能做到这一点之前可能还有一段时间。 –