使用外部库进行编译
Compiling with external libraries
我在从 github 项目编译基于 C++ 的 jwt 库的示例(示例代码)之一时遇到问题:jwt-cpp 我克隆了它并使用自述文件中提供的步骤对其进行了编译,这似乎很成功。
之后我做了一个ldconfig
.
现在,我正在尝试构建示例代码。
#include <iostream>
#include "jwt/jwt_all.h"
using json = nlohmann::json;
int main()
{
// Setup a signer
HS256Validator signer("secret!");
// Create the json payload that expires 01/01/2017 @ 12:00am (UTC)
json payload = {{"sub", "subject"}, {"exp", 1483228800}};
// Let's encode the token to a string
auto token = JWT::Encode(signer, payload);
std::cout << token << std::endl;
}
我使用终端上的命令编译它,上面写着:
g++ -std=c++11
-I/usr/local/include
-I/usr/local/include
/usr/local/lib/libjwt.a
/usr/local/lib/libcrypto.a
sign.cpp -o sign
它会导致以下错误:
/tmp/ccX4ghoR.o: In function `main':
sign.cpp:(.text+0x24e): undefined reference to
JWT::Encode(MessageSigner const&, nlohmann::basic_json<std::map,
std::vector, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer> const&,
nlohmann::basic_json<std::map, std::vector,
std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer>)'
/tmp/ccX4ghoR.o: In function
`HS256Validator::HS256Validator(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)': sign.cpp:(.text._ZN14HS256ValidatorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN14HS256ValidatorC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x21): undefined reference to `EVP_sha256' sign.cpp:(.text._ZN14HS256ValidatorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE[_ZN14HS256ValidatorC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE]+0x5f): undefined reference to `HMACValidator::HMACValidator(std::__cxx11::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&, evp_md_st
const*, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> > const&)'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x20): undefined
reference to `HMACValidator::Verify(nlohmann::basic_json<std::map,
std::vector, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer> const&, unsigned char
const*, unsigned long, unsigned char const*, unsigned long) const'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x28): undefined
reference to `HMACValidator::toJson[abi:cxx11]() const'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x38): undefined
reference to `MessageValidator::Accepts(nlohmann::basic_json<std::map,
std::vector, std::__cxx11::basic_string<char, std::char_traits<char>,
std::allocator<char> >, bool, long, unsigned long, double,
std::allocator, nlohmann::adl_serializer> const&) const'
/tmp/ccX4ghoR.o:
(.rodata._ZTV14HS256Validator[_ZTV14HS256Validator]+0x40): undefined
reference to `HMACValidator::Sign(unsigned char const*, unsigned long,
unsigned char*, unsigned long*) const'
/tmp/ccX4ghoR.o: In function `HS256Validator::~HS256Validator()':
sign.cpp:
(.text._ZN14HS256ValidatorD2Ev[_ZN14HS256ValidatorD5Ev]+0x20):
undefined reference to `HMACValidator::~HMACValidator()'
/tmp/ccX4ghoR.o:
(.rodata._ZTI14HS256Validator[_ZTI14HS256Validator]+0x10): undefined
reference to `typeinfo for HMACValidator'
collect2: error: ld returned 1 exit status
我试过什么:
我已经通读了这些问题,以了解我可能做错了什么: 如何使用库并使用linux终端的外部库编译C文件,尽管第二个问题专门针对C而不是C++,但那里的问题和解决方案也适用于C++。
我还尝试了命令行编译的变体,例如,
g++ -std=c++11
-I/usr/local/include
-I/usr/local/include
-L/usr/local/lib/
-lcrypto -ljwt
sign.cpp -o sign
我熟悉这样一个事实,即当我做一个-lfoo
时,链接器试图在-L
选项提供的位置找到一个libfoo.a。
我已经确认用于编译的选项包含它们应该包含的内容。
对于-I
选项:我可以在/usr/local/include
中看到jwt
和openssl
目录对于-L
选项:我可以看到libjwt.a
,libssl.a
,libcrypto.a
,libssl.so
等/usr/local/lib/
问题: 我在编译此示例时做错了什么?
当我告诉你这个问题时,你会踢自己。
将源文件放在程序中的库声明之前sign.cpp
:
g++ -std=c++11 -I/usr/local/include -L/usr/local/lib sign.cpp -lcrypto -ljwt -o sign
原因是 unix 链接器不会在命令行中向后查看以解析依赖项。有几个命令行开关(例如--start-group
和--end-group
),这将调整此行为,但上述内容将使您暂时畅通无阻。 更多细节在这里。
在上面的示例中,我冒昧地删除了重复的 INCLUDE 路径参数。您可能甚至不需要-I/usr/local/include
或-L/usr/local/lib
部分,因为它通常已经在标准编译器路径中。
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- 尝试从 XamlApplication 编译 C++/WinRT 空白应用程序时,我收到未解析的外部符号 winrt_make_*
- 良好做法:如何定义用于编译的外部库的路径
- 交叉编译时包括外部库
- C++ 为什么在定义的编译和链接之前引用外部实例的程序
- 我正在为学校开发一个小班模板实验室,我的代码无法编译,它给了我一个未解析的外部符号
- 如何使用Makevars链接到RCPP中已经编译的外部共享库
- C++编译错误LNK2019:未解决的外部符号错误
- 错误 LNK 2019 在 c++ 中编译期间未解析的外部生成错误
- 编译示例 Cinder 项目时存在大量未解决的外部问题
- 是否可以编译具有在编译时无法解析的外部依赖项的静态库?
- QT-不能使用外部库进行交叉编译
- 编译轨道样品时的C 错误-LNK2019未解决的外部符号
- 在Linux上编译外部库
- 使用外部库进行编译
- 在 gcc 中使用数组参数编译外部"c"代码
- 你如何告诉Visual Studio编译外部库
- QtCreator:如何编译外部源文件
- 编译外部C++库以用于 iOS 项目
- 编译外部库时涉及增量(C1073)编译的错误