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?
With libuv installed through homebrew do:
$ gcc -luv main.c
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
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.
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
Related
I am attempting to compile third party code to a shared library so I can call it from Python using ctypes. This is my first time knowingly working with shared libraries.
I build the object code using:
gcc -c -fPIC -o elisa3-lib.o ../elisa3-lib.c
gcc -c -fPIC -o usb-comm.o ../usb-comm.c
Then I attempt to build the shared library:
gcc -shared -fPIC -Wl,-install_name,libelisa3.so -o libelisa3.so.1.0 elisa3-lib.o usb-comm.o -lc
And I get linker errors because usbcomm.c references libusb.h:
#ifdef __APPLE__
#include </opt/local/include/libusb-1.0/libusb.h>
#endif
With errors:
Undefined symbols for architecture x86_64:
"_libusb_bulk_transfer", referenced from:
_usb_send in usb-comm.o
_usb_receive in usb-comm.o
"_libusb_claim_interface", referenced from:
_openCommunication in usb-comm.o
"_libusb_close", referenced from:
_closeCommunication in usb-comm.o
"_libusb_exit", referenced from:
_closeCommunication in usb-comm.o
"_libusb_init", referenced from:
_openCommunication in usb-comm.o
"_libusb_open_device_with_vid_pid", referenced from:
_find_nrf_device in usb-comm.o
"_libusb_release_interface", referenced from:
_closeCommunication in usb-comm.o
ld: symbol(s) not found for architecture x86_64
How do I link correctly? Do I need to compile libusb to a shared library as well and link with -l?
"Undefined symbols" are functions/variables that the linker can't find. In short, yes you need to include libusb as either one of the libraries or objects you're linking in. Do whichever is easier or more appropriate.
I'm trying to compile something I've fetched off of github with gcc:
users-MBP:Chip-8-Emulator user$ gcc -o chip8 chip8.c
Undefined symbols for architecture x86_64:
"_SDL_Delay", referenced from:
_chip8_draw in chip8-bc89fc.o
"_SDL_Flip", referenced from:
_chip8_draw in chip8-bc89fc.o
"_SDL_GetKeyState", referenced from:
_chip8_execute in chip8-bc89fc.o
_chip8_prec in chip8-bc89fc.o
"_SDL_GetVideoSurface", referenced from:
_chip8_draw in chip8-bc89fc.o
"_SDL_Init", referenced from:
_chip8_prepare in chip8-bc89fc.o
"_SDL_LockSurface", referenced from:
_chip8_draw in chip8-bc89fc.o
"_SDL_PollEvent", referenced from:
_chip8_prepare in chip8-bc89fc.o
_chip8_prec in chip8-bc89fc.o
"_SDL_SetVideoMode", referenced from:
_chip8_prepare in chip8-bc89fc.o
"_SDL_UnlockSurface", referenced from:
_chip8_draw in chip8-bc89fc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The error seems to be an issue with the SDL library in this case, but I've had the exact same issue with several other little projects I treid to compile. Before compiling, I installed the SDL libs and checked they are there:
users-MBP:Chip-8-Emulator user$ ls /usr/local/lib/
...
libSDL-1.2.0.dylib
libSDL.a
libSDL.dylib
libSDLmain.a
...
After lots of Googling it seems I'm not the only one with this problem and I couldn't find solution that worked. It seems that osx doesn't include /use/local/lib in the default linker paths.
How can I fix this (a permanent solution would be welcome)?
EDIT
Following a suggestion from user Alex I tried with $ gcc -o chip8 -L/usr/local/lib chip8.c. The error and output is identical to the above.
You'll want to add -L/usr/local/lib -lSDL to your GCC invocation.
As for a more permanent solution, this is a convention of OS X, and strictly speaking /usr/local/lib is a non-standard installation location.
The upcoming release of Go 1.5 comes with new buildmodes which allow for exporting Go symbols to be linked and called from C code. I've been playing around with it and got basic "Hello world" examples working, but now I'm trying to link a Go library which starts a net/http.Server and it's failing. The code looks like this (it's also available here):
gohttplib.go:
package main
import "C"
import "net/http"
//export ListenAndServe
func ListenAndServe(caddr *C.char) {
addr := C.GoString(caddr)
http.ListenAndServe(addr, nil)
}
func main() {}
examples/c/main.c:
#include <stdio.h>
#include "../../gohttplib.h"
int main()
{
ListenAndServe(":8000");
return 0;
}
Producing the statically-linked object and headers works fine:
$ go build -buildmode=c-archive
But compiling against it is failing:
$ gcc -o gohttp-c examples/c/main.c gohttplib.a -lpthread
Undefined symbols for architecture x86_64:
"_CFArrayGetCount", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_CFArrayGetValueAtIndex", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_CFDataAppendBytes", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_CFDataCreateMutable", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_CFDataGetBytePtr", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
__cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr in gohttplib.a(000003.o)
(maybe you meant: __cgo_6dbb806e9976_Cfunc_CFDataGetBytePtr)
"_CFDataGetLength", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
__cgo_6dbb806e9976_Cfunc_CFDataGetLength in gohttplib.a(000003.o)
(maybe you meant: __cgo_6dbb806e9976_Cfunc_CFDataGetLength)
"_CFRelease", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
__cgo_6dbb806e9976_Cfunc_CFRelease in gohttplib.a(000003.o)
(maybe you meant: __cgo_6dbb806e9976_Cfunc_CFRelease)
"_SecKeychainItemExport", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_SecTrustCopyAnchorCertificates", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
"_kCFAllocatorDefault", referenced from:
_FetchPEMRoots in gohttplib.a(000003.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [example-c] Error 1
This is using a recent version from the Go github repository (38e3427) on OS X 10.9.5. I understand that Go 1.5 is not released yet and that there are no guarantees about it working, but I'm doing this for educational purposes and I suspect I'm missing something.
Related versions:
$ ld -v
#(#)PROGRAM:ld PROJECT:ld64-241.9
configured to support archs: armv6 armv7 armv7s arm64 i386 x86_64 x86_64h armv6m armv7m armv7em
LTO support using: LLVM version 3.5svn
$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 6.0 (clang-600.0.57) (based on LLVM 3.5svn)
Target: x86_64-apple-darwin13.4.0
Thread model: posix
Turns out this problem exists on OSX/darwin. To work around it, we need to add -framework CoreFoundation -framework Security options to the gcc linking command. The final command looks like this:
$ gcc -o gohttp-c examples/c/main.c gohttplib.a \
-framework CoreFoundation -framework Security -lpthread
This requirement might be removed in a future version of Go. More discussion on this issue here: https://github.com/golang/go/issues/11258
Sundown a (formerly) popular Markdown editor includes an examples directory, and I want to play around with it on the command line, but I'm having difficulties.
I navigate to examples, I run gcc sundown.c -o sundown and it complains that I don't have markdown.h. So I dump the contents of the src directory and the html directory into the examples category, and run it.
I then get:
Undefined symbols for architecture x86_64:
"_bufgrow", referenced from:
_main in sundown-3bac08.o
"_bufnew", referenced from:
_main in sundown-3bac08.o
"_bufrelease", referenced from:
_main in sundown-3bac08.o
"_sd_markdown_free", referenced from:
_main in sundown-3bac08.o
"_sd_markdown_new", referenced from:
_main in sundown-3bac08.o
"_sd_markdown_render", referenced from:
_main in sundown-3bac08.o
"_sdhtml_renderer", referenced from:
_main in sundown-3bac08.o
What exactly am I doing wrong here? I feel like this should be relatively simple, I'm just at wits end trying to figure out what I'm doing wrong.
you tell gcc where to obtain #include files by adding the parameter:
'-Ipathtoincludes'
you tell gcc where to obtain the needed libraries by adding the parameters:
'-Lpathtoolibrary' and '-lshortlibraryname
note: the -l is lower case 'L'
The library path and library name parameters must be last in the gcc command line, as the linker looks at the linker parameters in the order listed on the command line
I am trying to learn how to program in C, and I want to be able to import data into my C program from a .hdf file.
I am using a mid 2009 MacBook Pro with Mac OS X lion.
I think I am having problems, because I didn't know where on my system to put all the header files associated with hdd (remember, I'm a bit of a noob). So I just downloaded the hdf5-1.8.9-freebsd-shared folder from the hdd website, and manually dragged all the .h files from the /include subdirectory into the /usr/include directory onto my computer.
When I try to compile my .c program using the normal gcc terminal command, I get an error which is along the lines of (sorry about the formatting but I think the exact error doesn't matter too much anyway):
Undefined symbols for architecture x86_64:
"_H5check_version", referenced from:
_main in cc9FVO6S.o
"_H5Fcreate", referenced from:
_main in cc9FVO6S.o
"_H5Screate_simple", referenced from:
_main in cc9FVO6S.o
"_H5open", referenced from:
_main in cc9FVO6S.o
"_H5T_STD_I32LE_g", referenced from:
_main in cc9FVO6S.o
"_H5Dcreate2", referenced from:
_main in cc9FVO6S.o
"_H5T_NATIVE_INT_g", referenced from:
_main in cc9FVO6S.o
"_H5Dwrite", referenced from:
_main in cc9FVO6S.o
"_H5Dclose", referenced from:
_main in cc9FVO6S.o
"_H5Sclose", referenced from:
_main in cc9FVO6S.o
"_H5Fclose", referenced from:
_main in cc9FVO6S.o
"_H5Fopen", referenced from:
_main in cc9FVO6S.o
"_H5Dopen2", referenced from:
_main in cc9FVO6S.o
"_H5Dread", referenced from:
_main in cc9FVO6S.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Though the exact error varies depending on exactly what code I am trying to compile. This was generated when I tried to compile a sample program.
Does anybody have any experience with getting hdf5 to work in C in Mac OS X? I have found it all very confusing.
By the way, I normally use python, and frequently use h5py without trouble.
You have to tell your linker when you are using a library using -l<library-name> and -L<library-dir> for non-standard locations.
If you installed hdf5 in /usr/local/hdf5, you need something like:
gcc -o h5ex_d_rdwr h5ex_d_rdwr.c -I/usr/local/hdf5/include -L/usr/local/hdf5/lib -lhdf5
If you are using the High-Level API, also add -lhdf5_hl.
If you have pkg-config installed and it is aware of your hdf5 installation, you can let it do it for you:
gcc -o h5ex_d_rdwr h5ex_d_rdwr.c `pkg-config hdf5 --cflags --libs`