使用外部库编译 我在编译一个基于Github项目的基于C++的JWT库的示例(示例代码)时遇到了问题: 我克隆了它,并使用自述文件中提供的步骤编译了它,这似乎是成功的

使用外部库编译 我在编译一个基于Github项目的基于C++的JWT库的示例(示例代码)时遇到了问题: 我克隆了它,并使用自述文件中提供的步骤编译了它,这似乎是成功的,c++,compiler-errors,compilation,linker-errors,C++,Compiler Errors,Compilation,Linker Errors,之后,我做了一个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:00a

之后,我做了一个
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;
}
它会导致以下错误:

/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
我熟悉这样一个事实:当我执行
-lfoo
时,链接器会尝试在提供
-L
选项的位置查找libfoo.a

我已确认用于编译的选项包含它们应该包含的内容。
对于
-I
选项:
我可以在
/usr/local/include

对于
-L
选项:
我可以在
/usr/local/lib/
上看到
libjwt.a
libcrypto.a
libssl.so

问题:
编译这个例子我做错了什么?

当我告诉你这个问题时,你会自责的

将源文件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
部分,因为这通常已经在标准编译器路径中

g++ -std=c++11 \
 -I/usr/local/include \
 -I/usr/local/include \
 -L/usr/local/lib/ \
 -lcrypto -ljwt \
 sign.cpp -o sign
g++ -std=c++11 -I/usr/local/include -L/usr/local/lib sign.cpp  -lcrypto -ljwt -o sign