我目前正在编写一个仅包头的包装器库,它应该提供对某些FORTRAN代码的C++访问.但我坚持'未定义的引用(...)'
我必须说我有点惊讶,因为我使用这个FORTRAN例程在C++中编译自己的函数,没有任何问题.
也许关于结构的几句话.该库遵循单例模式,但没有用户实例化库的可能性.因此,用户的唯一入口点是静态方法MultiNestWrapper::Wrapper<T>::run().MultiNestWrapper是一个名称空间,Wrapper<T>是一个模板化的类(因此,将来您可以基准测试选择执行计算的类型会影响结果和性能).FORTRAN例程在此命名空间之外声明,如
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);
}
Run Code Online (Sandbox Code Playgroud)
我称之为
__nested_MOD_nestrun(&_mmodal, &_ceff, &_nlive, &_tol, &_efr, &_ndims, &_nPar, &_nClsPar, &_maxModes, &_updInt, &_Ztol, _root, &_seed, _pWrap, &_fb, &_resume, internalLogLike, internalDumper, &_context);
Run Code Online (Sandbox Code Playgroud)
参数的类型匹配.
当我尝试编译它时,我收到以下错误:
[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
Run Code Online (Sandbox Code Playgroud)
但是,请注意
[dare2be@schroedinger multinest-interfejs]$ nm /usr/local/lib/libnest3.a | grep __nested_MOD_nestrun
000000000001e0f0 T __nested_MOD_nestrun
Run Code Online (Sandbox Code Playgroud)
我自己总是处理那些未定义的引用.但现在我无法绕过这个.我指定-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
Run Code Online (Sandbox Code Playgroud)