libuv undefined reference to uv_loop_new

2019-02-22 10:27发布

问题:

After compiling, I am trying to run libuv sample program:

#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;
}

But, when try to run, I get the following error:

**/tmp/ccHTpspB.o: In function `main':
main.c:(.text+0x9): undefined reference to `uv_loop_new'
main.c:(.text+0x28): undefined reference to `uv_run'
collect2: error: ld returned 1 exit status**

Where did I go wrong ?

PS: It doesn't work with #include "uv.h"

回答1:

You need to link the libuv.a with your compiled code and the linker doesn't know where to find the compiled libuv.

To give you a better answer I would need to see you compile command but in the meantime I would strongly recommend this video where Ryan builds a sample libuv project. The actual code he uses is a little out of date as the API has changed but I think you will find the start where he puts a project together very enlightening.

http://vimeo.com/24713213



回答2:

In ubuntu I have used following command with success:

gcc sample.c -luv


标签: c libuv