c++ - 编译 bfx-cpp-api - 字节数据类型未声明

标签 c++ gnu crypto++

所以我想编译这个项目 https://github.com/MMquant/bfx-cpp-api使用示例代码。我已包含自述文件中建议的 cryptopp 文件。可以在这里找到:https://github.com/weidai11/cryptopp .

我正在使用 Ubutnu 版本 17.10 和 GNU 编译器。

这是我的编译方式:

g++ example.cpp BitfinexAPI.cpp BitfinexAPI.hpp  -Icryptopp -I.  -o a -w -std=c++17

BitFinex 支持损坏的代码似乎很奇怪,所以我相当确定问题一定是我正在做的事情。

我收到的错误与第 936 行相关,其中未声明数据类型“byte”。我的预测是我在某处缺少头文件,但任何帮助将不胜感激。


$ g++ example.cpp BitfinexAPI.cpp BitfinexAPI.hpp  -Icryptopp -I.  -o a -w -std=c++17
BitfinexAPI.cpp: In static member function ‘static int BitfinexAPI::getBase64(const string&, std::__cxx11::string&)’:
BitfinexAPI.cpp:936:5: error: ‘byte’ was not declared in this scope
     byte buffer[1024] = {};
     ^~~~
BitfinexAPI.cpp:936:5: note: suggested alternative:
In file included from ../cryptopp/seckey.h:9:0,
                 from ../cryptopp/hmac.h:9,
                 from BitfinexAPI.cpp:37:
../cryptopp/config.h:222:23: note:   ‘CryptoPP::byte’
 typedef unsigned char byte;
                       ^~~~
BitfinexAPI.cpp:940:9: error: ‘buffer’ was not declared in this scope
         buffer[i] = content[i];
         ^~~~~~
BitfinexAPI.cpp:940:9: note: suggested alternative: ‘setbuffer’
         buffer[i] = content[i];
         ^~~~~~
         setbuffer
BitfinexAPI.cpp:943:21: error: ‘buffer’ was not declared in this scope
     StringSource ss(buffer, content.length(), true, new Base64Encoder( new StringSink(encoded), false));
                     ^~~~~~
BitfinexAPI.cpp:943:21: note: suggested alternative: ‘setbuffer’
     StringSource ss(buffer, content.length(), true, new Base64Encoder( new StringSink(encoded), false));
                     ^~~~~~
                     setbuffer
BitfinexAPI.cpp: In static member function ‘static int BitfinexAPI::getHmacSha384(const string&, const string&, std::__cxx11::string&)’:
BitfinexAPI.cpp:963:33: error: ISO C++ forbids declaration of ‘type name’ with no type [-fpermissive]
     SecByteBlock byteKey((const byte*)key.data(), key.size());
                                 ^~~~
BitfinexAPI.cpp:963:27: error: expected primary-expression before ‘const’
     SecByteBlock byteKey((const byte*)key.data(), key.size());
                           ^~~~~
BitfinexAPI.cpp:963:27: error: expected ‘)’ before ‘const’

最佳答案

Here's how I am compiling:

g++ example.cpp BitfinexAPI.cpp BitfinexAPI.hpp  -Icryptopp -I.  -o a -w -std=c++17

It seems strange that BitFinex would support broken code so I am fairly sure that the issue must be something that I am doing.

The errors I am getting are related to line 936 where the datatype 'byte' is not declared. My prediction is that I am missing a header file somewhere but any help would be appreciated.

@kabanus 发现了问题。

Crypto++ 用于在全局 C++ 命名空间中提供一个字节。它在那里有两个原因。首先,它是 Linux 上的一个便利项目。您可以使用 byte 而不是 CryptoPP::byte。其次,它避免了 Windows 上的编译器错误。 Microsoft SDK 在全局命名空间中提供了一个 byte,如果我们在 CryptoPP::byte 处放置一个 byte,则会因定义不明确而导致编译错误。

C++17 出现并提供了 std::byte;请参阅P0298R0, A byte type definition 。当人们使用 using namespace std 时,全局 Crypto++ byte 破坏了 Linux。它完全打破了微软,因为微软的 SDK 提供了一个全局字节。讽刺的是,P0298R0 的作者为 Microsoft 工作。

Crypto++ 在全局命名空间中放置一个字节 是 C++ 的禁忌。多年来我们都侥幸逃脱了惩罚,但在 C++17 中它却跳了起来咬了我们一口。我们将它移动到它所属的命名空间中。登记入住时间为 Commit 00f9818b5d8e ,发生在 5.6.5 发布之后、6.0 发布之前。

查看 BitfinexAPI.cpp 的源代码,这可能是解决方案...打开 BitfinexAPI.cpp,并在文件顶部添加以下内容:

// CRYPTOPP_NO_GLOBAL_BYTE signals byte is at CryptoPP::byte
#if defined(CRYPTOPP_NO_GLOBAL_BYTE)
  using CryptoPP::byte;
#endif

另请参阅std::byte在 Crypto++ wiki 上。我们花时间记录它是因为 std::byte、Microsoft 的全局 byte 和我们的 byte 定义将导致所有问题。


相关,你不需要在命令行上指定C++头。编译错误是由于上面详述的更改造成的。您所需要的只是:

g++ -std=c++17 example.cpp BitfinexAPI.cpp -I. ./cryptopp/libcryptopp.a -o example.exe

这假设目录结构为:

- bfx-api/
  |
  +- cryptopp/

cryptopp/libcryptopp.a 是回避那些已经存在多年的愚蠢的 Linux 路径问题的便捷方法。您链接到静态存档,这意味着您在运行时不需要库。


现在在 BFX 问题跟踪器中打开:Crypto++ byte change at Crypto++ 6.0 。它应该可以帮助项目工程师解决这些变化。

关于c++ - 编译 bfx-cpp-api - 字节数据类型未声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47510106/

相关文章:

c++ - decltype 在 gcc 4.3.2 中不能正常工作?

c++ - ifstream 找不到文件?

c++ - 当前 tz 的夏令时

c++ - 多线程无法按预期工作

c++ - gcc 和 libstdc++ 向前兼容

linux - 多个 GNU-Bash 命令在 aix 上运行时表现不同,任何解决方案

linux - 在远程服务器上执行本地脚本/命令

c++ - 为什么 65537 不使用 CryptoPP 将 base64URL 编码为 "AQAB"?

c++ - 将编码的 std::string 从 Base16 转换为 Base10?

c++ - 使用 minGW 构建 crypto562