Cannot compile, error: cryptlib.h: No such file or directory

Cannot compile, error: cryptlib.h: No such file or directory

我已经从官方网站下载了 Crypto++ 7.0.0,用它构建了一个静态库,包括 cryptlib 头文件:

#include "cryptlib.h"

当我尝试使用以下代码编译我的程序时:

gcc main.cpp ./cryptopp700/libcryptopp.a

它向我抛出这样的错误:

main.cpp:2:10: fatal error: cryptlib.h: No such file or directory
 #include "cryptlib.h"
          ^~~~~~~~~~~~
compilation terminated.

我也试过:

-L. -llibcryptopp //while moving libcryptopp.a to the same directory main.cpp is
-L./cryptopp700 -llibcryptopp

所以我开始怀疑我是否做错了什么,但是当我检查带有静态库的代码示例时,一切似乎都很好。

请帮忙。

基于:

main.cpp:2:10: fatal error: cryptlib.h: No such file or directory

并且:

gcc main.cpp ./cryptopp700/libcryptopp.a

您的目录结构如下:

+- Project Folder
  |
  +- main.cpp
  |
  +- cryptopp700
    |
    +- cryltib.h
    +- ...
    +- libcryptopp.a

您只需要将 cryptopp700/ 添加到您的包含 header 搜索路径 -I:

g++ main.cpp -I ./cryptopp700 ./cryptopp700/libcryptopp.a

请注意,您还应该使用 g++(C++ 编译器),而不是 gcc (C 编译器)。


你也可以安装这个库,因为它已经构建好了。默认情况下,它安装到 /usr/local 中:

skylake:cryptopp$ sudo make install
[sudo] password for jwalton:
install -m 644 *.h /usr/local/include/cryptopp
install -m 644 libcryptopp.a /usr/local/lib
install cryptest.exe /usr/local/bin
install -m 644 TestData/*.dat /usr/local/share/cryptopp/TestData
install -m 644 TestVectors/*.txt /usr/local/share/cryptopp/TestVectors

您可以使用 PREFIX 安装到备用位置:

skylake:cryptopp$ sudo make install PREFIX=/opt/local
install -m 644 *.h /opt/local/include/cryptopp
install -m 644 libcryptopp.a /opt/local/lib
install cryptest.exe /opt/local/bin
install -m 644 TestData/*.dat /opt/local/share/cryptopp/TestData
install -m 644 TestVectors/*.txt /opt/local/share/cryptopp/TestVectors

然后,您可以将编译和 link 命令更改为:

g++ main.cpp -I /usr/local/include/cryptopp -o main.exe /usr/local/lib/libcryptopp.a

安装如下所示后,我通常会告诉人们 运行 自检。不幸的是,如果您所做的只是 make -j 4 或类似的,那么将无法工作。

$ make -j 4
...

$ sudo make install
[sudo] password for jwalton:
install -m 644 *.h /usr/local/include/cryptopp
install -m 644 libcryptopp.a /usr/local/lib
install cryptest.exe /usr/local/bin
install -m 644 TestData/*.dat /usr/local/share/cryptopp/TestData
install -m 644 TestVectors/*.txt /usr/local/share/cryptopp/TestVectors

这是您会得到的错误:

skylake:cryptopp$ cd /opt/local/bin/
skylake:bin$ ./cryptest.exe v
Using seed: 1544189072

Testing Settings...

passed:  Your machine is little endian.
passed:  Aligned data access.
passed:  sizeof(byte) == 1
passed:  sizeof(word16) == 2
passed:  sizeof(word32) == 4
passed:  sizeof(word64) == 8
passed:  sizeof(word128) == 16
passed:  sizeof(hword) == 4, sizeof(word) == 8, sizeof(dword) == 16
passed:  cacheLineSize == 64
hasSSE2 == 1, hasSSSE3 == 1, hasSSE4.1 == 1, hasSSE4.2 == 1, hasAVX == 1, hasAVX2 == 1, hasAESNI == 1, hasCLMUL == 1, hasRDRAND == 1, hasRDSEED == 1, hasSHA == 0, isP4 == 0
...

SHA validation suite running...

Exception caught: Can not open file TestVectors/sha.txt for reading

我的想法是 "just work" 适合您的东西。对于常见情况,您无需担心 CRYPTOPP_DATA_DIR。而且您当然不必使用 RTFM 来使常见情况起作用。这告诉我我们的工程过程存在缺陷。

我们现在要解决这个问题:第 760 期,Make self-tests run after install by a typical user