无法越过“对'XXXX'的未定义引用” [英] Can't get past "undefined reference to `XXXX'"

查看:728
本文介绍了无法越过“对'XXXX'的未定义引用”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在编写一个头文件包装库,它应该提供C ++访问某些FORTRAN代码的功能。但我坚持'未定义的引用(...)'

我必须说我有点惊讶,因为我使用C ++编译自己的函数这个FORTRAN程序,完全没有问题。

也许关于结构的几句话。该库遵循单例模式,但没有用户实例化库的可能性。因此,用户的唯一入口点是静态方法 MultiNestWrapper :: Wrapper< T> :: run() MultiNestWrapper 是一个命名空间, Wrapper< T> 是一个模板类(所以将来你可以基准如何选择执行计算的类型会影响结果和性能)。 FORTRAN例程在这个命名空间之外被声明为:

$ externC{
extern void __nested_MOD_nestrun(int * ,int *,int *,double *,double *,int *,int *,int *,int *,int *,double *,const char *,int *,int *,int *,int *,void(* (double *,int *,int *,double *),void(* dumper)(int *,int *,int *,double **,double **,double *,double *,double *),int *上下文);
}

我称它为

  __ nested_MOD_nestrun(& _mmodal,& _ceff,& _nlive,& _tol,& _efr,& _ndims,& _nPar,& _nClsPar,& _maxModes ,& _updInt,& _Ztol,_root,& _seed,_pWrap,& _fb,& _resume,internalLogLike,internalDumper,& _context); 

参数的类型匹配。

当我尝试编译它时,出现以下错误:

  [dare2be @ schroedinger multinest-interfejs] $ make all 
g ++ -c ExampleLibMnCpp.cpp -o ExampleLibMnCpp.o
gfortran -lstdc ++ -llapack -lblas -lnest3 -L / usr / local / lib ExampleLibMnCpp.o -o ExampleLibMnCpp
ExampleLibMnCpp.o:In函数`MultiNestWrapper :: Wrapper< double> :: run(MultiNestWrapper :: MNParams< double> *,double(*)(double const *,int),bool,std :: basic_string< char,std :: char_traits< char> ,std :: allocator< char>,int,double,int,double,void(*)(int *,int *,int *,double **,double **,double *,double *,double *) ,int,double,bool)':
ExampleLibMnCpp.cpp :( .text._ZN16MultiNestWrapper7WrapperIdE3runEPNS_8MNParamsIdEEPFdPKdiEbSsididPFvPiS9_S9_PPdSB_SA_SA_SA_Eidb [MultiNestWrapper :: Wrapper< double> :: run(MultiNestWrapper :: MNParams< double> *,double(*)(double const, *,int ),bool,std :: basic_string< char,std :: char_traits< char> ;, std :: allocator< char> > ;, int,double,int,double,void(*)(int *,int *,int *,double **,double **,double *,double *,double *),int,double,bool)] + 0x585):对`__nested_MOD_nestrun'的未定义引用
collect2:ld返回1退出状态
make:*** [ExampleLibMnCpp]错误1

但是,请注意,

  [dare2be @ schroedinger multinest-interfejs] $ nm /usr/local/lib/libnest3.a | grep __nested_MOD_nestrun 
000000000001e0f0 T __nested_MOD_nestrun

我自己一直处理那些未定义的引用。但是现在我无法把头绕在这里。我指定 -lnest3 -L / usr / local / lib explicite /usr/local/lib/libnest3.a 包含链接器发出的例程...请帮助兄弟! :)

编辑:固定错字

解决方案

您可以在GCC的命令行中指定库。有关详细信息,请阅读GCC手册页 - 简而言之,您必须在模块使用它们之后指定库( -lnest3 等)。 (我总是在命令行末尾指定库)。



在你的情况下,你必须写

  gfortran ExampleLibMnCpp.o -o ExampleLibMnCpp -L / usr / local / lib -lstdc ++ -llapack -lblas -lnest3 


I'm currently writing a header-only wrapper library which is supposed to provide C++ access to some FORTRAN code. But I'm stuck at the 'undefined reference (...)'

I must say I'm a little bit surprised because I compiled my own functions in C++ using this FORTRAN routine, with no problems at all.

Maybe a few words about the structure. The library follows the singleton pattern, but without the possibility of instantiating the Library by the user. So the only entry point for the user is a static method MultiNestWrapper::Wrapper<T>::run(). MultiNestWrapper is a namespace, Wrapper<T> is a templated class (so that in future you can benchmark how choosing on which type to perform calculations affects results and performance). The FORTRAN routine is declared outside of this namespace, as

extern "C" {
  extern void __nested_MOD_nestrun(int *, int *, int *, double *, double *, int *, int *, int *, int *, int *, double *, const char *, int *, int *, int *, int *, void (*Loglike)(double *, int *, int *, double *), void (*dumper)(int *, int *, int *, double **, double **, double *, double *, double *), int *context);
}

and I call it like

__nested_MOD_nestrun(&_mmodal, &_ceff, &_nlive, &_tol, &_efr, &_ndims, &_nPar, &_nClsPar, &_maxModes, &_updInt, &_Ztol, _root, &_seed, _pWrap, &_fb, &_resume, internalLogLike, internalDumper, &_context);

And the types for arguments match.

When I try to compile it, I get the following error:

[dare2be@schroedinger multinest-interfejs]$ make all
g++ -c ExampleLibMnCpp.cpp -o ExampleLibMnCpp.o
gfortran -lstdc++ -llapack -lblas -lnest3 -L/usr/local/lib ExampleLibMnCpp.o -o ExampleLibMnCpp
ExampleLibMnCpp.o: In function `MultiNestWrapper::Wrapper<double>::run(MultiNestWrapper::MNParams<double>*, double (*)(double const*, int), bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, double, int, double, void (*)(int*, int*, int*, double**, double**, double*, double*, double*), int, double, bool)':
ExampleLibMnCpp.cpp:(.text._ZN16MultiNestWrapper7WrapperIdE3runEPNS_8MNParamsIdEEPFdPKdiEbSsididPFvPiS9_S9_PPdSB_SA_SA_SA_Eidb[MultiNestWrapper::Wrapper<double>::run(MultiNestWrapper::MNParams<double>*, double (*)(double const*, int), bool, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, int, double, int, double, void (*)(int*, int*, int*, double**, double**, double*, double*, double*), int, double, bool)]+0x585): undefined reference to `__nested_MOD_nestrun'
collect2: ld returned 1 exit status
make: *** [ExampleLibMnCpp] Error 1

But, note that

[dare2be@schroedinger multinest-interfejs]$ nm /usr/local/lib/libnest3.a | grep __nested_MOD_nestrun
000000000001e0f0 T __nested_MOD_nestrun

I've always dealt with those undefined references myself. But now I can't wrap my head around this. I specify -lnest3 -L/usr/local/lib explicite and /usr/local/lib/libnest3.a contains the routine the linker whines about... Please, help a brother out! :)

EDIT: fixed typos

解决方案

The order in which you specify libraries in GCC's command-line matters. Read GCC man pages for details - in short you have to specify libraries (-lnest3 etc) after modules that use them. (I always specify libraries in the end of the command-line.)

In your case you have to write

gfortran ExampleLibMnCpp.o -o ExampleLibMnCpp -L/usr/local/lib -lstdc++ -llapack -lblas -lnest3

这篇关于无法越过“对'XXXX'的未定义引用”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆