简体   繁体   English

如何使用 libuv 编译最小程序?

[英]How can I compile a minimum program with libuv?

It's been quite a while since I wrote a program in C, and even so I always found the actual compiling and linking quite confusing.自从我用 C 编写程序以来已经有一段时间了,即便如此,我仍然发现实际的编译和链接非常混乱。

Since I've been playing / working with node.js lately, I have become curious enough to start taking a peek under the hood and am currently looking at libuv.由于我最近一直在玩/使用 node.js,我已经变得足够好奇,开始窥视引擎盖,目前正在研究 libuv。

I've found some excellent guides, but have found the actual compiling part has been largely skipped over.我找到了一些优秀的指南,但发现实际的编译部分在很大程度上被跳过了。 Mostly likely due to the fair assumption that whoever is interesting probably works with gcc a lot.很可能是由于公平的假设,即感兴趣的人可能经常使用 gcc。

I've downloaded the latest libuv from gtihub as a zip and have unzipped in into a working folder.我已经从 gtihub 以 zip 格式下载了最新的 libuv,并将其解压缩到一个工作文件夹中。 I compiled and installed it following README.md.我按照 README.md 编译并安装了它。 All this went fine and without an any issues.所有这一切都很顺利,没有任何问题。

The code I'm tying to compile comes from http://nikhilm.github.io/uvbook/basics.html我要编译的代码来自http://nikhilm.github.io/uvbook/basics.html

#include <stdio.h>
#include <uv.h>

int main() {
    uv_loop_t *loop = uv_loop_new();

    printf("Now quitting.\n");
    uv_run(loop, UV_RUN_DEFAULT);

    return 0;
}

I've saved this as main.c我把它保存为 main.c

This is the Makefile I am using, which I suspect is the problem as it's cobbled together from various sources and my knowledge in this area is cloudy to say the least.这是我正在使用的 Makefile,我怀疑这是问题所在,因为它是从各种来源拼凑而成的,至少可以说我在这方面的知识是模糊的。

main: main.c
    gcc -g  -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I./libuv-master/include/ -o main main.c -pthread -lrt -lm
clean:
    rm main

This is the result of running make.这是运行make的结果。

/tmp/ccJbU03z.o: In function `main':
/home/tom/libuv-test/main.c:5: undefined reference to `uv_loop_new'
/home/tom/libuv-test/main.c:8: undefined reference to `uv_run'
collect2: error: ld returned 1 exit status

I realise that this not exactly specific to libuv, but this is just where I'm at, so any help would be much appreciated.我意识到这并不完全特定于 libuv,但这正是我所处的位置,因此非常感谢任何帮助。

I'm using Ubuntu 13.04我正在使用 Ubuntu 13.04

The most simple Makefile I could make work is this, but obviously it is specific to the location of libuv.a on my system.我可以制作的最简单的 Makefile 就是这个,但显然它特定于我系统上 libuv.a 的位置。 I would welcome an edit / new post of this answer that provides a more generic compile line.我欢迎这个答案的编辑/新帖子,它提供了一个更通用的编译行。

main: main.c
    gcc -o main main.c /usr/local/lib/libuv.a -pthread
clean:
    rm main

To improve the previous answer a bit, pkg-config can be used to avoid hardcoding the path:为了稍微改进之前的答案,可以使用pkg-config来避免对路径进行硬编码:

LDFLAGS = `pkg-config --libs libuv`

main: src/main.c
    $(CC) -o main src/main.c $(LDFLAGS)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM