Gentlemen, I have a serious and important question, over which I have been fighting my head on the keyboard for over a week now. I am even ready to discuss the remuneration of someone who will help me. The question is: I need to be taught how to correctly tick the linker settings for VS 2015, so that my libevent library links to the project in c ++ (and, possibly, build this library correctly).

Background: I read a couple of motivating articles about the libevent library (for example, this one: https://habrahabr.ru/post/217437/ ). I started with the fact that I made a development machine for Ubuntu 16 LTS, where I put g ++ and compiled a library of source codes for instructions from the github. ( https://github.com/libevent/libevent ) Then I wrote a simple program test.cpp, in which I tried to initialize the library:

#include <memory> #include <cstdint> #include <iostream> #include <evhttp.h> #include <string> #include <fstream> using namespace std; int main(){ cout<< "Hi!" << endl ; if ( !event_init()) cout << "Failed to init libevent." << endl; else cout << "libevent init successfully!" << endl; return 0; } 

After a bit of confusion, I normally assembled this program with the g ++ -std = c ++ 11 -o test.ex test.cpp -levent command and started it. ./test.ex Everything works. This is great. Then I rewrote a slightly more complicated example from the examples ... But as soon as I started writing myself, I needed debugging. And my own system in this sense is Windows. Well, you understand ... relatives do not choose ...

And under Windows everything is not so smooth. First, you need to build a library from source. According to the instructions, I do this with CMake:

 "CMake (Windows) Install CMake: http://www.cmake.org $ md build && cd build $ cmake -G "Visual Studio 10" .. # Or whatever generator you want to use $ start libevent.sln" 

Cmake at assembly swears at the lack of OpenSSL, and no dances with tambourines help any more: in order to use OpenSSL under Windows, it is necessary to define three magic environment variables

 OPENSSL_ROOT_DIR OPENSSL_INCLUDE_DIR OPENSSL_CRYPTO_LIBRARY 

and how to define these variables is not written anywhere. Fortunately, if the door is closed - you can check the window ... libevent allows you to assemble yourself without OpenSSL support, by adding the key -DEVENT__DISABLE_OPENSSL = on, that is, using the cmake -DEVENT__DISABLE_OPENSSL = on -G "Visual Studio 14 2015" command Some warnings that this command wrote to me, its output can be seen here: le_pic0.jpg But the * .sln file is being created, and after running the compilation, I received a set of * .lib files in the Debug directory: event.lib, event_core.lib, event_extra.lib. le_pic1.jpg But then the problems started. No dancing with a tambourine allows me to link these libraries to a C ++ project. For the sake of completeness: in my system, the path to these * .lib files is C: \ Programs \ includes \ libevent \ lib \ Debug; I got them here: le_pic2.jpg and here: le_pic3.jpg and here: le_pic4.jpg and even here: le_pic5.jpg

The last attempt created the illusion that something happened. that is, the project is properly assembled. But when I started, I instantly got an error: le_pic6.jpg

Then I made two more pathetic attempts to understand: linking (because the assembly of the obj-file passes without errors, note) under Windows from the command line (the result is the same, does not see the library) le_pic7.jpg and try to compare the contents of obj files using the nm utility under Linux and the dumpbin under Windows with the / symbols option. But the difference is too great, I just see in the Windows object file that _event_init is UNDEFined. le_pic8.jpg In general, I did not succeed. Please help me, for example, by suggesting how to determine the magic variables for OpenSSL (maybe the library just refreshed itself properly assembled?), Or by trying to build this project with this library, perhaps you can.

Thanks in advance for the tips.

    2 answers 2

    Gentlemen, thank you very much for the advice, they helped me, and the tip on cyberforum.ru especially helped me. So, I, as a reasonable person and have already tried to run all of this under linux and effortlessly started it, argued that there could be no special initialization of winsock, because the library, everything must be done inside it! And it was precisely this opinion that I wrote in response to the advice to do something with the winsocket with my hands.

    It turned out that I was wrong.

    The hint sounded like this: "Look at the example of libevent / sample / hello-world.c - you need to call it." Exactly ... Yes, I began to compare, and I found in the dispenser where in one case the initialization takes place, and in the other - it doesn't happen. It turned out that under Windows it is REALLY necessary at the very beginning of the main function to write

     #ifdef _WIN32 WSADATA wsa_data; WSAStartup(0x0201, &wsa_data); #endif 

    As they say, never was, and here again!

    No, it never bothers!

    Thanks to everyone who helped me.

    (my personal opinion is that it is necessary to write about such things on each fence. in order to get out of work, you see a fence - and there it’s written about winsocks under Windows).

      Gentlemen, I have got new successes: as it turned out, I have DLLs being created during the libevent assembly. I just did not immediately notice them. Now I copy them to the same folder where the EXEs are. Now I have the next stage: the program

        #include <memory> #include <cstdint> #include <iostream> #include <evhttp.h> using namespace std; int main() { if (!event_init()) { cout << "Failed to init libevent." << endl; } else { cout << "Libevent initialised!" << endl; char const SrvAddress[] = "192.168.10.53"; uint16_t SrvPort = 5555; unique_ptr<evhttp, decltype(&evhttp_free)> Server(evhttp_start(SrvAddress, SrvPort), &evhttp_free); if (!Server) { cout << "Failed to init http server." << std::endl; return -1; } void(*OnReq)(evhttp_request *req, void *) = [](evhttp_request *req, void *) { auto *OutBuf = evhttp_request_get_output_buffer(req); if (!OutBuf) return; evbuffer_add_printf(OutBuf, "<html><body><center><h1>Hello World! under Windows!</h1></center></body></html>"); evhttp_send_reply(req, HTTP_OK, "", OutBuf); }; evhttp_set_gencb(Server.get(), OnReq, nullptr); if (event_dispatch() == -1) { cout << "Failed to run messahe loop." << std::endl; return -1; } } return 0; } 

      going and running, and after starting it gives an error

       C:\MyProjects\Cpp\LibeEx1\Debug>LibeEx1.exe [warn] evsig_init_: socketpair: Either the application has not called WSAStartup, or WSAStartup failed. Libevent initialised! [warn] socket: Either the application has not called WSAStartup, or WSAStartup failed. Failed to init http server. 

      after which it is completed.

      • pull the WSAStartup handles. Any winsock tutorial tells you how to do it - KoVadim
      • Took the guide from here citforum.ru/book/cook/winsock.shtml I tried, but the error has not changed. I think that pulling a winnow from my program is not a solution, because the library, which I am trying to link to my project, should "pull" it. Winsok2 should already be linked to it, otherwise it would not have gathered. In general, while - it is not clear what to do ... - SH
      • I went through debugging up to the line in which the program cannot create a socket. The picture shows what values ​​the variables had at the moment when the socket could not be created. Look, maybe in something understandable thing? junecat.ru/Storage/oth/le_pic11.jpg - SH