在C++ Builder中创建含Eigen库的DLL编译报错求助
解决C++ Builder 11 CE中集成Eigen 3.4.0编译DLL的未声明标识符错误
环境与问题
使用C++ Builder 11 CE、Eigen 3.4.0,尝试将集成Eigen库的程序打包为动态链接库(.dll)时,在DLL的cpp文件中引入#include <Eigen/Eigen>后编译,出现大量未声明标识符错误(如fabsf、expf、logf等)。
错误信息
[bcc32c Error] MathFunctions.h(1304): use of undeclared identifier 'fabsf' [bcc32c Error] MathFunctions.h(1310): use of undeclared identifier 'fabs' [bcc32c Error] MathFunctions.h(1318): use of undeclared identifier 'fabsl' [bcc32c Error] Half.h(660): no member named 'expf' in the global namespace [bcc32c Error] Half.h(671): no member named 'logf' in the global namespace [bcc32c Error] Half.h(678): no member named 'log10f' in the global namespace [bcc32c Error] Half.h(681): no member named 'logf' in the global namespace [bcc32c Error] Half.h(689): no member named 'sqrtf' in the global namespace [bcc32c Error] Half.h(693): no member named 'powf' in the global namespace [bcc32c Error] Half.h(696): no member named 'sinf' in the global namespace [bcc32c Error] Half.h(699): no member named 'cosf' in the global namespace [bcc32c Error] Half.h(702): no member named 'tanf' in the global namespace [bcc32c Error] Half.h(705): no member named 'tanhf' in the global namespace [bcc32c Error] Half.h(708): no member named 'asinf' in the global namespace [bcc32c Error] Half.h(711): no member named 'acosf' in the global namespace [bcc32c Error] Half.h(718): no member named 'floorf' in the global namespace [bcc32c Error] Half.h(726): no member named 'ceilf' in the global namespace [bcc32c Error] Half.h(730): no member named 'rintf' in the global namespace [bcc32c Error] Half.h(733): no member named 'roundf' in the global namespace [bcc32c Error] Half.h(736): no member named 'fmodf' in the global namespace [bcc32c Error] BFloat16.h(494): no member named 'expf' in the global namespace [bcc32c Error] BFloat16.h(500): no member named 'logf' in the global namespace [bcc32c Error] BFloat16.h(506): no member named 'log10f' in the global namespace [bcc32c Error] BFloat16.h(509): no member named 'logf' in the global namespace [bcc32c Error] BFloat16.h(512): no member named 'sqrtf' in the global namespace [bcc32c Error] BFloat16.h(515): no member named 'powf' in the global namespace [bcc32c Error] BFloat16.h(518): no member named 'sinf' in the global namespace [bcc32c Error] BFloat16.h(521): no member named 'cosf' in the global namespace [bcc32c Error] BFloat16.h(524): no member named 'tanf' in the global namespace [bcc32c Error] BFloat16.h(527): no member named 'asinf' in the global namespace [bcc32c Error] BFloat16.h(530): no member named 'acosf' in the global namespace [bcc32c Error] BFloat16.h(533): no member named 'atanf' in the global namespace [bcc32c Error] BFloat16.h(536): no member named 'sinhf' in the global namespace [bcc32c Error] BFloat16.h(539): no member named 'coshf' in the global namespace [bcc32c Error] BFloat16.h(542): no member named 'tanhf' in the global namespace [bcc32c Error] BFloat16.h(546): no member named 'asinhf' in the global namespace [bcc32c Error] BFloat16.h(549): no member named 'acoshf' in the global namespace [bcc32c Error] BFloat16.h(552): no member named 'atanhf' in the global namespace [bcc32c Error] BFloat16.h(556): no member named 'floorf' in the global namespace [bcc32c Error] BFloat16.h(559): no member named 'ceilf' in the global namespace [bcc32c Error] BFloat16.h(562): no member named 'rintf' in the global namespace [bcc32c Error] BFloat16.h(565): no member named 'roundf' in the global namespace [bcc32c Error] BFloat16.h(568): no member named 'fmodf' in the global namespace [bcc32c Error] BFloat16.h(585): no member named 'fminf' in the global namespace [bcc32c Error] BFloat16.h(590): no member named 'fmaxf' in the global namespace Failed
现有DLL代码
#include <Eigen/Eigen> #pragma hdrstop #pragma argsused extern "C" double __declspec(dllexport) __stdcall Sum(double a, double b) { return a +b; } extern "C" int _libmain(unsigned long reason) { return 1; }
解决办法
1. 提前引入标准数学头文件
Eigen依赖C标准库的数学函数,需在引入Eigen前包含<cmath>,确保编译器能识别这些函数。修改后的代码如下:
#include <cmath> #include <Eigen/Eigen> #pragma hdrstop #pragma argsused extern "C" double __declspec(dllexport) __stdcall Sum(double a, double b) { return a +b; } extern "C" int _libmain(unsigned long reason) { return 1; }
2. 添加编译器预定义宏
bcc32c编译器需要显式定义宏来启用C99标准的数学函数,操作步骤:
- 打开项目选项(
Project -> Options) - 切换到
C++ Compiler -> Preprocessor - 在
Defined macros中添加_USE_C99_MATH和__STDC_WANT_IEC_60559_BFP_EXT__
3. 强制Eigen使用标准库数学函数
如果前两步无效,可在引入Eigen前添加Eigen专属配置宏,强制其依赖标准库函数:
#define EIGEN_USE_STD_MATH_FUNCTIONS #include <cmath> #include <Eigen/Eigen>
4. 确认编译标准版本
确保项目使用C++11及以上标准:
- 打开项目选项 ->
C++ Compiler -> Language - 将
C++ Language Version设置为C++17或C++11
内容的提问来源于stack exchange,提问作者Scarlet
相关产品推荐
相关产品推荐

