1

I complie my c++ program by input:

g++ main.cpp crypto/Sign.cpp -o main -lssl -lcrypto && ./main

Unfortunately, I got these output:

/tmp/ccEDu0Qv.o: In function `Sign::md_ctx_sign_init(SignatureScheme, evp_md_ctx_st*)':
Sign.cpp:(.text+0x1b): undefined reference to `EVP_MD_CTX_reset'
Sign.cpp:(.text+0x104): undefined reference to `EVP_sha3_224'
Sign.cpp:(.text+0x137): undefined reference to `EVP_sha3_256'
Sign.cpp:(.text+0x16a): undefined reference to `EVP_sha3_384'
Sign.cpp:(.text+0x196): undefined reference to `EVP_sha3_512'
/tmp/ccEDu0Qv.o: In function `Sign::md_ctx_veri_init(SignatureScheme, evp_md_ctx_st*)':
Sign.cpp:(.text+0x22f): undefined reference to `EVP_MD_CTX_reset'
Sign.cpp:(.text+0x318): undefined reference to `EVP_sha3_224'
Sign.cpp:(.text+0x34b): undefined reference to `EVP_sha3_256'
Sign.cpp:(.text+0x37e): undefined reference to `EVP_sha3_384'
Sign.cpp:(.text+0x3aa): undefined reference to `EVP_sha3_512'
/tmp/ccEDu0Qv.o: In function `Sign::sign_hash(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, evp_md_ctx_st*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
Sign.cpp:(.text+0x4a9): undefined reference to `EVP_MD_CTX_free'
Sign.cpp:(.text+0x53f): undefined reference to `EVP_MD_CTX_free'
/tmp/ccEDu0Qv.o: In function `Sign::get_public_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
Sign.cpp:(.text+0x697): undefined reference to `EVP_PKEY_get0_EC_KEY'
/tmp/ccEDu0Qv.o: In function `Sign::get_private_key(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
Sign.cpp:(.text+0x85b): undefined reference to `EVP_PKEY_get0_EC_KEY'
/tmp/ccEDu0Qv.o: In function `Sign::EC_sign(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, SignatureScheme)':
Sign.cpp:(.text+0xa4a): undefined reference to `EVP_MD_CTX_new'
/tmp/ccEDu0Qv.o: In function `Sign::EC_veri(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, SignatureScheme)':
Sign.cpp:(.text+0xc56): undefined reference to `EVP_MD_CTX_new'
collect2: error: ld returned 1 exit status

So, what's wrong happend? How can I fix this problem?

The openssl I use is:

#:~$ which -a openssl
/usr/local/bin/openssl
/usr/bin/openssl
#:~$ openssl version
OpenSSL 1.1.1-pre5 (beta) 17 Apr 2018

In crypto/Sign.h, it include:

#include <openssl/conf.h>
#include <openssl/ec.h>    // for EC_GROUP_new_by_curve_name, EC_GROUP_free, EC_KEY_new, EC_KEY_set_group, EC_KEY_generate_key, EC_KEY_free
#include <openssl/ecdsa.h> // for ECDSA_do_sign, ECDSA_do_verify
#include <openssl/err.h>
#include <openssl/evp.h>

#include <iostream>
#include <string>
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
Nash
  • 43
  • 9
  • 1
    C and C++ are very different languages. Only tag the language you're actually program in. – Some programmer dude Apr 27 '18 at 13:16
  • 1
    "So, what's wrong happend?" - you obviously forgot to link whatever defines those functions *or* you linked stuff in the wrong order - the user of a symbol must generally be listed before the provider of the symbol, or the symbol will be discarded by the linker. – Jesper Juhl Apr 27 '18 at 13:17
  • 2
    The order in which you link things is important. – UKMonkey Apr 27 '18 at 13:19
  • Possible duplicate of [g++ linking order dependency when linking c code to c++ code](https://stackoverflow.com/questions/3363398/g-linking-order-dependency-when-linking-c-code-to-c-code) – UKMonkey Apr 27 '18 at 13:19
  • 1
    @Yksisarvinen putting the libraries *before* the users would be the *wrong* order! The linker looks for users of symbols then for providers. If it encounters a provider of a symbol that it has not seen a user for (which you get by listing libs first) it will discard that symbol. – Jesper Juhl Apr 27 '18 at 13:21
  • @JesperJuhl Oops, my bad. You're right of course. I'll delete my comment so it won't create confusion. – Yksisarvinen Apr 27 '18 at 13:23
  • @codekaizer yes, this is in Ubuntu 16.04LTS – Nash Apr 27 '18 at 13:40
  • this is weird. use `pkg-config` to check if `openssl` libs and cflags have values. Use those for your `-L` and `-I` respectively. – Joseph D. Apr 27 '18 at 13:41
  • @Someprogrammerdude sorry, I'm a new programmer, I will be pay attention next time. In fact, the language I program is C++. – Nash Apr 27 '18 at 13:42
  • @codekaizer I input: g++ main.cpp crypto/Sign.cpp 'pkg-config -lssl -lcrypto' -o main && ./main But I got: g++: error: pkg-config -lssl -lcrypto: No such file or directory So the command I use is right? I'm beginning with programming. – Nash Apr 27 '18 at 13:46
  • @codekaizer Sorry, The fllowing things I get now: #$ pkg-config --cflags openssl -I/usr/local/include – Nash Apr 27 '18 at 13:55
  • @codekaizer This time, I input: g++ crypto/Sign.cpp main.cpp -I/usr/local/include/ -o main && ./main, but I still got the same mistake. – Nash Apr 27 '18 at 14:01
  • @codekaizer #$ pkg-config --libs openssl -L/usr/local/lib -lssl -lcrypto – Nash Apr 27 '18 at 14:03
  • @codekaizer OK, I input: : g++ crypto/Sign.cpp main.cpp -lssl -lcrypto -o main && ./main, and still mistake. – Nash Apr 27 '18 at 14:08
  • @codekaizer thanks, I will check that. – Nash Apr 27 '18 at 14:30
  • @codekaizer Now, I input: g++ main.cpp crypto/Sign.cpp \`pkg-config --cflags --libs openssl\` -o main && ./main The mistake missing, and now problem appear: ./run.sh: line 1: 90415 Segmentation fault (core dumped) ./main , it may be my code's problem. Thank's for your help! – Nash Apr 27 '18 at 14:37
  • @Nash, good to know! – Joseph D. Apr 27 '18 at 14:40

0 Answers0