我目前正在编写一个仅 header 的包装器库,它应该提供对某些 FORTRAN 代码的 C++ 访问。但是我卡在了“ undefined reference (...)”
我必须说我有点惊讶,因为我使用这个 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);
}
我这样调用它
__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 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
但是,请注意
[dare2be@schroedinger multinest-interfejs]$ nm /usr/local/lib/libnest3.a | grep __nested_MOD_nestrun
000000000001e0f0 T __nested_MOD_nestrun
我一直都是自己处理那些 undefined reference 。但是现在我无法解决这个问题。我指定 -lnest3 -L/usr/local/lib
明确和/usr/local/lib/libnest3.a
包含链接器提示的例程......请帮助兄弟! :)
编辑:修复错别字
最佳答案
在 GCC 的命令行中指定库的顺序很重要。阅读 GCC 手册页了解详细信息 - 简而言之,您必须在使用它们的模块之后指定库(-lnest3
等)。 (我总是在命令行末尾指定库。)
在你的情况下你必须写
gfortran ExampleLibMnCpp.o -o ExampleLibMnCpp -L/usr/local/lib -lstdc++ -llapack -lblas -lnest3
关于c++ - 无法通过 "undefined reference to ` XXXX'”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7036158/