karastojko / mailio

mailio is a cross platform C++ library for MIME format and SMTP, POP3 and IMAP protocols. It is based on standard C++ 17 and Boost library.
Other
372 stars 98 forks source link

Problem when linking the "simple smtp message" example with mingw. #164

Closed Patatedelespace closed 4 months ago

Patatedelespace commented 4 months ago

Hi everyone, I have reproduce the example code of "simple smtp message" in a file, like this :

#include <mailio/message.hpp>
#include <mailio/smtp.hpp>

using mailio::message;
using mailio::mail_address;
using mailio::smtps;
using mailio::smtp_error;
using mailio::dialog_error;
using std::cout;
using std::endl;

int main()
{
    try
    {
        // create mail message
        message msg;
        msg.from(mail_address("mailio library", "mailio@gmail.com"));// set the correct sender name and address
        msg.add_recipient(mail_address("mailio library", "mailio@gmail.com"));// set the correct recipent name and address
        msg.subject("smtps simple message");
        msg.content("Hello, World!");

        // connect to server
        smtps conn("smtp.gmail.com", 587);
        // modify username/password to use real credentials
        conn.authenticate("mailio@gmail.com", "mailiopass", smtps::auth_method_t::START_TLS);
        conn.submit(msg);
    }
    catch (smtp_error& exc)
    {
        cout << exc.what() << endl;
    }
    catch (dialog_error& exc)
    {
        cout << exc.what() << endl;
    }

    return EXIT_SUCCESS;
}

When I compile this file with Mingw64 with this command :

$ x86_64-w64-mingw32-g++ -o ./build/flemme_de_trouver_un_nom ./main.cpp -static-libgcc -static-libstdc++ --static -I/home/patate/Prog/CPP/include/ -I/opt/vcpkg/installed/x64-linux/include -L/opt/vcpkg/installed/x64-linux/lib/ -lcrypto -lmailio -lssl -lboost_system -lboost_container -lboost_coroutine -lboost_regex -lboost_exception -lboost_date_time -lboost_context

I get those errors :

/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccYycvfZ.o:main.cpp:(.text$_ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh[_ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh]+0x74): undefined reference to `__imp_WSAStartup'

/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccYycvfZ.o:main.cpp:(.text$_ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE[_ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE]+0x35): undefined reference to `__imp_WSACleanup'

/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccYycvfZ.o:main.cpp:(.text$_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei[_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei]+0x1d): undefined reference to `ERR_reason_error_string'

/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccYycvfZ.o:main.cpp:(.text$_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei[_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei]+0x36): undefined reference to `ERR_lib_error_string'

collect2: erreur: ld a retourné le statut de sortie 1

Is that because of missing libraries ? Or just a bad use of Mingw64 ?

Thanks for your help.

karastojko commented 4 months ago

By default mailio is compiled as dynamic library. Seems you are trying to link it as static. How did you compile mailio, what is the library file that was produced by the build?

Patatedelespace commented 4 months ago

I tried to compile the program without the --static flag, like that :

x86_64-w64-mingw32-g++ -o ./build/flemme_de_trouver_un_nom ./main.cpp -static-libgcc -static-libstdc++ -I/home/patate/Prog/CPP/include/ -I/opt/vcpkg/installed/x64-linux/include -L/opt/vcpkg/installed/x64-linux/lib/ -lcrypto -lmailio -lssl -lboost_system -lboost_container -lboost_coroutine -lboost_regex -lboost_exception -lboost_date_time -lboost_context

and I have got those errors :

/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0xce): undefined reference to `mailio::message::message()'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x14e): undefined reference to `mailio::mail_address::mail_address(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> > const&)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x161): undefined reference to `mailio::message::from(mailio::mail_address const&)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x22e): undefined reference to `mailio::mail_address::mail_address(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> > const&)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x241): undefined reference to `mailio::message::add_recipient(mailio::mail_address const&)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x2d1): undefined reference to `mailio::message::subject(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x333): undefined reference to `mailio::mime::content(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x3c6): undefined reference to `mailio::smtps::smtps(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, unsigned int, std::chrono::duration<long long, std::ratio<1ll, 1000ll> >)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x474): undefined reference to `mailio::smtps::authenticate(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> > const&, mailio::smtps::auth_method_t)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text+0x4db): undefined reference to `mailio::smtp::submit[abi:cxx11](mailio::message const&)'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text$_ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh[_ZN5boost4asio6detail17winsock_init_base7startupERNS2_4dataEhh]+0x74): undefined reference to `__imp_WSAStartup'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text$_ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE[_ZN5boost4asio6detail17winsock_init_base7cleanupERNS2_4dataE]+0x35): undefined reference to `__imp_WSACleanup'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text$_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei[_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei]+0x1d): undefined reference to `ERR_reason_error_string'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text$_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei[_ZNK5boost4asio5error6detail12ssl_category7messageB5cxx11Ei]+0x36): undefined reference to `ERR_lib_error_string'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.text$_ZN6mailio5smtpsD1Ev[_ZN6mailio5smtpsD1Ev]+0x22): undefined reference to `mailio::smtp::~smtp()'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.rdata$.refptr._ZTVN6mailio7messageE[.refptr._ZTVN6mailio7messageE]+0x0): undefined reference to `vtable for mailio::message'
/usr/lib/gcc/x86_64-w64-mingw32/13.1.0/../../../../x86_64-w64-mingw32/bin/ld: /tmp/ccPu0uyf.o:main.cpp:(.rdata$.refptr._ZTVN6mailio4mimeE[.refptr._ZTVN6mailio4mimeE]+0x0): undefined reference to `vtable for mailio::mime'
collect2: erreur: ld a retourné le statut de sortie 1
x86_64-w64-mingw32-g++ -o ./build/flemme_de_trouver_un_nom ./main.cpp          4,69s user 0,63s system 14% cpu 36,415 total

I've downloaded mailio with vcpkg and that produce those libraries :

patate@ArchdePatate /opt/vcpkg/installed/x64-linux/lib % ls -la           
total 21428
drwxr-xr-x 3 root root     4096 24 avril 13:06 .
drwxr-xr-x 8 root root     4096 24 avril 13:06 ..
-rw-r--r-- 2 root root   546180 24 avril 13:04 libboost_container.a
-rw-r--r-- 2 root root    29050 24 avril 13:04 libboost_context.a
-rw-r--r-- 2 root root   172410 24 avril 13:04 libboost_coroutine.a
-rw-r--r-- 2 root root     8546 24 avril 13:04 libboost_date_time.a
-rw-r--r-- 2 root root     7246 24 avril 13:04 libboost_exception.a
-rw-r--r-- 2 root root  4695844 24 avril 13:04 libboost_regex.a
-rw-r--r-- 2 root root     6636 24 avril 13:04 libboost_system.a
-rw-r--r-- 2 root root 10994600 24 avril 13:06 libcrypto.a
-rw-r--r-- 2 root root  3561862 24 avril 13:06 libmailio.a
-rw-r--r-- 2 root root  1882240 24 avril 13:06 libssl.a
drwxr-xr-x 2 root root     4096 24 avril 13:06 pkgconfig

and

patate@ArchdePatate /opt/vcpkg/packages/mailio_x64-linux/lib % ls -la
total 3492
drwxr-xr-x 3 root root    4096 24 avril 13:06 .
drwxr-xr-x 6 root root    4096 24 avril 13:06 ..
-rw-r--r-- 2 root root 3561862 24 avril 13:06 libmailio.a
drwxr-xr-x 2 root root    4096 24 avril 13:06 pkgconfig

thanks for your help

karastojko commented 4 months ago

The reported problems are related to OpenSSL. Since you are using the static libraries, do not remove the --static flag as in the second attempt.

Because the linking order matters, can you please use -lssl -lcrypto in this sequence? So, rewrite -lcrypto -lmailio -lssl to be -lmailio -lssl -lcrypto. If this does not work, according to StackOVerflow, libcrypto depends on libdl and libpthread, so maybe it's worth trying also to add -ldl -lpthread at the end of your gcc command.