gpt4 book ai didi

c++ - 链接期间未定义的函数引用

转载 作者:行者123 更新时间:2023-11-28 03:38:59 25 4
gpt4 key购买 nike

当我尝试链接我的代码时出现链接错误( undefined reference ),但我无法弄清楚原因。

I am linking using the following command:
mpic++ -Wl,-V main.o Particle.o Particle_forces.o User_input.o output.o time_step_Gear_Verlet.o Verlet_variables.o -o DEM.exe -Lboost_mpi.a -Lboost_serialization.a

我得到的错误告诉我“main.cpp 中的 step(...) 函数未定义。

所以...main() 的代码看起来像这样(我省略了包含,但它确实包含带有 step() 函数的 header ,这很明显,因为我没有编译错误):

int main() 
{

//do some stuff


for(int i=0;i<input_data.nstep();i++)
{
//call step() to do all calculations necessary for a particular timestep
step(particles, safe_particles, particle_properties, particle_forces, input_data, verlet_list, verlet_celllist, coll_eros_track, tan_contact_histories, collision_number_part);

//do some more stuff
}


return 0;
}

step函数可以在time_step_Gear_Verlet.cpp中找到:

void step(std::vector<Particle> & particles, std::vector<Particle> & safe_particles, std::vector<Particle_props> & particle_properties, std::vector<Particle_forces> & particle_forces, User_input& input_data, std::vector<std::set<int> > & verlet_list, vector<vector<vector<vector<int> > > > verlet_celllist, data_tracking & coll_eros_track, vector<map<int,Vector> > & tan_contact_histories, vector<int> & collision_number_part)
{
//define booleans used for checking what to do
//newverlet states whether the verlet lists are currently new and ok will be the output to make_verlet
bool ok=true, newverlet=false;

if(input_data.collisions_on())
{
//uses verlet_needs_update() to see if the verlet lists need to be updated
if(verlet_needs_update(particles, safe_particles, input_data))
{
//if the lists need to be updated then a call to make_verlet is made to make new lists and output is stored in "ok"
ok=make_verlet(particles, particle_properties, input_data, verlet_celllist, verlet_list);

//since new lists have been made, newverlet becomes true
newverlet=true;
}

//if something went wrong when the new lists were made, then a restoration has to be made
if(!ok)
{
cout<<"verlet list construction failed due to particles already touching. Try changing the verlet distance"<<endl;
exit(1);
}

//if the lists are new and make_verlet worked fine, then store current particle values
if (newverlet && ok)
{
//store current particle and time values
safe_particles=particles;
}
}
//call integrate() to integrate the equations of motion
integrate(particles, particle_properties, particle_forces, input_data, verlet_list, coll_eros_track, tan_contact_histories, collision_number_part);
}

标题定义:

#ifndef time_step_Gear_h
#define time_step_Gear_h

#include <vector>
#include <map>
#include <set>

#include "Particle.h"
#include "Particle_props.h"
#include "Particle_forces.h"
#include "User_input.h"
#include "data_tracking.h"

void step(std::vector<Particle> &, std::vector<Particle> &, std::vector<Particle_props> &, std::vector<Particle_forces> &, User_input&, std::vector<std::set<int> > &, std::vector<std::vector<std::vector<std::vector<int> > > > &, data_tracking &, std::vector<std::map<int,Vector> > &, std::vector<int> &);
void make_forces(std::vector<Particle> &, std::vector<Particle_props> &, std::vector<Particle_forces> &, User_input&, std::vector<std::set<int> > &, data_tracking &, std::vector<std::map<int,Vector> > &, std::vector<int> &);
void integrate(std::vector<Particle> &, std::vector<Particle_props> &, std::vector<Particle_forces> &, User_input&, std::vector<std::set<int> > &, data_tracking &, std::vector<std::map<int,Vector> > &, std::vector<int> &);
void init_algorithm(std::vector<Particle> &, vector<Particle> & safe_particles, std::vector<Particle_props> &, User_input&, std::vector<std::vector<std::vector<std::vector<int> > > > &, std::vector<std::set<int> > &);


#endif

我显然是将文件链接在一起(顺序无效)。对于从 main.cpp 调用并在另一个 .cpp 文件中定义的任何其他函数,我没有这个问题(并且注释掉对 step() 的调用根本不会导致错误)。有什么想法可能导致此问题吗?

最佳答案

您的定义中似乎缺少 &。检查verlet_celllist

关于c++ - 链接期间未定义的函数引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9945854/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com