2014-10-10 61 views
1

我對C++和犰狳很陌生,並且遇到下面描述的構建錯誤。我想測試以下簡單的代碼保存一個犰狳矩陣HDF5文件:使用犰狳和hdf5庫的簡單代碼的C++構建錯誤

#include <iostream> 
#include <armadillo> 

using namespace std; 
using namespace arma; 

int main() 
{ 
    mat A = randu<mat>(240,320); 
    A.save("A.hdf5",hdf5_binary); 

    return 0; 
} 

編譯時,我得到以下錯誤:

/usr/include/armadillo_bits/hdf5_misc.hpp:131: undefined reference in « arma_H5T_NATIVE_DOUBLE » 
/usr/include/armadillo_bits/hdf5_misc.hpp:131: undefined reference in « arma_H5Tcopy » 
obj/Debug/main.o: in function « bool arma::diskio::save_hdf5_binary<double> (arma::Mat<double> const&, std::string const&) »: 
/usr/include/armadillo_bits/diskio_meat.hpp:1299: undefined reference in « arma_H5Eset_auto » 
/usr/include/armadillo_bits/diskio_meat.hpp:1308: undefined reference in « arma::H5check_version(unsigned int, unsigned int, unsigned int) » 
/usr/include/armadillo_bits/diskio_meat.hpp:1308: undefined reference in « arma_H5Fcreate » 
/usr/include/armadillo_bits/diskio_meat.hpp:1315: undefined reference in « arma_H5Screate_simple » 
/usr/include/armadillo_bits/diskio_meat.hpp:1324: undefined reference in « arma_H5Dcreate » 
/usr/include/armadillo_bits/diskio_meat.hpp:1330: undefined reference in « arma_H5Dwrite » 
/usr/include/armadillo_bits/diskio_meat.hpp:1333: undefined reference in « arma_H5Dclose » 
/usr/include/armadillo_bits/diskio_meat.hpp:1334: undefined reference in « arma_H5Tclose » 
/usr/include/armadillo_bits/diskio_meat.hpp:1335: undefined reference in « arma_H5Sclose » 
/usr/include/armadillo_bits/diskio_meat.hpp:1336: undefined reference in « arma_H5Fclose » 

彙編指令如下:

g++ -Wall -fexceptions -O2 -g -larmadillo -lhdf5 -c main.cpp -o main 

我正在使用Linux Fedora 20系統上的CodeBlocks。我有打包的HDF5-devel,可以在/usr/include/ 中找到hdf5.h,我也用#define ARMA_USE_HDF5config.hpp中激活了犰狳中的hdf5。我使用最新版本的犰狳(4.450)和gcc 4.8.3。

我錯過了一個鏈接?對我而言,添加-larmadillo和-lhdf5(如在犰狳的用戶指南中所述)應該足夠了。任何線索? 謝謝

+0

可能有[g ++鏈接順序依賴關係nking c代碼到c + +代碼](http://stackoverflow.com/questions/3363398/g-linking-order-dependency-when-linking-c-code-to-c-code) – 2014-10-10 09:21:15

+0

我投了這個關閉作爲重複,鏈接的問題應該告訴你該怎麼做,爲什麼(改變'-laramdillo'和'-lhdf5'的順序)。無論哪種方式,我會稱這是一個在犰狳問題,因爲它取決於libhdf5,但沒有鏈接它。 – 2014-10-10 09:25:28

+0

我明白了你的觀點,並且你鏈接的帖子的確很有意思,但是我懷疑有訂單問題,並且在發帖前檢查了這一點。問題實際上是不同的(見下面的答案)。 – JColin 2014-10-10 15:18:15

回答

1

由於基於Linux的系統上HDF5庫版本不同的問題,Armadillo的作者已禁用HDF5庫的自動檢測功能。如果你想在Armadillo中使用HDF5,有兩種選擇:

1. 解壓armadillo .tar.gz包並編輯CMakeLists.txt文件。取消註釋第231至238行,從find_package(HDF5)開始(即刪除#個字符)。修改CMakeLists.txt後,請按照README.txt文件中所述運行基於cmake的安裝。

OR

2. 安裝犰狳(通常不修改的CMakeLists.txt),然後使用(都在同一行)編譯程序:

g++ main.cpp -o main -O2 -DARMA_DONT_USE_WRAPPER -DARMA_USE_BLAS -DARMA_USE_LAPACK -DARMA_USE_HDF5 -lblas -llapack -lhdf5 

加分點: 如果你有安裝了高速OpenBLAS庫並希望Armadillo使用它來代替標準BLAS,將-lblas更改爲-lopenblas

+1

非常感謝,我試過選項2,它工作正常。我現在就按照推薦的方式給我們openblas。 – JColin 2014-10-10 15:01:20