Autumn_Ning_Blog icon indicating copy to clipboard operation
Autumn_Ning_Blog copied to clipboard

libuv编译最佳实践

Open wangning0 opened this issue 8 years ago • 1 comments
trafficstars

在学习libuv框架时,编译使用它会出现各种各样的情况,现总结一下我发现的最佳实践

  • 第一步

    首先在github上去将代码下载到本地

  • 第二步

    假设现在你有一个项目是webserver,所在的目录是/tmp/webserver

    1

    则你可以先到你第一步下载好的目录下,输入命令

    git archive --prefix="libuv/" HEAD | (cd /tmp/webserver ; tar -xf -)
    

    然后进入到/tmp/webserver下,看目录

    2

  • 第三步

    进入到/tmp/webserver/libuv目录下,然后根据readme的要求进行编译

    ./autogen.sh
    ./configure
    make
    

    过程中,如果出现了什么报错,一般都是缺少了第三方工具,原则就是缺什么补什么就可以。

    执行完之后,你会发现在/tmp/webserver/libuv/.libs目录下,会出现很多文件,其中最重要的就是生成的静态连接库libuv.a

    3

  • 第四步

    随便编写一个文件webserver.c

    // webserver.c
    // 只是为了可以运行起来
    #include <stdio.h>
    #include <uv.h>
    
    int64_t counter = 0;
    void wait_for_a_while(uv_idle_t* handle) {
        counter++;
        if (counter >= 10e6)
            uv_idle_stop(handle);
    }
    int main() {
        uv_idle_t idler;
        uv_idle_init(uv_default_loop(), &idler);
        uv_idle_start(&idler, wait_for_a_while);
        printf("Idling...\n");
        uv_run(uv_default_loop(), UV_RUN_DEFAULT);
        uv_loop_close(uv_default_loop());
        return 0;
    }
    
  • 第五步

    编写Makefile

    UV_PATH=$(shell pwd)/libuv
    UV_LIB=$(UV_PATH)/.libs/libuv.a
    CFLAGS=-g -Wall -I$(UV_PATH)/include
    LIBS=
    
    uname_S=$(shell uname -s)
    
    ifeq (Darwin, $(uname_S))
    CFLAGS+=-framework CoreServices
    endif
    
    ifeq (Linux, $(uname_S))
    LIBS=-lrt -ldl -lm -pthread
    endif
    
    webserver: webserver.c
            gcc $(CFLAGS) -o webserver webserver.c $(UV_LIB) $(LIBS)
    

    运行make

  • 第六步

    ./webserver

wangning0 avatar Oct 10 '17 08:10 wangning0

沙发??!🛋

yifei-zhan avatar Oct 10 '17 08:10 yifei-zhan