来自上一个声明的 G++ 错误,由于包含中的函数名称重复而导致的错误

G++ error from previous declaration, error due to duplicate function name in includes

本文关键字:错误 函数 于包含 声明 上一个 G++      更新时间:2023-10-16

我正在编写一个包含两个库RcppArmadillo和trng4的Rcpp代码。但是,当我包含两个头文件(RcppArmadillo.h和trng/gamma_dist.hpp)时,它会给出编译错误。

 trng/special_functions.hpp:47:39: error: declaration of ‘float lgammaf(float) throw ()’ has a  different exception specifier
 extern "C" float lgammaf(float) throw();
include-fixed/math.h:476:14: error: from previous declaration ‘float lgammaf(float)’
extern float lgammaf(float);

完整的编译选项是

-fopenmp -lgomp -DUSE_R -DNDEBUG -DDISABLE_SINGLE -DNTHROW -DDISABLE_FIO -I/usr/local/include -I"/Users/avi/Library/R/3.0/library/Rcpp/include" -I"/Users/avi/Library/R/3.0/library/RcppArmadillo/include"   -fPIC -pipe -std=c++0x -Wall -pedantic -c 

似乎在两个头文件中都声明了 lgammaf。我尝试将 -E 与 g++ 选项一起使用,但给出 ld 警告".o,文件是为不受支持的文件格式构建的",并在 .当我在 R 中加载后尝试时调用。我做错了什么?

也许脱离上下文,我正在使用 trng4 包来开发一个线程 gibbs 采样器(在 openmp 中),该采样器从伽马分布中采样。我目前正在运行这个MacOS。但最终它将在Linux服务器中运行。

听起来犰狳和trng4之间确实有问题。 如果可能的话,也许您应该尝试将接口分开,这样您就不需要在同一文件中包含两者。

如果我错了,有人可以纠正我,但我相信您可以通过在每个头文件中使用 #ifndef 来防止此问题,这样即使它第二次包含也不会第二次定义。但我想这些不是你的文件是吗...?

#ifndef __your_unique_header_name__
blah blah

#endif