Libraries to compile libuv on os x?

2019-05-10 16:35发布

问题:

I'm trying to learn some libuv and it seems there's a great book that goes through it. However, the book doesn't explain how to actually compile it. I ran make on the code that I pulled from github, and compiled with GYP as described on the github (https://github.com/joyent/libuv). However I'm not sure what kind of libraries I need to include to get the code to compile. I tried to compile this code:

/* first.c */
#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 compiled it with the following command from the libuv folder:

gcc -o first first.c build/Release/libuv.a

and I got the following missing symbols:

Undefined symbols for architecture x86_64:
  "_CFArrayCreate", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_CFRunLoopAddSource", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopGetCurrent", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopRemoveSource", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopRun", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
  "_CFRunLoopSourceCreate", referenced from:
      _uv__platform_loop_init in libuv.a(darwin.o)
  "_CFRunLoopSourceSignal", referenced from:
      _uv__cf_loop_signal in libuv.a(darwin.o)
  "_CFRunLoopStop", referenced from:
      _uv__platform_loop_delete in libuv.a(darwin.o)
  "_CFRunLoopWakeUp", referenced from:
      _uv__cf_loop_signal in libuv.a(darwin.o)
  "_CFStringCreateWithCString", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_CFStringGetSystemEncoding", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_FSEventStreamCreate", referenced from:
      _uv__fsevents_init in libuv.a(fsevents.o)
  "_FSEventStreamInvalidate", referenced from:
      _uv__fsevents_close in libuv.a(fsevents.o)
  "_FSEventStreamRelease", referenced from:
      _uv__fsevents_close in libuv.a(fsevents.o)
  "_FSEventStreamScheduleWithRunLoop", referenced from:
      _uv__fsevents_schedule in libuv.a(fsevents.o)
  "_FSEventStreamStart", referenced from:
      _uv__fsevents_schedule in libuv.a(fsevents.o)
  "_FSEventStreamStop", referenced from:
      _uv__fsevents_close in libuv.a(fsevents.o)
  "_kCFRunLoopDefaultMode", referenced from:
      _uv__cf_loop_runner in libuv.a(darwin.o)
      _uv__fsevents_schedule in libuv.a(fsevents.o)
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status

Can someone give me a quick tutorial for how to build libuv, or if there's anything else I need?

回答1:

With libuv installed through homebrew do:

$ gcc -luv main.c


回答2:

OK, figured it out. I have to use the OSX "CoreFoundation" and "CoreServices" frameworks. The following command compiles successfully:

gcc -o first first.c build/Release/libuv.a -framework CoreFoundation -framework CoreServices


回答3:

Thanks for the solution – I was struggling with the same problem.

I developed your answer so that I could compile and link from any folder by using the following options:

gcc -o first -L/my/folders/libuv/ -I/my/folders/libuv/include/ first.c -luv -framework CoreFoundation -framework CoreServices

Also, I added the library into Eclipse using the following steps:

To add the path to the header file uv.h:

Right click on project and select Properties->C/C++ General->Paths and Symbols->Includes. Click on Add.. and in the text box enter:

/my/folders/libuv/include/

Click Apply->Okay.

To add the library:

While in same screen, as above, click Libraries. Click on Add.. and in the text box enter:

uv

To add the path to the library:

Still on the same screen click on Library Paths. Click Add.. and enter in the text box:

/my/folders/libuv/

To add the frameworks:

Right click on project Properties->C/C++Build->Setting->Tool Settings->Miscellaneous->Mac OS X C++ Linker. Then in the text box with the title Linker Flags add:

-framework CoreFoundation –framework CoreServices

Click on Apply then build.



回答4:

You can use GYP to generate an xcodeproj for libuv (as explained in libuv's README) and add this xcodeproj to your main Xcode project.

It can be automated (for easy updating) with a simple shell script (assumes you put the libuv submodule in Externals/libuv, but can be changed):

git submodule update --init
git clone https://chromium.googlesource.com/external/gyp.git Externals/libuv/build/gyp
Externals/libuv/gyp_uv.py -f xcode

Then you'll be able to add libuv as a dependency and to the libraries to link your target to:

The last thing to do is to tell Xcode where are libuv's headers:

See this post