linux libuv demo 編譯 ,製作make檔案
在學習libuv框架時,編譯使用它會出現各種各樣的情況,現總結一下我發現的最佳實踐
- 第一步
首先在github上去將程式碼下載到本地
- 第二步
假設現在你有一個專案是webserver
,所在的目錄是/tmp/webserver
,
則你可以先到你第一步下載好的目錄下,輸入命令
git archive --prefix="libuv/" HEAD | (cd /tmp/webserver ; tar -xf -)
然後進入到/tmp/webserver下,看目錄
- 第三步
進入到/tmp/webserver/libuv
目錄下,然後根據readme的要求進行編譯
./autogen.sh
./configure
過程中,如果出現了什麼報錯,一般都是缺少了第三方工具,原則就是缺什麼補什麼就可以。
執行完之後,你會發現在/tmp/webserver/libuv/.libs
目錄下,會出現很多檔案,其中最重要的就是生成的靜態連線庫libuv.a
- 第四步
隨便編寫一個檔案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)
}
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
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