Visual Studio 2017中Armadillo静态链接失败求助
我尝试在Visual Studio 2017的C++项目中静态链接Armadillo库,但编译失败,执行的步骤如下:
- 平台设置为x64;
- C/C++ -> 常规 -> 附加包含目录:
$(SolutionDir)Dependencies\include; - 源文件中添加
#include "armadillo"(也尝试过#include <armadillo>); - 链接器 -> 输入 -> 附加依赖项:
blas_win64_MT.lib; lapack_win64_MT.lib; - 链接器 -> 常规 -> 附加库目录:
$(SolutionDir)Dependencies\lib_win64;
注:include文件夹包含armadillo和armadillo_bits子文件夹;lib_win64文件夹包含blas_win64_MT.lib和lapack_win64_MT.lib文件。
编译时出现错误:
c:........\dependencies\include\armadillo_bits\arma_rng.hpp(444): error C2760: 语法错误:意外的标识符,应为';'
c:........\dependencies\include\armadillo_bits\arma_rng.hpp(524): note: 正在编译类模板实例化“arma::arma_rng::randn<std::complex<_Other>>”的引用
相关的库代码片段(arma_rng.hpp):
template<typename T> struct arma_rng::randn < std::complex<T> > { inline operator std::complex<T> () const { T a, b; //************line 444*************** arma_rng::randn<T>::dual_val(a, b); return std::complex<T>(a, b); } inline static void fill(std::complex<T>* mem, const uword N) { ... }; //************line 524***************
测试用的示例代码:
#include <iostream> #include <armadillo> using namespace std; using namespace arma; int main(int argc, char** argv) { cout << "Armadillo version: " << arma_version::as_string() << endl; mat A(2,3); cout << "A.n_rows: " << A.n_rows << endl; cout << "A.n_cols: " << A.n_cols << endl; A(1,2) = 456.0; A.print("A:"); A = 5.0; A.print("A:"); A.set_size(4,5); A.fill(5.0); A.print("A:"); // endr indicates "end of row" A << 0.165300 << 0.454037 << 0.995795 << 0.124098 << 0.047084 << endr << 0.688782 << 0.036549 << 0.552848 << 0.937664 << 0.866401 << endr << 0.348740 << 0.479388 << 0.506228 << 0.145673 << 0.491547 << endr << 0.148678 << 0.682258 << 0.571154 << 0.874724 << 0.444632 << endr << 0.245726 << 0.595218 << 0.409327 << 0.367827 << 0.385736 << endr; A.print("A:"); // determinant cout << "det(A): " << det(A) << endl; // inverse cout << "inv(A): " << endl << inv(A) << endl; // save matrix as a text file A.save("A.txt", raw_ascii); // load from file mat B; B.load("A.txt"); // submatrices cout << "B( span(0,2), span(3,4) ):" << endl << B( span(0,2), span(3,4) ) << endl; cout << "B( 0,3, size(3,2) ):" << endl << B( 0,3, size(3,2) ) << endl; cout << "B.row(0): " << endl << B.row(0) << endl; cout << "B.col(1): " << endl << B.col(1) << endl; // transpose cout << "B.t(): " << endl << B.t() << endl; // maximum from each column (traverse along rows) cout << "max(B): " << endl << max(B) << endl; // maximum from each row (traverse along columns) cout << "max(B,1): " << endl << max(B,1) << endl; // maximum value in B cout << "max(max(B)) = " << max(max(B)) << endl; // sum of each column (traverse along rows) cout << "sum(B): " << endl << sum(B) << endl; // sum of each row (traverse along columns) cout << "sum(B,1) =" << endl << sum(B,1) << endl; // sum of all elements cout << "accu(B): " << accu(B) << endl; // trace = sum along diagonal cout << "trace(B): " << trace(B) << endl; // generate the identity matrix mat C = eye<mat>(4,4); // random matrix with values uniformly distributed in the [0,1] interval mat D = randu<mat>(4,4); D.print("D:"); // row vectors are treated like a matrix with one row rowvec r; r << 0.59119 << 0.77321 << 0.60275 << 0.35887 << 0.51683; r.print("r:"); // column vectors are treated like a matrix with one column vec q; q << 0.14333 << 0.59478 << 0.14481 << 0.58558 << 0.60809; q.print("q:"); // convert matrix to vector; data in matrices is stored column-by-column vec v = vectorise(A); v.print("v:"); // dot or inner product cout << "as_scalar(r*q): " << as_scalar(r*q) << endl; // outer product cout << "q*r: " << endl << q*r << endl; // multiply-and-accumulate operation (no temporary matrices are created) cout << "accu(A % B) = " << accu(A % B) << endl; // example of a compound operation B += 2.0 * A.t(); B.print("B:"); // imat specifies an integer matrix imat AA; imat BB; AA << 1 << 2 << 3 << endr << 4 << 5 << 6 << endr << 7 << 8 << 9; BB << 3 << 2 << 1 << endr << 6 << 5 << 4 << endr << 9 << 8 << 7; // comparison of matrices (element-wise); output of a relational operator is a umat umat ZZ = (AA >= BB); ZZ.print("ZZ:"); // cubes ("3D matrices") cube Q( B.n_rows, B.n_cols, 2 ); Q.slice(0) = B; Q.slice(1) = 2.0 * B; Q.print("Q:"); // 2D field of matrices; 3D fields are also supported field<mat> F(4,3); for(uword col=0; col < F.n_cols; ++col) for(uword row=0; row < F.n_rows; ++row) { F(row,col) = randu<mat>(2,3); // each element in field<mat> is a matrix } F.print("F:"); return 0; }
这个C2760错误主要是因为编译器对C++标准的支持不足,或者Armadillo的预处理器宏没有正确配置导致的,按照以下步骤调整:
1. 设置正确的C++标准版本
Visual Studio 2017默认可能没有启用C11或更高版本的标准,而Armadillo的模板特化(比如std::complex相关的代码)需要C11及以上支持:
- 右键项目 -> 属性 -> 配置属性 -> C/C++ -> 语言 -> C++标准
- 选择
ISO C++11 标准(/std:c++11)或更高版本(比如ISO C++14 标准(/std:c++14))
2. 配置Armadillo的预处理器宏
Armadillo需要通过预处理器宏来启用BLAS/LAPACK支持并禁用默认的包装器(静态链接时需要):
- 右键项目 -> 属性 -> 配置属性 -> C/C++ -> 预处理器 -> 预处理器定义
- 添加以下宏:
ARMA_USE_BLAS:启用BLAS库支持ARMA_USE_LAPACK:启用LAPACK库支持ARMA_DONT_USE_WRAPPER:禁用Armadillo自带的包装器,直接链接系统BLAS/LAPACK库(静态链接时必须设置)- (可选)
ARMA_NO_DEBUG:发布版本中可以添加,禁用调试检查提升性能
3. 匹配运行库版本
你使用的是blas_win64_MT.lib和lapack_win64_MT.lib(多线程静态版本),需要确保项目的运行库设置和它们一致:
- 右键项目 -> 属性 -> 配置属性 -> C/C++ -> 代码生成 -> 运行库
- 选择
多线程(/MT)(Debug版本可以选多线程调试(/MTd),注意对应库的Debug版本)
4. 验证路径设置
再次确认附加包含目录和库目录的路径是否正确:
- 附加包含目录
$(SolutionDir)Dependencies\include:确保该路径下确实有armadillo头文件和armadillo_bits子文件夹 - 附加库目录
$(SolutionDir)Dependencies\lib_win64:确保该路径下存在指定的.lib文件
完成以上步骤后,重新编译项目,应该就能解决这个语法错误并成功链接Armadillo库了。
内容的提问来源于stack exchange,提问作者user9279721

