當我在自己的庫中使用它時,我遇到了一些與犰狳有關的麻煩。我安裝了Armadillo,並創建了自己的庫,它調用了svd_econ方法。這是CMakeList.txt文件:犰狳鏈接錯誤,只有當我在自己的庫中使用它
cmake_minimum_required (VERSION 2.8.9)
set(PROJECTNAME training_library)
project(${PROJECTNAME})
if (NOT DEFINED ENV{EIGEN_ROOT})
message(FATAL_ERROR "Could not find EIGEN_ROOT environment variable")
endif()
find_package(Armadillo REQUIRED)
include_directories("$ENV{EIGEN_ROOT}")
add_definitions(-g)
add_definitions(-O3)
add_definitions(-DNDEBUG)
include_directories(${ARMADILLO_INCLUDE_DIRS})
include_directories(${PROJECT_SOURCE_DIR}/include)
file(GLOB header include/*.h)
file(GLOB source src/*.cpp)
source_group("Source Files" FILES ${source})
source_group("Header Files" FILES ${header})
add_library(${PROJECTNAME} ${source} ${header})
target_link_libraries(${PROJECTNAME} ${ARMADILLO_LIBRARIES})
這是頭文件:
#include <algorithm>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>
#include <armadillo>
#include <Eigen/Dense>
#include <Eigen/SVD>
namespace statistics
{
bool findPCS(const Eigen::MatrixXd &data, Eigen::VectorXd &eigenvalues, Eigen::MatrixXd &eigenvectors);
}
#endif
,這是在.cpp:
#include "statistics.h"
namespace statistics
{
Eigen::MatrixXd convert(const arma::mat &data)
{
Eigen::MatrixXd res(data.n_rows,data.n_cols);
for (int i = 0; i < data.n_rows; i++)
{
for (int j = 0; j < data.n_cols; j++)
{
res(i,j) = data(i,j);
}
}
return res;
}
arma::mat convert(const Eigen::MatrixXd &data)
{
arma::mat res(data.rows(),data.cols());
for (int i = 0; i < data.rows(); i++)
{
for (int j = 0; j < data.cols(); j++)
{
res(i,j) = data(i,j);
}
}
return res;
}
Eigen::VectorXd convert(const arma::vec &data)
{
Eigen::VectorXd res(data.size());
for (int i = 0; i < data.size(); i++)
{
res(i) = data(i);
}
return res;
}
arma::vec convert(const Eigen::VectorXd &data)
{
arma::vec res(data.size());
for (int i = 0; i < data.size(); i++)
{
res(i) = data(i);
}
return res;
}
bool findPCS(const Eigen::MatrixXd &data, Eigen::VectorXd &eigenvalues, Eigen::MatrixXd &eigenvectors)
{
arma::mat arma_mat = convert(data);
arma::mat U; arma::vec S; arma::mat V;
arma::svd_econ(U,S,V,arma_mat);
eigenvalues = convert(S)/sqrt((double)(data.rows()));
eigenvectors = convert(V);
return true;
}
}
現在,這個庫編譯沒有任何問題。然後,我想從另一個名爲clean_data的模塊中調用findPCS方法。它的的CMakeLists.txt文件如下:
cmake_minimum_required (VERSION 2.8.9)
set(PROJECTNAME clean_data)
project(${PROJECTNAME})
find_package(OpenCV REQUIRED)
find_package(Armadillo REQUIRED)
if (NOT DEFINED ENV{EIGEN_ROOT})
message(FATAL_ERROR "Could not find EIGEN_ROOT environment variable")
endif()
include_directories("$ENV{EIGEN_ROOT}")
include_directories("$ENV{training_INCLUDE}")
link_directories("$ENV{training_LIBS}")
add_definitions(-g)
add_definitions(-O3)
add_definitions(-DNDEBUG)
file(GLOB SOURCES *.c *.cpp)
add_executable(${PROJECTNAME} ${SOURCES})
target_link_libraries(${PROJECTNAME} ${OpenCV_LIBS} ${ARMADILLO_LIBRARIES} training_library)
它調用findPCS方法是如下的main.cpp:
#include <armadillo>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <iostream>
#include <fstream>
#include <ctime>
#include <algorithm>
#include <numeric>
#include <list>
#include <statistics.h>
#include <opencv2/opencv.hpp>
#include <Eigen/Dense>
int main() {
Eigen::MatrixXd matrix(4,10);
matrix << 1, 2, 2, 1, 5, 5, 4, 12, 32, 1,
44, 34, 12, 11, 21, 5, 54, 1, 12, 3,
33, 128, 112, 126, 3, 3, 2, 12, 1, 54,
66, 34, 1, 54, 2, 2, 1, 43, 4, 12;
Eigen::VectorXd eigenval; Eigen::MatrixXd eigenvec;
statistics::findPCS(matrix,eigenval,eigenvec);
//arma::mat x = arma::randu<arma::mat>(3,4);
//arma::mat U; arma::vec S; arma::mat V;
//arma::svd(U,S,V,x);
return 0;
}
這段代碼,當我嘗試編譯,使我這個連接錯誤:
/home/ilaria/Dev/training_library/build/libtraining_library.a(statistics.cpp.o): In function `gesdd<double>':
/usr/local/include/armadillo_bits/wrapper_lapack.hpp:571: undefined reference to `wrapper_dgesdd_'
/home/ilaria/Dev/training_library/build/libtraining_library.a(statistics.cpp.o): In function `gesvd<double>':
/usr/local/include/armadillo_bits/wrapper_lapack.hpp:506: undefined reference to `wrapper_dgesvd_'
/usr/local/include/armadillo_bits/wrapper_lapack.hpp:506: undefined reference to `wrapper_dgesvd_'
collect2: error: ld returned 1 exit status
make[2]: *** [clean_data] Error 1
make[1]: *** [CMakeFiles/clean_data.dir/all] Error 2
make: *** [all] Error 2
不過,如果我decomment了在我的main.cpp中註釋行,代碼編譯沒有任何問題,它運行良好,它給了我正確的結果!我查看了互聯網,找不到任何有用的答案。我使用的是Ubuntu 14.04。我從源頭安裝了犰狳,並從突觸中安裝了lapack,blas和openblas。任何人都可以在任何地方找到問任何幫助將非常感激。
感謝, Ilaria的
聽起來像一個鏈接順序問題。你能分享生成的鏈接器命令行嗎? – Smeeheey