Home c++ OpenSSL under Windows (using g ++ and MSYS2) - I can not...

OpenSSL under Windows (using g ++ and MSYS2) – I can not build the project

Author

Date

Category

Lord, I’m trying out a C++ program to send email via SMTP server through gmail.com.

I take a very old and very famous project

In it everything is fine and understandable, but it is a project under Visual Studio, and it is 32-bit. And the library (and inkluda from openssl) “stitched” right in the project.

I’m trying to build it using the fresh catch openssl, setting it through MSYS2 command pacman -S mingw-w64-x86_64-openssl

Checking: command pacman -Q | grep ssl prints

libopenssl 1.1.1.g-3
  mingw-w64-x86_64-openssl 1.1.1.g-1
  openssl 1.1.1.g-3

and the path C: \ Programs \ msys64 \ mingw64 \ include has a directory openssl with all the * .h – files

.

I went on link, describing the package, I see the following there binary files (ie, non-inkuludy not – docks and non-mana):

/mingw64/bin/libcrypto-1_1-x64.dll
  /mingw64/bin/libssl-1_1-x64.dll
  /mingw64/bin/openssl.exe
  /mingw64/lib/engines-1_1/capi.dll
  /mingw64/lib/engines-1_1/padlock.dll
  /mingw64/lib/libcrypto.a
  /mingw64/lib/libcrypto.dll.a
  /mingw64/lib/libssl.a
  /mingw64/lib/libssl.dll.a
  /mingw64/lib/pkgconfig/libcrypto.pc
  /mingw64/lib/pkgconfig/libssl.pc
  /mingw64/lib/pkgconfig/openssl.pc

So, I have a directory with the project, it has a file main.cpp, I put all the necessary dependencies, write in the console launched in the directory g ++ -std = C++ 17 -LC: / Programs / msys64 / mingw64 / bin -lssl-1_1-x64 -lcrypto-1_1-x64 -IC: \ Programs \ msys64 \ mingw64 \ include main.cpp -o main.exe and get a great set of linker errors, which begins so:

C: / Programs / msys64 / mingw64 / bin /../ lib / gcc / x86_64-w64-mingw32 / 10.2.0 /../../../../ x86_64-w64-mingw32 / bin / ld.exe: C: \ Users \ Konst \ AppData \ Local \ Temp \ ccF1y7VW.o: main.cpp :( .text + 0x26): undefined reference to `CSmtp :: CSmtp () '
  C: / Programs / msys64 / mingw64 / bin /../ lib / gcc / x86_64-w64-mingw32 / 10.2.0 /../../../../ x86_64-w64-mingw32 / bin / ld. exe: C: \ Users \ Konst \ AppData \ Local \ Temp \ ccF1y7VW.o: main.cpp :( text + 0x45):. undefined reference to `CSmtp :: SetSMTPServer (char const *, unsigned short, bool) '
  C: / Programs / msys64 / mingw64 / bin /../ lib / gcc / x86_64-w64-mingw32 / 10.2.0 /../../../../ x86_64-w64-mingw32 / bin / ld. exe: C: \ Users \ Konst \ AppData \ Local \ Temp \ ccF1y7VW.o: main.cpp :( text + 0x69):. undefined reference to `CSmtp :: SetLogin (char const *) '
  C: / Programs / msys64 / mingw64 / bin /../ lib / gcc / x86_64-w64-mingw32 / 10.2.0 /../../../../ x86_64-w64-mingw32 / bin / ld. exe: C: \ Users \ Konst \ AppData \ Local \ Temp \ ccF1y7VW.o: main.cpp :( text + 0x7c):. undefined reference to `CSmtp :: SetPassword (char const *) '
  C: / Programs / msys64 / mingw64 / bin /../ lib / gcc / x86_64-w64-mingw32 / 10.2.0 /../../../../ x86_64-w64-mingw32 / bin / ld. exe: C: \ Users \ Konst \ AppData \ Local \ Temp \ ccF1y7VW.o: main.cpp :( text + 0x8f):. undefined reference to `CSmtp :: SetSenderName (char const *) '

The same set of error I get, if I do not specify libraries.

and the team assembled

g ++ -std = C++ 17 -LC: / Programs / msys64 / mingw64 / lib -lssl -lcrypto - IC: \ Programs \ msys64 \ mingw64 \ include main.cpp -o main.exe

or the command

g ++ -std = C++ 17 -LC: / Programs / msys64 / mingw64 / lib -llibssl -llibcrypto - IC: \ Programs \ msys64 \ mingw64 \ include main.cpp -o main.exe

be a set of error does not change.

So, I have something wrong in pointing out the -l or -L?

In the initial project uses only 2 libraries called libeay32.lib and ssleay32.lib.

Maybe someone knows what their counterparts have to specify when you build the project in the 64-bit version?

Thank you.


Answer 1, authority 100%

Gentlemen,

Thanks to everyone who took notice of my question.

In fact, my question in the comments was answered by HolyBlackCat , which helps me not for the first time.

And the answer to this question is good not only because it answers “how to build a specific program”, but also because it describes a general way to solve such problems.

Summary: I needed to understand what mistakes I make when I form the compiler startup line. Then I needed to learn how to use the pkg-config utility and just copy the keys that this utility prompts into the compiler start command. And like the icing on the cake – I finally, after 3 years of torment, learned to statically link in windows.

My steps were as follows:

  1. simplify the task to the point of impossibility.

Let’s not send emails, it’s difficult, besides, who reads them now … Let’s just calculate the SHA1 hash

Google example

Correct the error in the example by replacing SHA1 (ibuf, strlen (ibuf), obuf); with SHA1 (ibuf, strlen ((const char *) ibuf), obuf);

Google video

In this video at 18:38 on the screen for a quarter of a second flashes a folder with libs that need to be linked.

You can see that the library is called libcrypto

We iterate over all possible files on the disk that contain this name.

We are lucky on the file llibcrypto-1_1-x64.dll , and the team

g ++. exe -std = C++ 17 -llibcrypto-1_1-x64 -LC: / Programs / msys64 / mingw64 / bin / -g shatest.cpp -o shatest.exe

builds the project successfully.

  • What about static linking? – the attentive reader will ask

Forget it. It’s Windows, baby. This doesn’t happen here.

All my attempts to somehow slip g ++ ‘from lib and the -static switch boiled down to the same error undefined reference to' SHA1 '

  1. Back to the original problem. User7860670 kindly suggested to me that I need to list all C++ files included in the project.

So, on the team

g ++. exe -std = C++ 17 -g main.cpp CSmtp.cpp md5.cpp base64.cpp -llibcrypto-1_1-x64 -llibssl-1_1-x64 -LC: / Programs / msys64 / mingw64 / bin / -o main.exe

the linker starts to swear in a completely new way, and this one already refers to winsock.

I am trying to add winsok to the list of dependencies.

I have been suffering for a while, trying to find library names in -l and paths in -L – compiler switches. Complaining about the problem in the comments and getting very valuable hints. I would have carved them in granite if I had a suitable slab on hand.

I. It is incorrect to write the -l (and, apparently, -L) switches to the left of the set of * .cpp files in the command for g ++. These things should be written on the right.

II. Guessing dependencies is, of course, a fun game, but the big boys just get this information by running (in my case) pkg-config --libs --cflags --static openssl . This command displays information like this:

pkg-config --libs --cflags --static openssl
  -IC: / Programs / msys64 / mingw64 / include -LC: / Programs / msys64 / mingw64 / lib -lssl -lcrypto -lws2_32 -lgdi32 -lcrypt32

-and all you need to do is just copy the necessary keys and paths to the command for g ++

III. To make this possible, you need to install this very pkg-config, which in my case is done by the pacman -S mingw-w64-x86_64-pkg-config command, which you need to write in the msys2-shell window.

IIII. Static compilation for windows exists! It’s hard to believe in it, after I couldn’t do it for several years and was ready to bite off my fingers with envy, seeing how simple it works in the same ubuntu. The whole secret, apparently, is in that very wrong sequence of * .cpp files and -l switches in the g ++ command …

The resulting command

g ++. exe -std = C++ 17 -static -g main.cpp CSmtp.cpp md5.cpp base64 .cpp -lssl -lcrypto -lws2_32 -lgdi32 -lcrypt32 -o main.exe

successfully building the project!

Many thanks to everyone who suggested and generally read up to this point.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions