我做了一个很小的例子:

解算器

#pragma once

#include <Eigen/Dense>
#include <Eigen/Sparse>
#include <Eigen/CholmodSupport>

typedef Eigen::SparseMatrix<double> SpMat;

class UseCholmodSolver
{
    public:
        UseCholmodSolver() {}
        ~UseCholmodSolver() {}
    private:
        Eigen::CholmodSupernodalLLT<SpMat> cholmod;
};

求解器
#include "solver.h"

main.cpp
#include "solver.h"

int main()
{
    return 0;
}

生成文件
CXXFLAGS=-g -O2 -Wall -DNDEBUG -I./ext/eigen -I/usr/include/suitesparse
CXX=g++

all: t1
clean:
    rm -f *.o main

main.o: main.cpp solver.h
solver.o: solver.cpp solver.h

t1: main.o
    $(CXX) main.o -o main -lcholmod
t2: main.o solver.o
    $(CXX) main.o solver.o -o main -lcholmod

当我执行“make t1”时,一切都很好。
但是,当我键入“make t2”时,会出现重新定义错误:
g++ main.o solver.o -o main -lcholmod
solver.o: In function `int Eigen::internal::cm_start<long>(cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:177: multiple definition of `int Eigen::internal::cm_start<long>(cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:177: first defined here
solver.o: In function `int Eigen::internal::cm_finish<long>(cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:178: multiple definition of `int Eigen::internal::cm_finish<long>(cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:178: first defined here
solver.o: In function `int Eigen::internal::cm_free_factor<long>(cholmod_factor_struct*&, cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:180: multiple definition of `int Eigen::internal::cm_free_factor<long>(cholmod_factor_struct*&, cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:180: first defined here
solver.o: In function `int Eigen::internal::cm_free_dense<long>(cholmod_dense_struct*&, cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:181: multiple definition of `int Eigen::internal::cm_free_dense<long>(cholmod_dense_struct*&, cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:181: first defined here
solver.o: In function `int Eigen::internal::cm_free_sparse<long>(cholmod_sparse_struct*&, cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:182: multiple definition of `int Eigen::internal::cm_free_sparse<long>(cholmod_sparse_struct*&, cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:182: first defined here
solver.o: In function `cholmod_factor_struct* Eigen::internal::cm_analyze<long>(cholmod_sparse_struct&, cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:184: multiple definition of `cholmod_factor_struct* Eigen::internal::cm_analyze<long>(cholmod_sparse_struct&, cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:184: first defined here
solver.o: In function `cholmod_dense_struct* Eigen::internal::cm_solve<long>(int, cholmod_factor_struct&, cholmod_dense_struct&, cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:187: multiple definition of `cholmod_dense_struct* Eigen::internal::cm_solve<long>(int, cholmod_factor_struct&, cholmod_dense_struct&, cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:187: first defined here
solver.o: In function `cholmod_sparse_struct* Eigen::internal::cm_spsolve<long>(int, cholmod_factor_struct&, cholmod_sparse_struct&, cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:190: multiple definition of `cholmod_sparse_struct* Eigen::internal::cm_spsolve<long>(int, cholmod_factor_struct&, cholmod_sparse_struct&, cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:190: first defined here
solver.o: In function `int Eigen::internal::cm_factorize_p<long>(cholmod_sparse_struct*, double*, long*, unsigned long, cholmod_factor_struct*, cholmod_common_struct&)':
/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:195: multiple definition of `int Eigen::internal::cm_factorize_p<long>(cholmod_sparse_struct*, double*, long*, unsigned long, cholmod_factor_struct*, cholmod_common_struct&)'
main.o:/home/xiang/Code/testEigen/./ext/eigen/Eigen/src/CholmodSupport/CholmodSupport.h:195: first defined here
collect2: error: ld returned 1 exit status
Makefile:14: recipe for target 't2' failed
make: *** [t2] Error 1

似乎原因是“CholmodSupport.h”中的“long”类型专用模板函数。有解决这个问题的主意吗?

最佳答案

在Eigen的开发分支中,CholmodSupport出现了问题,我刚刚在这里修复了该问题:
https://bitbucket.org/eigen/eigen/commits/5ecddbf9fae7f5a4d4e587b8809d9287306885d3

问题在于,某些辅助函数(用于委派给适当的内部cholmod函数)没有标记为inline,因此在不同的编译单元中进行编译时具有多个定义。

对于Eigen 3.3和Eigen 3.2,您的最小示例应该可以正常工作。

关于c++ - 如何在Ubuntu下的Eigen3中使用CholmodSupport,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44288101/

10-13 08:16