5

I am trying to make SSL work with boost ASIO. I am on windows and using MingW 6.3.

I built OpenSSL 1.1, 1.0 and 0.8 with MingW when I try to link them to the project I always get different errors (depending on what is missing). Right now I am trying to make work OpenSSL 1.1.0g but I have those error even though I give manually the path to the libs with CMAKE:

This is the errors if I build my projct with OpenSSL 1.1.0g :

"C:\Program Files\JetBrains\CLion 2017.2.3\bin\cmake\bin\cmake.exe" --build C:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer\cmake-build-debug --target AuthServer -- -j 2
"C:\Program Files\JetBrains\CLion 2017.2.3\bin\cmake\bin\cmake.exe" -HC:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer -BC:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 AuthServer
mingw32-make.exe[1]: Entering directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
"C:\Program Files\JetBrains\CLion 2017.2.3\bin\cmake\bin\cmake.exe" -HC:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer -BC:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer\cmake-build-debug --check-build-system CMakeFiles\Makefile.cmake 0
"C:\Program Files\JetBrains\CLion 2017.2.3\bin\cmake\bin\cmake.exe" -E cmake_progress_start C:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer\cmake-build-debug\CMakeFiles 6
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\Makefile2 CMakeFiles/AuthServer.dir/all
mingw32-make.exe[2]: Entering directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\AuthServer.dir\build.make CMakeFiles/AuthServer.dir/depend
mingw32-make.exe[3]: Entering directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
"C:\Program Files\JetBrains\CLion 2017.2.3\bin\cmake\bin\cmake.exe" -E cmake_depends "MinGW Makefiles" C:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer C:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer C:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer\cmake-build-debug C:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer\cmake-build-debug C:\Users\myuser\Documents\MagesBattleAuthServerCPP\AuthServer\cmake-build-debug\CMakeFiles\AuthServer.dir\DependInfo.cmake --color=
mingw32-make.exe[3]: Leaving directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
C:/MinGW/bin/mingw32-make.exe -f CMakeFiles\AuthServer.dir\build.make CMakeFiles/AuthServer.dir/build
mingw32-make.exe[3]: Entering directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
[ 16%] Linking CXX executable bin\Windows\AuthServer.exe
"C:\Program Files\JetBrains\CLion 2017.2.3\bin\cmake\bin\cmake.exe" -E cmake_link_script CMakeFiles\AuthServer.dir\link.txt --verbose=1
"C:\Program Files\JetBrains\CLion 2017.2.3\bin\cmake\bin\cmake.exe" -E remove -f CMakeFiles\AuthServer.dir/objects.a
C:\MinGW\bin\ar.exe cr CMakeFiles\AuthServer.dir/objects.a @CMakeFiles\AuthServer.dir\objects1.rsp
C:\MinGW\bin\g++.exe -g   -Wl,--whole-archive CMakeFiles\AuthServer.dir/objects.a -Wl,--no-whole-archive  -o bin\Windows\AuthServer.exe -Wl,--out-implib,lib\libAuthServer.dll.a -Wl,--major-image-version,0,--minor-image-version,0 @CMakeFiles\AuthServer.dir\linklibs.rsp
CMakeFiles\AuthServer.dir/objects.a(main.cpp.obj): In function `ZN5boost4asio3ssl6detail17openssl_init_base7do_initD1Ev':
c:/users/myuser/documents/magesbattleauthservercpp/include/boost/asio/ssl/detail/impl/openssl_init.ipp:89: undefined reference to `CONF_modules_unload'
CMakeFiles\AuthServer.dir/objects.a(main.cpp.obj): In function `ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei':
c:/users/myuser/documents/magesbattleauthservercpp/include/boost/asio/ssl/impl/error.ipp:39: undefined reference to `ERR_reason_error_string'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\AuthServer.dir\build.make:217: recipe for target 'bin/Windows/AuthServer.exe' failed
mingw32-make.exe[3]: Leaving directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
CMakeFiles\Makefile2:69: recipe for target 'CMakeFiles/AuthServer.dir/all' failed
mingw32-make.exe[2]: Leaving directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
mingw32-make.exe[3]: *** [bin/Windows/AuthServer.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/AuthServer.dir/all] Error 2
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/AuthServer.dir/rule' failed
mingw32-make.exe[1]: Leaving directory 'C:/Users/myuser/Documents/MagesBattleAuthServerCPP/AuthServer/cmake-build-debug'
Makefile:120: recipe for target 'AuthServer' failed
mingw32-make.exe[1]: *** [CMakeFiles/AuthServer.dir/rule] Error 2
mingw32-make.exe: *** [AuthServer] Error 2

CMAKE :

cmake_minimum_required(VERSION 3.8)
project(AuthServer)

#----------------- MACROS -----------------------------
#FIND THE CORRECT VERSION OF WINNT
macro(get_WIN32_WINNT version)
    if (CMAKE_SYSTEM_VERSION)
        set(ver ${CMAKE_SYSTEM_VERSION})
        string(REGEX MATCH "^([0-9]+).([0-9])" ver ${ver})
        string(REGEX MATCH "^([0-9]+)" verMajor ${ver})
        # Check for Windows 10, b/c we'll need to convert to hex 'A'.
        if ("${verMajor}" MATCHES "10")
            set(verMajor "A")
            string(REGEX REPLACE "^([0-9]+)" ${verMajor} ver ${ver})
        endif ("${verMajor}" MATCHES "10")
        # Remove all remaining '.' characters.
        string(REPLACE "." "" ver ${ver})
        # Prepend each digit with a zero.
        string(REGEX REPLACE "([0-9A-Z])" "0\\1" ver ${ver})
        set(${version} "0x${ver}")
    endif(CMAKE_SYSTEM_VERSION)
endmacro(get_WIN32_WINNT)
#----------------- MACROS -----------------------------

set(CMAKE_VERBOSE_MAKEFILE ON)

set(CMAKE_CXX_STANDARD 14)

SET(CMAKE_FIND_LIBRARY_PREFIXES "lib")

add_definitions(-DBOOST_NETWORK_ENABLE_HTTPS)

set(SOURCE_FILES
        AuthServer/Client.cpp
        AuthServer/Client.h
        AuthServer/main.cpp
        AuthServer/NetworkManager.cpp
        AuthServer/NetworkManager.h
        AuthServer/Message.cpp
        AuthServer/Message.h
        AuthServer/JsonSerializable.h
        AuthServer/JsonDeserializerFactory.cpp
        AuthServer/JsonDeserializerFactory.h)

if (WIN32) #windows

    #set the correct WINNET version
    get_WIN32_WINNT(ver)
    add_definitions(-D_WIN32_WINNT=${ver})

    set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} -lws2_32 -lwsock32 -lmswsock")

    #set the correct lib build type .a because we are using mingw
    if(${CMAKE_BUILD_TYPE} MATCHES "debug")
        SET(CMAKE_FIND_LIBRARY_SUFFIXES "-mgw63-mt-d-1_65_1.a" ".a")
    ELSEIF(${CMAKE_BUILD_TYPE} MATCHES "release")
        SET(CMAKE_FIND_LIBRARY_SUFFIXES "-mgw63-mt-1_65_1.a" ".a")
    ENDIF()

    #find all the libs
    find_library(BOOST_LIBRARY_atomic
            NAMES boost_atomic
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_chrono
            NAMES boost_chrono
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_date_time
            NAMES boost_date_time
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_regex
            NAMES boost_regex
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_thread
            NAMES boost_thread
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_system
            NAMES boost_system
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_iostreams
            NAMES boost_iostreams
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_zlib
            NAMES boost_zlib
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(LIBRARY_ossl_ssl
            NAMES ssl
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(LIBRARY_ossl_crypto
            NAMES crypto
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/win/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    add_executable(AuthServer ${SOURCE_FILES})
ELSEif(${CMAKE_SYSTEM_NAME} MATCHES "Darwin") #osx

    SET(CMAKE_OSX_ARCHITECTURES x86_64)
    SET(CMAKE_FIND_LIBRARY_SUFFIXES ".a" ".dylib")

    find_library(BOOST_LIBRARY_atomic
            NAMES boost_atomic
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_chrono
            NAMES boost_chrono
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_date_time
            NAMES boost_date_time
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_regex
            NAMES boost_regex
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_thread
            NAMES boost_thread
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_system
            NAMES boost_system
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_iostreams
            NAMES boost_iostreams
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    find_library(BOOST_LIBRARY_zlib
            NAMES boost_zlib
            PATHS ${CMAKE_SOURCE_DIR}/../Lib/osx/x64/${CMAKE_BUILD_TYPE}
            NO_DEFAULT_PATH)

    add_executable(AuthServer ${SOURCE_FILES})
endif()

target_include_directories(AuthServer PUBLIC ${CMAKE_SOURCE_DIR}/../Include)


target_link_libraries(AuthServer
        PUBLIC
        ${BOOST_LIBRARY_chrono}
        ${BOOST_LIBRARY_atomic}
        ${BOOST_LIBRARY_date_time}
        ${BOOST_LIBRARY_regex}
        ${BOOST_LIBRARY_thread}
        ${BOOST_LIBRARY_system}
        ${BOOST_LIBRARY_iostreams}
        ${BOOST_LIBRARY_zlib}
        ${LIBRARY_ossl_ssl}
        ${LIBRARY_ossl_crypto}
        )


set_target_properties(AuthServer
        PROPERTIES
        ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
        LIBRARY_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/lib"
        RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin/${CMAKE_SYSTEM_NAME}"
        )

Command to build OpenSSL (mingw msys2)

$ ./Configure mingw64 no-shared no-asm --prefix=/C/OpenSSL-x64
$ make && make install

linklibs.rsp

C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_chrono-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_atomic-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_date_time-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_regex-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_thread-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_system-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_iostreams-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libboost_zlib-mgw63-mt-d-1_65_1.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libssl.a
 C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libcrypto.a
 -lkernel32 -luser32 -lgdi32 -lwinspool -lshell32 -lole32 -loleaut32 -luuid
 -lcomdlg32 -ladvapi32 -lws2_32 -lwsock32 -lmswsock -ldl -lpthread 

nm of both my compiled OpenSSL libs :

myuser@LHCFB203 MINGW64 ~
$ nm /C/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libcrypto.a | grep 'CONF_modules_unload'
00000000000005c0 T CONF_modules_unload

myuser@LHCFB203 MINGW64 ~
$ nm /C/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libcrypto.a | grep 'ERR_reason_error_string'
0000000000004de0 T ERR_reason_error_string

Am I using the wrong version of OpenSSL ? Should I not use OpenSSL ? Am I building it wrong ?

EDIT : Added -ldl and -lpthread but that did not help.

rXp
  • 617
  • 2
  • 12
  • 25
  • Are you not able to use the [binary distribution](https://sourceforge.net/projects/boost/files/boost-binaries/) of the boost libraries? – user23573 Nov 07 '17 at 06:54
  • @user23573 I am using MingW therefore am using *.a not *.lib. I did not find any mingw precompiled boost WITH zlib, openssl etc. Everything is working but when I include OpenSSL I'm having problem. – rXp Nov 07 '17 at 06:57
  • Show your link commands. – n. m. could be an AI Nov 07 '17 at 07:15
  • @n.m. I use CMAKE and I import it manually (since I built it) with find_library, I updated with my CMAKE – rXp Nov 07 '17 at 07:22
  • Ah, the beauty of CMAKE. Frees you completely from all kinds of worries about the libraries, now does it? – n. m. could be an AI Nov 07 '17 at 08:57
  • @n.m. So ? I'm here to find help why I have these undefined references not here to talk about the good and bad of cmake. If my cmake script has mistakes tell me but if it is not the problem... – rXp Nov 07 '17 at 09:12
  • CMake is another unknown in your equation. When it works, all is great. When it doesn't, you have to debug the issue. It is absolutely essential that any CMake user knows how to produce a full build log. [It's not hard](https://stackoverflow.com/questions/2670121/using-cmake-with-gnu-make-how-can-i-see-the-exact-commands). Separations of concerns and all that. – n. m. could be an AI Nov 07 '17 at 09:37
  • @n.m. Ah I didn't put the full build log because I thought the error were only coming from the library files since if I remove my include of asio/ssl everything works fine. I added it now. – rXp Nov 07 '17 at 09:49
  • There is no -lssl or -lcrypto or ssl.a or crypto.a mentioned in the build log, so it looks like the cmake commands related to these libraries have no effect. – n. m. could be an AI Nov 07 '17 at 10:12
  • No wait, you have an .rsp file so you better post its contents too. – n. m. could be an AI Nov 07 '17 at 10:16
  • @n.m. I added the linklibs.rsp – rXp Nov 07 '17 at 10:42
  • It looks like libssl and libcrypto are in the wrong order, you probably want to swap LIBRARY_ossl_crypto and LIBRARY_ossl_ssl in target_link_libraries in cmakefile. – n. m. could be an AI Nov 07 '17 at 10:52
  • @n.m. I swapped them and they got swapped too in the rsp files but the same undefined references error are thrown. – rXp Nov 07 '17 at 11:02
  • I don't understand how a name like libssl-mgw63-mt-d-1_65_1.a comes into being. This looks like a boost library naming scheme. If you build openssl separately, there ought to be libssl-1.1.0g.a or similar name somewhere. Same about libcrypto. – n. m. could be an AI Nov 07 '17 at 11:19
  • @n.m. Yes but I didn't find a way to tell find_library to not use the current suffix so I renamed them so the suffix matches. When the libs are built the names are libssl.a and libcrypto.a, that's it. – rXp Nov 07 '17 at 11:29
  • It looks like there's a header/library mismatch. You compile against ssl 1.1.0 headers but link against earlier libraries. Earlier libraries don't have CONF_modules_unload but boost detects 1.1.0 headers and adds the call. I suggest you find a way to avoid renaming of the libraries, and furthermore keep all headers and library files under their own package directories. If you have -I...ssl1.1.0/include and -L...ssl1.1.0/lib, there's much less potential for confusion. – n. m. could be an AI Nov 07 '17 at 11:39
  • @n.m. I added what happens when I try to find the missing function in my libs. As you can see both are in my generated libs. – rXp Nov 07 '17 at 11:53
  • I can only see they are found in libcrypto.a, not in C:/Users/myuser/Documents/MagesBattleAuthServerCPP/Lib/win/x64/debug/libcrypto-mgw63-mt-d-1_65_1.a you actually link with. It is important to have ONE copy of a lib and do everything with that one copy. – n. m. could be an AI Nov 07 '17 at 12:56
  • @n.m. There I changed it so the nm is on the copied/renamed lib. – rXp Nov 07 '17 at 13:02
  • hmm sorry, CONF_modules_unload seems to be defined in earlier versions of openssl, something else is wrong. – n. m. could be an AI Nov 07 '17 at 13:10
  • All I can suggest is take the boost [openssl/asio example](http://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/example/ssl/client.cpp) and try to build it manually first with `-lboost_system -lssl -lcrypto` or similar, and then with a cmakefile similar to yours. – n. m. could be an AI Nov 07 '17 at 14:06
  • also add `-ldl -lpthread`. (This is from [another stackoverflow](https://stackoverflow.com/questions/14674360/building-openssl-source-for-compiling-boost)) – user23573 Nov 08 '17 at 06:38
  • @user23573 You are sure about that ? Because I am not on linux but on Windows + MingW. Meaning that libpthread does not exist here. I went ahead and built a port of libdl.a but there is no libpthread. Also I don't have any undefined references to dl or pthread but to crypto itself. – rXp Nov 08 '17 at 08:21
  • Well I added pthread (through msys) and dl but nothing changed. – rXp Nov 08 '17 at 09:48
  • @rXp, no, I'm not sure about it - it was just a guess from another SO question (link) – user23573 Nov 09 '17 at 08:59
  • Could it be an issue with the link order? – user23573 Nov 09 '17 at 09:09
  • @user23573 That's what I thought but I tried to swap crypto and ssl and ld and pthread and nothing changed. – rXp Nov 09 '17 at 09:14
  • Maybe you don't just need to swap, you need to add libcrypto.a again at the end of the linker command line.Or, you need to add it immediately after the module that calls libcrypto, and then again at the end. – user23573 Nov 10 '17 at 07:05
  • Your question didn't really provide any answers that helped me, but your question just reminded me to include the openssl libs to my -l flags and that solved my problem, so thanks! – skittlebiz Jan 10 '22 at 23:40

1 Answers1

0

I had this problem because I accidentally used a x86 (32bit) version of openssl ( from https://www.nuget.org/packages/openssl-vc141/ which didn't state the machine) and linked it in my x64 project with an x64 boost asio.

VS2017 compiled and only complained during linking about missing two symbols:

LNK2001 unresolved external symbol CONF_modules_unload  
LNK2001 unresolved external symbol ERR_reason_error_string

Dumpbin showed those symbols to exists but then I saw that the symbol (and the whole library) was compiled for x86... facepalm; Weird that the linker wouldn't complain about the different machines...

I switched to this version and all compiled/linked correctly:

https://www.nuget.org/packages/openssl-vc140-vc141-x86_64/

Oliver Zendel
  • 2,695
  • 34
  • 29