使用外部库进行编译 [英] Compiling with external libraries

查看:139
本文介绍了使用外部库进行编译的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在为来自github项目的基于C ++的jwt库编译示例(示例代码)之一时遇到问题: jwt-cpp 我克隆了它,并使用README文件中提供的步骤对其进行了编译,这似乎很成功.

之后,我做了 ldconfig .

现在,我正在尝试构建示例代码.

  #include< iostream>#include"jwt/jwt_all.h"使用json = nlohmann :: json;int main(){//设置签名者HS256Validator签名者(秘密!");//创建将于2017年1月1日@ 12:00 am(UTC)过期的json有效负载json负载= {{"sub","subject"},{"exp",1483228800}};//让我们将令牌编码为字符串自动令牌= JWT :: Encode(signer,payload);std :: cout<<令牌<<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号 

它会导致以下错误:

 /tmp/ccX4ghoR.o:在函数"main"中:sign.cpp :(.text + 0x24e):未定义的引用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:在功能中HS256Validator :: HS256Validator(std :: __ cxx11 :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>常量&安培). ':sign.cpp :( text._ZN14HS256ValidatorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE [_ZN14HS256ValidatorC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE] + 0×21):未定义参考`EVP_sha256' sign.cpp :( text._ZN14HS256ValidatorC2ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE [_ZN14HS256ValidatorC5ERKNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE] + 0x5F的):未定义参考`HMACValidator ::HMACValidator(std :: __ cxx11 :: basic_string< char,std :: char_traits< char>,std :: allocator< char>>const& ;, evp_md_stconst *,std :: __ cxx11 :: basic_string< char,std :: char_traits< char> ;,std :: allocator< char>>const&)'/tmp/ccX4ghoR.o:(.rodata._ZTV14HS256Validator [_ZTV14HS256Validator] + 0x20):未定义引用`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& ;,未签名的字符const *,无符号长整数,无符号字符const *,无符号长整数)const'/tmp/ccX4ghoR.o:(.rodata._ZTV14HS256Validator [_ZTV14HS256Validator] + 0x28):未定义引用"HMACValidator :: toJson [abi:cxx11]()const"/tmp/ccX4ghoR.o:(.rodata._ZTV14HS256Validator [_ZTV14HS256Validator] + 0x38):未定义引用`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):未定义引用`HMACValidator :: Sign(unsigned char const *,unsigned long,unsigned char *,unsigned long *)const'/tmp/ccX4ghoR.o:在函数"HS256Validator ::〜HS256Validator()"中:sign.cpp:(.text._ZN14HS256ValidatorD2Ev [_ZN14HS256ValidatorD5Ev] + 0x20):未定义对`HMACValidator :: ~~ HMACValidator()'的引用/tmp/ccX4ghoR.o:(.rodata._ZTI14HS256Validator [_ZTI14HS256Validator] + 0x10):未定义引用用于HMACValidator的typeinfo"collect2:错误:ld返回1退出状态 

我尝试过什么:

我已仔细阅读以下问题,以了解自己可能做错了什么:如何使用库解决方案

当我告诉您问题时,您会踢自己.

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

I am having an issue with compiling one of the sample (example codes) for a C++ based jwt library from a github project: jwt-cpp I cloned it and compiled it using the steps provided in the README file, which seemed successful.

After that I did a ldconfig.

Now, I am trying to build the example code.

#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;
}

I compile this with a command on terminal, which says:

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

And it results in following error:

/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

What have I tried:

I have read through these questions to know what I might be doing wrong: How to use libraries and compile C file using external library from linux terminal, Although second question is specifically for C and not C++, the problem there and the solution is applicable to C++ as well.

I have also tried variants of command line compilation such as,

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

I am familiar with the fact that when I do a -lfoo, the linker tries to find a libfoo.a at the location provided with -L option.

I have confirmed that the options that are used for compilation contains what they should.
For -I options: I can see jwt and openssl directories in /usr/local/include
For -L options: I can see libjwt.a, libssl.a, libcrypto.a, libssl.so, etc at /usr/local/lib/

Question: What am I doing wrong to compile this example?

解决方案

You're gonna kick yourself when I tell you the issue.

Put the source file, sign.cpp before the library declarations in your program:

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

The reason being is that the unix linker doesn't look backwards in the command line to resolve dependencies. There's a few command line switches (e.g. --start-group and --end-group) that will adjust this behavior, but the above will get you unblocked for now. More details here.

In the above example, I took the liberty of removing the duplicated INCLUDE path arguments. You probably don't even need the -I/usr/local/include or -L/usr/local/lib part because that's typically already in the standard compiler path.

这篇关于使用外部库进行编译的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆