V. Panchenko
V. Panchenko

Reputation: 814

Can't link C++ project with openssl static libraries

I have some project done on C++ and I need to compile it for Windows XP and later.

This is the header file of my RsaEncryptor class (I think it is no matter to post sources here, because I'm sure on 100% that it works fine, but if it is required I can do it):

#ifndef RSAENCRYPTOR_H_
#define RSAENCRYPTOR_H_

#include <stdexcept>
#include <openssl/rsa.h>
#include <openssl/engine.h>
#include <openssl/pem.h>

// One of this paddings can be used
//#define PADDING RSA_PKCS1_OAEP_PADDING
#define PADDING RSA_PKCS1_PADDING
//#define PADDING RSA_NO_PADDING

class RsaEncryptor {

private:

    RSA* publicKey;
    RSA* privateKey;

public:

    RsaEncryptor() {
        publicKey = nullptr;
        privateKey = nullptr;
    }

    ~RsaEncryptor() {
        if ( this->publicKey )
            RSA_free( this->publicKey );
        if ( this->privateKey )
            RSA_free( this->privateKey );
    }

    size_t GetCipherBytesCount() {
        return 172; //is default for 1024 bit key length
    }

    void SetPublicKeyFromString(const std::string& content);
    void SetPrivateKeyFromString(const std::string& content);

    std::string Encrypt(const std::string& plainData);
    std::string Decrypt(const std::string& cipherData);

};


#endif /* RSAENCRYPTOR_H_ */

I use latest version of Eclipse Neon for C++ development on Windows 10 x64 machine.

Also I have read this:

(...) you will need to configure with no-async when building OpenSSL 1.1.0 and above for Windows XP or Vista

And I was guided by following instructions.

What did I do:

  1. install Ubuntu 17.04 x64 inside Virtual Box under Windows 10 x64.

  2. download latest version of OpenSSL library from Ubuntu

  3. install mxe and all requirements (but can't compile with it and I decide to use mingw32)

  4. install mingw32 via root@user-pc:/home/user# apt-get install gcc-mingw-w64-i686

  5. unpack OpenSSL library in /home/user/openssl-x32

  6. go to /home/user/openssl-x32

  7. run ./Configure mingw --cross-compile-prefix=i686-w64-mingw32 --prefix=/home/user/builds/openssl-x32-static no-shared no-async

  8. run make

  9. run make install

  10. copy libssl.a and libcrypto.a from /home/user/builds/openssl-x32-static/lib to host machine (Windows 10 x64) to folder E:\MyProjects\my-app\libraries\

  11. copy include directory from Ubuntu to host machine in folder E:\MyProjects\my-app\include

  12. Add libraries folder to linker configurations

  13. Add include folder to compiler configurations

  14. Then, I build my project and got following answer:

11:58:09 **** Rebuild of configuration Debug for project app **** Info: Configuration "Debug" uses tool-chain "MinGW GCC" that is unsupported on this system, attempting to build anyway. Info: Internal Builder is used for build g++ "-IC:\MyProjects\my-app\include" -O0 -g3 -Wall -c -std=c++11 -m32 -o RsaEncryptor.o "..\RsaEncryptor.cpp" g++ "-IC:\MyProjects\my-app\include" -O0 -g3 -Wall -c -std=c++11 -m32 -o main.o "..\main.cpp" In file included from ..\main.cpp:5:0: ..\FilesFinder.h: In member function 'void FilesFinder::FindRecursively(const string&, const FilesFilter&)': ..\FilesFinder.h:90:56: warning: comparison between signed and unsigned integer expressions [-Wsign-compare] while (destinationContainer.GetElementsCount() >= MAX_ELEMENTS_COUNT) { ^ g++ "-IC:\MyProjects\my-app\include" -O0 -g3 -Wall -c -std=c++11 -m32 -o aes256.o "..\aes256.cpp" g++ "-LC:\MyProjects\my-app\libraries" -static-libgcc -static-libstdc++ -static -lpthread -m32 -o C:/MyProjects/my-app/bin/Debug/app RsaEncryptor.o aes256.o main.o -lssl -lcrypto -lgdi32 C:\MyProjects\my-app\libraries\libcrypto.a(b_addr.o):b_addr.c:(.text+0xaa): undefined reference to _imp__getnameinfo@28' C:\MyProjects\my-app\libraries\libcrypto.a(b_addr.o):b_addr.c:(.text+0xe0): undefined reference to_imp__ntohs@4' C:\MyProjects\my-app\libraries\libcrypto.a(b_addr.o):b_addr.c:(.text+0x242): undefined reference to gai_strerrorW' C:\MyProjects\my-app\libraries\libcrypto.a(b_addr.o):b_addr.c:(.text+0x820): undefined reference to_imp__freeaddrinfo@4' C:\MyProjects\my-app\libraries\libcrypto.a(b_addr.o):b_addr.c:(.text+0xb5d): undefined reference to _imp__getaddrinfo@16' C:\MyProjects\my-app\libraries\libcrypto.a(b_addr.o):b_addr.c:(.text+0xba2): undefined reference togai_strerrorW' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0xd7): undefined reference to _imp__WSAStartup@8' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0xe8): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x2c7): undefined reference to _imp__WSAStartup@8' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x2d8): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x3b1): undefined reference to _imp__ntohs@4' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x40c): undefined reference to_imp__getsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x423): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x43c): undefined reference to_imp__gethostbyname@4' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x48b): undefined reference to _imp__WSAStartup@8' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x4a2): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x52c): undefined reference to _imp__WSACleanup@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x54d): undefined reference to_imp__ioctlsocket@12' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x563): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x6b7): undefined reference to_imp__WSAStartup@8' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x6c8): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x942): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0x9dc): undefined reference to _imp__setsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0xa15): undefined reference to_imp__ioctlsocket@12' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0xa32): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0xaa6): undefined reference to_imp__getsockname@12' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock.o):b_sock.c:(.text+0xb22): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x27): undefined reference to_imp__socket@12' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x4e): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x120): undefined reference to_imp__setsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x15b): undefined reference to _imp__connect@12' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x1f5): undefined reference to_imp__setsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x206): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x265): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x2c6): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x377): undefined reference to_imp__getsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x38f): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x4a7): undefined reference to_imp__setsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x4b4): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x53a): undefined reference to_imp__bind@12' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x55f): undefined reference to _imp__listen@8' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x5a5): undefined reference to_imp__setsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x5b6): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x63a): undefined reference to_imp__setsockopt@20' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x64b): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x6b2): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x713): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x7a6): undefined reference to_imp__accept@12' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x7e7): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x84b): undefined reference to_imp__closesocket@4' C:\MyProjects\my-app\libraries\libcrypto.a(b_sock2.o):b_sock2.c:(.text+0x86c): undefined reference to _imp__closesocket@4' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x1b1): undefined reference to_imp__WSASetLastError@4' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x1d0): undefined reference to _imp__send@16' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x20a): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x272): undefined reference to _imp__WSASetLastError@4' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x299): undefined reference to_imp__send@16' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x2ca): undefined reference to _imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x33d): undefined reference to_imp__WSASetLastError@4' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x360): undefined reference to _imp__recv@16' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x39a): undefined reference to_imp__WSAGetLastError@0' C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o):bss_sock.c:(.text+0x457): undefined reference to _imp__WSAGetLastError@0' c:/programs/mingw_w64_mingw32_gcc_stdthread_win32/bin/../lib/gcc/i686-w64-mingw32/4.8.1/../../../../i686-w64-mingw32/bin/ld.exe: C:\MyProjects\my-app\libraries\libcrypto.a(bss_sock.o): bad reloc address 0x24 in section.rdata' c:/programs/mingw_w64_mingw32_gcc_stdthread_win32/bin/../lib/gcc/i686-w64-mingw32/4.8.1/../../../../i686-w64-mingw32/bin/ld.exe: final link failed: Invalid operation collect2.exe: error: ld returned 1 exit status

11:58:21 Build Finished (took 11s.567ms)

I need just link my completed C++ project with correct-compiled (with flag no-async) OpenSSL library to let it work under old versions of Windows.

I will be happy to get any help with this issue.

Upvotes: 1

Views: 3100

Answers (1)

Joseph Artsimovich
Joseph Artsimovich

Reputation: 1519

Most if not all of the undefined symbols in your error log come from Winsock. Try linking with -lws2_32.

Upvotes: 2

Related Questions