交叉编译
执行命令:
./configure --prefix=/big/libevent/libevent-2.1.12-stable/_arm_install --host=arm-linux-gnueabihf CC=/arm-gcc/bin/arm-linux-gnueabihf-gcc CXX=/arm-gcc/bin/arm-linux-gnueabihf-g++ --disable-openssl
./configure --prefix=/big/libevent/libevent-2.1.12-stable/_arm_install --host=arm-linux-gnueabihf CC=/arm-gcc/bin/arm-linux-gnueabihf-gcc CXX=/arm-gcc/bin/arm-linux-gnueabihf-g++ --disable-openssl
命令解析
--prefix:安装目录
--host:搞不清楚
CC:如果环境变量里配置了,可以不带路径
CXX:同CXX
--disable-openssl:如果提示openssl错误,就加上。
然后就是make和make install
make
make install
编译和安装完毕
CMakeLists.txt
修改思路
在该CMakeLists.txt中简单实现了arm和ubuntu的兼容。MY_ARCH 如果设置为arm。使用交叉编译,设置为其他字符串,就使用系统默认的gcc和g++。
库路径这里也是,我的x86版本的库路径是:
/big/libevent/libevent-2.1.12-stable/_install,
arm版本的库路径是:
/big/libevent/libevent-2.1.12-stable/_arm_install。
所以按照这个思路,如果是非arm版本需要设置MY_ARCH为ubuntu。即
set(MY_ARCH "ubuntu")
project(libevent_project)
cmake_minimum_required(VERSION 3.8)
message(STATUS "lkmao:CMAKE_SOURCE_DIR -- ${CMAKE_SOURCE_DIR}")
set(MY_ARCH "arm")
if(${MY_ARCH} STREQUAL "arm")
set(CMAKE_C_COMPILER /arm-gcc/bin/arm-linux-gnueabihf-gcc)
set(CMAKE_CXX_COMPILER /arm-gcc/bin/arm-linux-gnueabihf-g++)
set(LIBEVET_INSTALL_PATH "/big/libevent/libevent-2.1.12-stable/_${MY_ARCH}_install")
elseif(MY_ARCH)
set(LIBEVET_INSTALL_PATH "/big/libevent/libevent-2.1.12-stable/_install")
# message(STATUS "error:MY_ARCH -- ${MY_ARCH}")
endif()
message(STATUS "LIBEVET_INSTALL_PATH -- ${LIBEVET_INSTALL_PATH}")
include_directories(
${LIBEVET_INSTALL_PATH}/include
)
# SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -std=c99 -pthread")
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -pthread -g -Wall")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread -lstdc++")
message(STATUS "lkmao:PROJECT_SOURCE_DIR -- ${PROJECT_SOURCE_DIR}")
# add_executable(main_01 main_01.c)
# target_link_libraries(main_01 ${LIBEVET_INSTALL_PATH}/lib/libevent.so)
# add_executable(app main_02.c)
# add_executable(app main_03.c)
# add_executable(app main_04.c)
# add_executable(app main_05.c)
# add_executable(app main_06.c)
# add_executable(app main_07.c)
# add_executable(app main_08.c)
add_executable(app main_09.c)
target_link_libraries(app ${LIBEVET_INSTALL_PATH}/lib/libevent.so)
# add_executable(valgrind1 valgrind1.c)
# add_executable(attribute1 attribute1.c)
# add_executable(attribute2 attribute2.c)
编译脚本:
#!/bin/bash
set -e
rm -rf _build_
mkdir _build_ -p
cmake -S ./ -B _build_
make -C _build_
# ./_build_/main_01
# ./_build_/app
file ./_build_/app
scp ./_build_/app root@192.168.0.30:/home/root
scp /big/libevent/libevent-2.1.12-stable/_arm_install/lib/*so* root@192.168.0.30:/lib
# ./_build_/app p1 p2
# ./_build_/app
# ./_build_/valgrind1
# valgrind ./_build_/valgrind1
# ./_build_/attribute1
# ./_build_/attribute2
# gcc main_01.c -o testEvent -I /big/libevent/libevent-2.1.12-stable/_install/include/ -L /big/libevent/libevent-2.1.12-stable/_install/lib/ -levent
测试源码main_09.c:
#include <sys/types.h>
#include <event2/event-config.h>
#include <event2/listener.h>
#include <stdio.h>
#include <event.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define _DEBUG_INFO
#ifdef _DEBUG_INFO
#define DEBUG_INFO(format, ...) printf("%s:%d $$ "format"\n",__func__,__LINE__ , ##__VA_ARGS__)
#else
#define DEBUG_INFO(format, ...)
#endif
struct private_timer_data{
struct event ev;
struct timeval tv;
};
void timer_1s_cb(int fd,short event,void *arg){
struct private_timer_data * ptd = (struct private_timer_data*)arg;
DEBUG_INFO("fd = %d, event = %d",fd,(int)event);
DEBUG_INFO("event = %d,0x%02x %s %s %s %s",event,event,
((event & EV_TIMEOUT)?"EV_TIMEOUT":""),
((event & EV_READ)?"EV_READ":""),
((event & EV_WRITE)?"EV_WRITE":""),
((event & EV_PERSIST)?"EV_PERSIST":"")
);
static int count = 0;
count++;
if(count >= 2){
event_del(&ptd->ev);
free(ptd);
}
}
void timer_5s_cb(int fd,short event,void *arg){
struct private_timer_data * ptd = (struct private_timer_data*)arg;
DEBUG_INFO("fd = %d, event = %d",fd,(int)event);
DEBUG_INFO("event = %d,0x%02x %s %s %s %s",event,event,
((event & EV_TIMEOUT)?"EV_TIMEOUT":""),
((event & EV_READ)?"EV_READ":""),
((event & EV_WRITE)?"EV_WRITE":""),
((event & EV_PERSIST)?"EV_PERSIST":"")
);
static int count = 0;
count++;
if(count >= 2){
event_del(&ptd->ev);
free(ptd);
}
}
void* base_01_thread(void *arg){
struct event_base *base = (struct event_base *)arg;
while(event_base_get_num_events(base,EVENT_BASE_COUNT_ADDED) == 0){
sleep(1);
}
DEBUG_INFO("%d %d %d",
event_base_get_num_events(base,EVENT_BASE_COUNT_ADDED),
event_base_get_num_events(base,EVENT_BASE_COUNT_VIRTUAL),
event_base_get_num_events(base,EVENT_BASE_COUNT_ACTIVE));
event_base_dispatch(base);
event_base_free(base);
DEBUG_INFO("集合下班了");
return NULL;
}
int main(int argc, char *argv[]){
struct event_base *base;
DEBUG_INFO("libevent version = %s",event_get_version());
base = event_base_new();
if(base == NULL){
DEBUG_INFO("event_base_new error");
}
;
pthread_t __attribute__((unused)) t1;
pthread_t __attribute__((unused)) t2;
if(pthread_create(&t1,NULL,base_01_thread,base) < 0){
perror((const char*)"pthread_create");
exit(-1);
}
struct private_timer_data * ptd;
//设置1秒定时器
ptd = (struct private_timer_data*)malloc(sizeof(struct private_timer_data));
ptd->tv.tv_sec = 1;
ptd->tv.tv_usec = 0;
// evtimer_set(&ptd->ev,timer_1s_cb,ptd);
event_set((&ptd->ev), -1, EV_PERSIST, (timer_1s_cb), (ptd));
event_base_set(base,&ptd->ev);
event_add(&ptd->ev,&ptd->tv);
//设置5秒定时器
ptd = (struct private_timer_data*)malloc(sizeof(struct private_timer_data));
ptd->tv.tv_sec = 5;
ptd->tv.tv_usec = 0;
// evtimer_set(&ptd->ev,timer_5s_cb,ptd);
event_set((&ptd->ev), -1, EV_PERSIST, (timer_5s_cb), (ptd));
event_base_set(base,&ptd->ev);
event_add(&ptd->ev,&ptd->tv);
DEBUG_INFO("%d %d %d",
event_base_get_num_events(base,EVENT_BASE_COUNT_ADDED),
event_base_get_num_events(base,EVENT_BASE_COUNT_VIRTUAL),
event_base_get_num_events(base,EVENT_BASE_COUNT_ACTIVE));
while(event_base_get_num_events(base,EVENT_BASE_COUNT_ADDED) > 0){
sleep(1);
}
sleep(1);
DEBUG_INFO("byebye");
return 0;
}
运行编译脚本,并到ARM机器上运行。