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

Compiling bfx-cpp-api - Byte datatype undeclared

所以我想编译这个项目https://github.com/MMquant/bfx-cpp-api using the example code. I have included the cryptopp file that was suggested in the read me. It is found here: 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’

有点棘手,但我(认为?)找到了。 bytecryptopp 代码中定义:

https://github.com/weidai11/cryptopp/blob/master/config.h

第 222 行。所以我猜测您系统上的 config.h 是 non-existent,可能是因为您没有正确安装 header。一个常见的问题是图书馆

sudo apt-get install libcrypto++

但缺少开发者文件(特别是 headers)

sudo apt-get install libcrypto++-dev

我不是 100% 了解软件包名称(Debian 用户),不过应该差不多。

JTejedor 收获不错:

似乎加密专家在他们的定义周围放置了一个名称空间,以免与 std::byte 冲突。您可能想在 bfx 中的字节定义中添加一个 CryptoPP::(第 936 行),如果可行,请向那边的人打开一个错误(或请求拉取)。

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++ 命名空间中提供 byte。它在那里有两个原因。首先,它是 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 提供了一个全局 byte。具有讽刺意味的是,P0298R0 的作者为 Microsoft 工作。

Crypto++ 在全局命名空间中放置 byte 是 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

另请参阅 Crypto++ wiki 上的 std::byte。我们花时间记录它是因为所有问题 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 路径问题的简便方法。您 link 反对静态存档,这意味着您在运行时不需要库。


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