Libevent Mounting and Libevent timers

Source: Internet
Author: User

Mainly refer to the method on the Internet, http://blog.sina.com.cn/s/blog_4b93170a0100mbm9.htmland the actual operation of their own, for their own appearance of some situations to explain. and practice in the process of actually compiling links.These are actually some basic simple operation, but for me this rookie level of small white, still very good, master please mouth under mercy.
This paper is mainly for Mac system, and also applicable to Linux system.
First from Libevent's official website (http://libevent.org) Download the compressed package, here I next is to so far the latest version, Libevent-2.0.5-stable, after the completion of the decompression.
Command line into Libevent's home directoryCD libevent-2.0.5-stable
Then execute the command:./CONFIGURE-PREFIX=/USRHere is the configuration library installed in the/USR folder
Then there will be a bunch of checking, no matter what they are.
Then execute the command: Make

Then install with administrator privilegesCommand:Make Install
By this, the installation is complete.

Let's see if Libevent is actually installed successfully, execute the command:ls-al/usr/lib | grep libeventIf a similar result occurs, the installation succeeds:

Below we carry out a simple timer program to test the libevent.Code reference the following classmate: http://blog.csdn.net/happyanger6/article/details/7272433
#include <event.h> #include <iostream>using namespace std;struct event ev;struct timeval tv;void TIME_CB (int FD, short event, void *ARGC) {    //printf ("Timer wakeup\n");//If you use this output instead of the output below, there is no output in my computer, and if it is an interface function, it will be stuck. I don't know why.    cout<< "Timer Wakeup" <<endl;    Event_add (&ev, &TV); Reschedule Timer}int Main () {    cout<< "test" <<endl;    struct Event_base *base = Event_init ();     Initialize    tv.tv_sec = 2;//2s period     Set timing time    tv.tv_usec = 0;    Evtimer_set (&ev, TIME_CB, NULL);    Initialize the event of interest and set the callback function    Event_add (&ev, &TV);        Register event    cout<< "Start" <<endl;    Event_base_dispatch (base);  Enter message loop and message distribution    cout<< "End" <<endl;    return 0;}



If you use the command line to compile, pay attention to add libevent external link library, I just did not add, have been, get a long time to fix.If the directg++-o a a.cppThen the following error will occur
This means that the header file is not included, so we compile it with the libevent include path.Command:g++-o a a.cpp-i/usr/includeThen compile without errors, but the link is wrong:
This is why there is no link to the Libevent library. In the case of a large number of cases are under the Windows IDE vs inside the program, the command line under the compile link did not get,After checking the link command on the Internet, we can link to it as follows:g++-o a a.cpp/usr/lib/libevent.a-i/usr/includeThis compiles the link through.

To run the program:. /A

At this point, the installation of Libevent and the first example of Libevent are implemented.
Also, what if the link is not compiled at the command line? How does it work? The following is for QT5 to explain.In fact, you just need to add the Include directory and LIB link library to the project file. Pro.Includepath += /usr/includeLIBS += /usr/lib/libevent.a:

Run the program, you can see the results in the application output:

OK, finally realized the basic operation.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Libevent Mounting and Libevent timers

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.