Libevent Learning II: Windows7 (Win7) under compilation libevent

Source: Internet
Author: User

Linux under the compiled reference source of the Readme file, which is mainly recorded under Windows compilation.

I. Preparatory work

Go to the official website to download the latest stable release version libevent-2.0.22-stable

Website address: http://libevent.org/

Two. Compiling with VS2012

1. Unzip libevent to C:\Users\zhang\Desktop\libevent-2.0.22-stable

2. Open the VS2012 Developer Command Prompt tool, as shown in.

3. Enter the instruction to start compiling as shown in.

A netizen said that before compiling should be added "#define _WIN32_WINNT 0x0500" at the beginning of the following 3 files

Libevent-2.0.22-stable\event_iocp.c
Libevent-2.0.22-stable\evthread_win32.c
Libevent-2.0.21-stable\listener.c

But it doesn't explain why it was added, and I didn't add it, compile and use it.

Later still check the meaning of _win32_winnt, you can refer to: Modify Winver, _win32_winnt and _msc_ver

4. Compilation results

After the compilation succeeds, the following three library files are generated in the Libevent-2.0.22-stable directory.

Libevent.lib
Libevent_core.lib
Libevent_extras.lib

As for the functions of these three library files, you can refer to: libevent study: Libevent The characteristics and structure of the source code

5. Testing

The development tools I use are QT 5.5.1 VS2012.

Create a new console project, create a new "include" folder in the project root directory, and then do the following three steps.

(1) Copy the C:\Users\zhang\Desktop\libevent-2.0.22-stable\include\event2 folder to the project's "include" folder

(2) Copy the C:\Users\zhang\Desktop\libevent-2.0.22-stable\WIN32-Code\event2\event-config.h file to the project's "Include\event2" folder

(3) Copy the C:\Users\zhang\Desktop\libevent-2.0.22-stable\WIN32-Code\tree.h file to the project's "include" folder

Create a new "Lib" folder in the project root directory and copy the generated three library files to the folder.

The pro file looks like this:

[CPP]View PlainCopy  
  1. QT + + Core
  2. QT-= GUI
  3. CONFIG + = c++11
  4. TARGET = Server
  5. CONFIG + = Console
  6. CONFIG-= App_bundle
  7. TEMPLATE = App
  8. SOURCES + = Main.cpp
  9. Includepath + = Include
  10. LIBS + = $ $PWD/lib/libevent.lib \
  11. $ $PWD/lib/libevent_core.lib \
  12. $ $PWD/lib/libevent_extras.lib \
  13. LIBS + =-lws2_32-ladvapi32

Windows socket communication needs to load ws2_32.lib, this everybody knows, as for why need Advapi32.lib, can refer to: LNK2019: unresolved external symbols [email protected]
Then copy the example C:\USERS\ZHANG\DESKTOP\LIBEVENT-2.0.22-STABLE\SAMPLE|H\ELLO-WORLD.C from Libevent to the project and compile it. It is important to note that there are two C2440 at compile time: "Initialize": Cannot convert from "void *" to "Event_base *" error, force a conversion.

This is an example of a simple server.

Three. Compiling with MinGW

1. Download the installation MSYS-1.0.11, rename the Fstab.sample file in C:\MinGW\msys\1.0\etc to Fstab after installation, and change the contents of Fstab to c:/qt/qt5.5.1/tools/mingw492 _32/MINGW, use the MinGW version of Qt, note that there are spaces between the path c:/qt/qt5.5.1/tools/mingw492_32 and/mingw
2. Open MSYS,CD to C:\Users\zhang\Desktop\libevent-2.0.22-stable directory,./configure, Make,make Install, after successful installation, generate a static library, as shown below.

C:\MINGW\MSYS\1.0\LOCAL\LIB\LIBEVENT.A,

C:\MINGW\MSYS\1.0\LOCAL\LIB\LIBEVENT_CORELIBEVENT.A,

C:\MinGW\msys\1.0\local\lib\libevent_extra.a

Copy the three library files to the project "Lib" directory.
Copy the header file shown in the project "Linclude" directory.

3. Testing

Or use that simple server example.

The pro file looks like this:

[CPP]View PlainCopy  
  1. QT + + Core
  2. QT-= GUI
  3. CONFIG + = c++11
  4. TARGET = Server
  5. CONFIG + = Console
  6. CONFIG-= App_bundle
  7. TEMPLATE = App
  8. SOURCES + = Main.cpp
  9. Includepath + = Include
  10. LIBS + = $ $PWD/lib/libevent.a \
  11. $ $PWD/lib/libevent_core.a \
  12. $ $PWD/lib/libevent_extra.a \
  13. LIBS + =-lws2_32-ladvapi32
  14. Defines + = WIN32

Note that compared to the "Use vs ' 2012 compilation" Pro file, the last defines + = WIN32, or compile the project will be error.
Of course, the QT version used for compiling the project is QT 5.5.1 MingW.

http://blog.csdn.net/caoshangpa/article/details/52838156

Libevent Learning II: Windows7 (Win7) under compilation libevent

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.