由于在python中的代码依赖numpy库,转到C++后,需要重新安装numpy的C++版本(NumCpp),地址为:
dpilger26/NumCpp: C++ implementation of the Python Numpy library (github.com)https://github.com/dpilger26/NumCpp按照官方只是安装编译完成后,运行测试代码
#include "NumCpp.hpp"
#include <cstdlib>
#include <iostream>
int main()
{
auto a = nc::random::randInt<int>({10, 10}, 0, 100);
std::cout << a;
return EXIT_SUCCESS;
}
报错:
D:/clion2024-1-4/CLion 2024.1.4/bin/mingw/x86_64-w64-mingw32/include/time.h:264:55: note: initializing argument 1 of 'errno_t gmtime_s(tm*, const time_t*)' 264 | static __inline errno_t __CRTDECL gmtime_s(struct tm *_Tm, const time_t *_Time) { return _gmtime64_s(_Tm, _Time); } | ~~~~~~~~~~~^~~ D:/island_project/NumCpp/include/NumCpp/DateTime/DateTime.hpp: In member function 'std::string nc::DateTime::toStr() const': D:/island_project/NumCpp/include/NumCpp/DateTime/DateTime.hpp:374:22: error: cannot convert 'time_t*' {aka 'long long int*'} to 'tm*' 374 | gmtime_r(&secondsFromEpoch, &tm); | ^~~~~~~~~~~~~~~~~ | | | time_t* {aka long long int*} D:/clion2024-1-4/CLion 2024.1.4/bin/mingw/x86_64-w64-mingw32/include/time.h:264:55: note: initializing argument 1 of 'errno_t gmtime_s(tm*, const time_t*)' 264 | static __inline errno_t __CRTDECL gmtime_s(struct tm *_Tm, const time_t *_Time) { return _gmtime64_s(_Tm, _Time); } | ~~~~~~~~~~~^~~ ninja: build stopped: subcommand failed.
在网上查找相关资料后,发现缺少boost库,于是 跟着(网站链接)网上的教程(配置和编译boost库后,还是会继续报错,显示boost无法安装,在网上找了好多方法后,还是无法安装编译成功boost(boost是numcpp的依赖库,必须先安装编译成功boost!)。最后实在没办法直接使用vcpkg安装,过程比较长,但是总算安装成功了。之后重新编译NumCpp,运行测试代码,依旧报上面的错误!!!
最终解决方案:在测试代码上面使用了条件编译来重新定义gmtime_r
,使其参数顺序与gmtime_s
一致。同时也重新定义了timegm
为_m
kgmtime;
#ifdef _WIN32
#define gmtime_r(time, result) gmtime_s(result, time)
#define timegm _mkgmtime
#endif
#include "NumCpp.hpp"
#include <cstdlib>
#include <iostream>
int main()
{
auto a = nc::random::randInt<int>({10, 10}, 0, 100);
std::cout << a;
return EXIT_SUCCESS;
}
参考资料: