我正在使用Eigen库在C ++中编写有限元程序。但是,链接器似乎无法识别我的所有文件。
这是我得到的错误:
Undefined symbols for architecture x86_64:
"Eigen::Matrix<float, 3, 1, 0, 3, 1> pear::extract<Eigen::Matrix<float, 3, 1, 0, 3, 1>, Eigen::Matrix<float, -1, 1, 0, -1, 1>, Eigen::Block<Eigen::Ma
trix<int, -1, -1, 0, -1, -1>, 1, -1, false> >(Eigen::Matrix<float, -1, 1, 0, -1, 1> const&, Eigen::Block<Eigen::Matrix<int, -1, -1, 0, -1, -1>, 1, -1,
false> const&)", referenced from:
pear::stiff(Eigen::Matrix<float, -1, 1, 0, -1, 1>&, Eigen::Matrix<float, -1, 1, 0, -1, 1>&, Eigen::Matrix<int, -1, -1, 0, -1, -1>&) in stiff.o
"Eigen::Matrix<float, -1, 1, 0, -1, 1> pear::load_csv<Eigen::Matrix<float, -1, 1, 0, -1, 1> >(std::__cxx11::basic_string<char, std::char_traits<char>
, std::allocator<char> > const&)", referenced from:
_main in main.o
"Eigen::Matrix<int, -1, -1, 0, -1, -1> pear::load_csv<Eigen::Matrix<int, -1, -1, 0, -1, -1> >(std::__cxx11::basic_string<char, std::char_traits<char>
, std::allocator<char> > const&)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
我的主要文件如下:
#include "eigen_ext.hpp"
#include "stiff.hpp"
#include <iostream>
#include <Eigen/Core>
int main(int args, char *argv[]) {
Eigen::MatrixXf stiff_matrix;
Eigen::MatrixXi node;
Eigen::VectorXf xp, yp;
node = pear::load_csv<Eigen::MatrixXi>("../mesh/node.csv");
xp = pear::load_csv<Eigen::VectorXf>("../mesh/xp.csv");
yp = pear::load_csv<Eigen::VectorXf>("../mesh/yp.csv");
return 0;
}
并且有问题的文件是以下文件,eigen_ext.cpp(我在同一个论坛上创建了一些函数):
#include "eigen_ext.hpp"
namespace pear {
template <typename T1, typename T2, typename T3>
T1 extract(const T2 &full, const T3 &ind) {
int num_indices = ind.innerSize();
T1 target(num_indices);
for (int i = 0; i < num_indices; i++) {
target[i] = full[ind[i]];
}
return target;
} // namespace
// peartemplate<typenameT1,typenameT2,typenameT3>T1extract(constT2&full,constT3&ind)
template <typename M> M load_csv(const std::string &path) {
std::ifstream indata;
indata.open(path);
std::string line;
std::vector<double> values;
uint64_t rows = 0;
while (std::getline(indata, line)) {
std::stringstream lineStream(line);
std::string cell;
while (std::getline(lineStream, cell, ',')) {
values.push_back(std::stod(cell));
}
++rows;
}
return Eigen::Map<
const Eigen::Matrix<typename M::Scalar, M::RowsAtCompileTime,
M::ColsAtCompileTime, Eigen::RowMajor>>(
values.data(), rows, values.size() / rows);
}
} // namespace pear
以下是包含刚性函数开头的stiff.cpp的摘录,其中调用了extract函数:
MatrixXf stiff(VectorXf &xp, VectorXf &yp, MatrixXi &node) {
/*
xp(i) : x-coordinates of the nodes
yp (i): y-coordinates of the nodes
node(i,j,k) : edges matrix (for each triangle, the indices of the three nodes)
*/
/*
m : number of triangles of triangulation
n : number of vertices of triangulation (edges??)
*/
int n = node.rows(); // number of vertices
int m = n - 2; // each points creates a new triangle except for the two first
// ones (can be seen as a variation of Euler's characteristic)
// stiff matrix declaration
MatrixXf S = MatrixXf::Zero(n, n), Dphi(3, 2);
Vector3f x = Vector3f::Zero(), y = Vector3f::Zero();
float D = 1;
int i, j;
除main.cpp之外的所有文件都有一个.hpp标题,其中包含对库的引用以及.cpp等效函数的定义。
我的makefile是以下内容:
#MAKEFILE FOR WIT PROJECT
.PHONY: clean all info
#CXX = g++
CXX = gcc-7
#CXX = clang
TARGETS := eigen_ext stiff tests main
SOURCES := $(TARGETS:=.cpp)
OBJS := $(TARGETS:=.o)
OFLAGS := -O2 -O3 -ffast-math
PARALLELFLAGS:= -D_GLIBCXX_PARALLEL -fopenmp -pthread -DUSEOMP
CXXFLAGS := -Wall -std=c++14 -v
LDFLAGS :=
LIBS := -lstdc++ -I../Eigen
LIPOPT :=
OPENMPLIB :=
EXAMPLE_DEPS = Makefile
all: main
clean:
rm -f $(OBJS) $(TARGETS)
info:
@echo Compiler: CXX = $(CXX)
@echo Compile command: COMPILE.cc = $(COMPILE.cc)
@echo Link command: LINK.cc = $(LINK.cc)
eigen_ext.o: eigen_ext.cpp $(EXAMPLE_DEPS)
@$(CXX) -c $(CXXFLAGS) $(OFLAGS) $(LIBS) -o eigen_ext.o eigen_ext.cpp
stiff.o: stiff.cpp $(EXAMPLE_DEPS)
@$(CXX) -c $(CXXFLAGS) $(OFLAGS) $(LIBS) -o stiff.o stiff.cpp
main.o: main.cpp $(EXAMPLE_DEPS)
@$(CXX) -c $(CXXFLAGS) $(OFLAGS) $(LIBS) -o main.o main.cpp
main: main.o stiff.o eigen_ext.o
@$(CXX) $(LDFLAGS) -o main main.o stiff.o eigen_ext.o $(LIBS)
我在macOS中使用g ++版本7.2.0(与自制软件一起安装)。编译后存在所有目标文件,除了未建立的符号外,我没有编译错误。
在几周内阅读了关于链接和库的各地之后,我仍然遇到了同样的问题。我想问题解决起来很简单,但我找不到它。出于这个原因,我决定在这里问一下。非常感谢你!