Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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.

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.

I've downloaded the latest libuv from gtihub as a zip and have unzipped in into a working folder. I compiled and installed it following 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

#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

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.

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.

/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.

I'm using Ubuntu 13.04

like image 201
thomas-peter Avatar asked Jan 13 '23 03:01

thomas-peter


2 Answers

The most simple Makefile I could make work is this, but obviously it is specific to the location of libuv.a on my system. 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
like image 135
thomas-peter Avatar answered Jan 19 '23 15:01

thomas-peter


To improve the previous answer a bit, pkg-config can be used to avoid hardcoding the path:

LDFLAGS = `pkg-config --libs libuv`

main: src/main.c
    $(CC) -o main src/main.c $(LDFLAGS)
like image 35
Steve Chavez Avatar answered Jan 19 '23 14:01

Steve Chavez