c++ - 无法通过 "undefined reference to ` XXXX'”

标签 c++ linker fortran

我目前正在编写一个仅 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/

相关文章:

C++ : Converting String to Double

c++ - std::list< std::unique_ptr<T>>:传递它

c++ - 如何在 visual studio 中强制链接库中的对象模块或符号

wpf - Fortran 可以与 Windows 上的 Silverlight、WPF 或 MFC 交互吗?

fortran - Fortran 90 和 Fortran 95 之间的区别

c++ - C++ Xcode构建失败,未找到errno.h

c++ - 使用 NULL 引用可以吗?

python - Numpy 设置工具 : How to compile fortran file as part of a module

c - 列出某个动态加载库的使用符号

c++ - Apple Mach-O 链接器命令失败,退出代码为 1