Libuv linux搭建
libuv需要自己手动下载源码,并手动编译。
如当前目录为:/work
$git clone https://github.com/libuv/libuv.git
即可下载到libuv源码
如git失败,
git同步遇到报错“fatal: unable to access ' https://github.com/libuv/libuv.git': Peer reports incompatible or unsupported protocol version.”
更新模块即可:
yum update nss curl yum update nss curl
然后$cd libuv 进入到libuv源码目录下,准备编译。
依次运行如下命令:
$ sh autogen.sh
$ ./configure
$ make
$ make check
$ sudo make install
备注:
1.如果提示缺少一些库,请自行安装,apt-get install packagename
如,我运行autogen.sh的时候,就提示缺少automake,libtoolize库:
root@test work:~/test/github/libuv$ sh autogen.sh
autogen.sh: 1: autogen.sh: automake: not found
autogen.sh: 33: test: Illegal number:
autogen.sh: 34: test: Illegal number:
libtoolize --copy
autogen.sh: 43: autogen.sh: libtoolize: not found
运行如下命令,手动安装,即可:
$ sudo apt-get install automake
$ sudo apt-get install libtool
当然也可以用yum进行库的安装。
2.有时报的是没有libtoolize包,如下,我们只需要安装libtool即可
[root@ip-10-242-118-147 FREECIV]# yum install libtoolize
(snip)
No package libtoolize available.
Nothing to do
[root@ip-10-242-118-147 FREECIV]# yum install libtool
(snip)
Installed:
libtool.i386 0:1.5.24-3.fc8
Complete!
编写测试代码:
在/work/test.c
使用gcc或g++进行编译:
g++ -luv test.c -o test.exe
-l指定链接的库,uv为libuv,linux下默认不加lib。
test.c
/*
* empty msg loop
* 这个例子新建了一个消息队列,但队列里没有任何消息,程序直接退出
* Created on 2016/9/10
*/
#include <stdio.h>
#include <stdlib.h>
#include "uv.h"
int main(int argc, char *argv[])
{
uv_loop_t *loop = uv_loop_new(); // 可以理解为新建一个消息队列
uv_run(loop, UV_RUN_DEFAULT); // 启动消息队列,UV_RUN_DEFAULT模式下,当消息数为0时,就会退出消息循环。
printf("hello, worldn");
return 0;
}
编译完成后,执行“./test.exe” 查看运行结果。
如果出现找不到动态库文件,请按下面的方法进行解决
linux也有动态库,其扩展名一般为.so文件。
/etc/ld.so.conf.d
可以在这个目录下建立一个文件,文件名一般为需要引入的动态库名.
如/etc/ld.so.conf.d/libuv.conf中引入需要的动态库目录/usr/local/lib /usr/local/lib64
设置文件成功后,执行ldconfig 命令更新。
注意:有的系统在链接为可执行文件后,执行程序时可能会报错,或者无法连接为可执行程序:
libtest: error while loading shared libraries: libuv.so.1: cannot open shared object file: No such file or directory
网上搜一下,这里主要原因是:ld提示找不到库文件,而库文件就在/usr/local/lib目录下。
链接器ld默认的目录是/lib/和/usr/lib,如果放在其他路径也可以,需要让ld知道库文件在哪里
方案1:
编辑·/etc/ld.so.conf`文件,在新的一行中加入库文件所在目录;
然后执行
$ldconfig
以更新/etc/ld.so.cache文件;
方案2:
在/etc/ld.so.conf.d/目录下新建任何以.conf为后缀的文件,如
$sudo vim libuv.conf
在libuv.conf文件中加入库文件所得目录作为内容;
/usr/local/lib
/usr/local/lib64
运行,如下命令,以更新/etc/ld.so.cache文件;
$sudo ldconfig。
参考:https://www.cnblogs.com/drfxiaoliuzi/p/5881059.html?utm_source=itdadao&utm_medium=referral
http://blog.csdn.net/limite_god/article/details/43565253
http://blog.csdn.net/bingqingsuimeng/article/details/8237869
http://www.cnblogs.com/lisuyun/p/7080401.html
https://quaid.fedorapeople.org/TOS/Practical_Open_Source_Software_Exploration/html/sn-Building_the_Code-configure.html