Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libuv编译最佳实践 #39

Open
wangning0 opened this issue Oct 10, 2017 · 1 comment
Open

libuv编译最佳实践 #39

wangning0 opened this issue Oct 10, 2017 · 1 comment

Comments

@wangning0
Copy link
Owner

wangning0 commented Oct 10, 2017

在学习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

@yifei-zhan
Copy link

沙发??!🛋

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants