Linker symbols not found - c

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.

Related

Jansson on Xcode

I am trying to test Jansson on Xcode 7.3.1. I installed according to the instructions, then used cMake when that didn't work, which also didn't work. I think Xcode is having trouble finding the Jansson library. I have modified the project header and library paths in many different ways, after extensive suggestions from searching out this problem. Xcode is able to find the header. Note also that I am able to install Jansson and run it in Eclipse. Any insight would be appreciated.
Here is the error log from Xcode:
Ld /Users/corrychapman/Library/Developer/Xcode/DerivedData/JanssonDev-dispcmnteuwiedaiazlbkwtztfcu/Build/Products/Debug/JanssonDev normal x86_64
cd /Users/corrychapman/Desktop/jansson-2.11/JanssonDev
export MACOSX_DEPLOYMENT_TARGET=10.11
/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/clang -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.11.sdk -L/Users/corrychapman/Library/Developer/Xcode/DerivedData/JanssonDev-dispcmnteuwiedaiazlbkwtztfcu/Build/Products/Debug -L/usr/local/lib -L/Users/corrychapman/Desktop/jansson-2.11/JanssonDev -F/Users/corrychapman/Library/Developer/Xcode/DerivedData/JanssonDev-dispcmnteuwiedaiazlbkwtztfcu/Build/Products/Debug -filelist /Users/corrychapman/Library/Developer/Xcode/DerivedData/JanssonDev-dispcmnteuwiedaiazlbkwtztfcu/Build/Intermediates/JanssonDev.build/Debug/JanssonDev.build/Objects-normal/x86_64/JanssonDev.LinkFileList -mmacosx-version-min=10.11 -Xlinker -no_deduplicate -Xlinker -dependency_info -Xlinker /Users/corrychapman/Library/Developer/Xcode/DerivedData/JanssonDev-dispcmnteuwiedaiazlbkwtztfcu/Build/Intermediates/JanssonDev.build/Debug/JanssonDev.build/Objects-normal/x86_64/JanssonDev_dependency_info.dat -o /Users/corrychapman/Library/Developer/Xcode/DerivedData/JanssonDev-dispcmnteuwiedaiazlbkwtztfcu/Build/Products/Debug/JanssonDev
Undefined symbols for architecture x86_64:
"_json_array_get", referenced from:
_main in main.o
"_json_array_size", referenced from:
_main in main.o
"_json_delete", referenced from:
_json_decref in main.o
"_json_loads", referenced from:
_main in main.o
"_json_object_get", referenced from:
_main in main.o
"_json_string_value", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Found it. Build Settings -> Linking -> Other Linker Flags: -ljansson

Mac OS X Sierra : Undefined symbols for architecture x86_64

I am trying to build a C source file based on Linphone in Mac OS X Sierra but getting the following error.
This is the link for the C source file.
http://www.linphone.org/docs/liblinphone/group__basic__call__tutorials.html
Edited:
I am trying to compile the source code with this command
clang -o tt tt.c -I/Users/softdev/Downloads/linphone-sdk-3.11.1-mac/include/
Error:
Undefined symbols for architecture x86_64
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have tried to change the target cpu but didn't work.
My system has XCode 8. Any help regarding this will be appreciated.
Edited: Complete Output
Undefined symbols for architecture x86_64:
"_linphone_call_get_state", referenced from:
_main in tt-ca2045.o
"_linphone_call_ref", referenced from:
_main in tt-ca2045.o
"_linphone_call_unref", referenced from:
_main in tt-ca2045.o
"_linphone_core_destroy", referenced from:
_main in tt-ca2045.o
"_linphone_core_invite", referenced from:
_main in tt-ca2045.o
"_linphone_core_iterate", referenced from:
_main in tt-ca2045.o
"_linphone_core_new", referenced from:
_main in tt-ca2045.o
"_linphone_core_terminate_call", referenced from:
_main in tt-ca2045.o
"_ms_usleep", referenced from:
_main in tt-ca2045.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I got the sample code to compile using this:
clang -o hello hello.c -Ilinphone-sdk-3/include -Llinphone-sdk-3/lib -llinphone -lmediastreamer_base
Clang's -I parameter points to the where the header (.h) files live
And as for my additions, -L specifies the path for clang to get to where the lib files live. In your case, it might live in -L/Users/softdev/Downloads/linphone-sdk-3.11.1-mac/lib
then -l specifies which dylibs you want to include (strip off the lib prefix and the dylib suffix).
Lastly, you need to add a missing line to the sample code you pointed to. Add:
#include <unistd.h>
after signal.h

Linking errors when compiling a shared library

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.

Nauty and Traces on Mac OS X

I am trying to use the code Nauty and Traces http://pallini.di.uniroma1.it/. But when I complied an example, say nautyex8.c (provided by the package), the following error showed up:
Undefined symbols for architecture x86_64:
"_alloc_error", referenced from:
_main in nautyex8-54d9da.o
"_densenauty", referenced from:
_main in nautyex8-54d9da.o
"_dispatch_graph", referenced from:
_main.options in nautyex8-54d9da.o
"_nauty_check", referenced from:
_main in nautyex8-54d9da.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am using Mac OS X Yosemite (10.10.5) and Xcode (version 7.1). I believe that there is no error in the codes because they are widely used so I guess the problem comes from my system settings.
Thank you so much!
I've just opened a pull request to make brew install nauty work on OS X: https://github.com/Homebrew/homebrew-core/pull/48701
Until then, here's how you would build nautyex8.c on OS X, as of Nauty/Traces 2.6r12.
Step 1 is to download the code and build the library:
wget http://pallini.di.uniroma1.it/nauty26r12.tar.gz
tar zxvf nauty26r12.tar.gz
cd nauty26r12
./configure
make
ls -l nauty.a
Step 2 is to compile nautyex8.c and link it against the library you just built:
gcc -O3 nautyex8.c nauty.a
./a.out
You probably forgot to put that "nauty.a" at the end of your compiler command line. If I do that too, I get:
gcc -O3 nautyex8.c
Undefined symbols for architecture x86_64:
"_alloc_error", referenced from:
_main in nautyex8-05f086.o
"_densenauty", referenced from:
_main in nautyex8-05f086.o
"_dispatch_graph", referenced from:
_main.options in nautyex8-05f086.o
"_nauty_check", referenced from:
_main in nautyex8-05f086.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
which exactly matches the symptom you described.

Compile simple hello world ZeroMQ C example, compile flags?

Trying to compile the example hello_world.c from the zeromq tutorial:
http://zguide.zeromq.org/page:all#Ask-and-Ye-Shall-Receive
Pretty sure I have everything installed in OSX Mountain Lion.
clang -Wall hwserver.c -o hwserver
gives me an error:
Undefined symbols for architecture x86_64:
"_zmq_bind", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_ctx_new", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_close", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_data", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_init", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_init_size", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_recv", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_msg_send", referenced from:
_main in hwserver-OgrEe6.o
"_zmq_socket", referenced from:
_main in hwserver-OgrEe6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I'm not the most experienced in C. Not sure if I sure be adding a compiler flag for the zmq dylib or headers or my $PATH being off.
in /usr/local/lib:
libzmq.3.dylib
libzmq.a
libzmq.dylib
libzmq.la
and in /usr/local/include:
zmq.h
zmq_utils.h
and echo $PATH:
/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/lib/:/usr/local/include/
You list the ZeroMQ libraries in your question, but you do not actually link with them. Change the command line to this:
clang -Wall hwserver.c -o hwserver -L/usr/local/lib -lzmq
Explanation of the extra arguments:
-L/usr/local/lib tells the linker to add a path (/usr/local/lib) to the library search path.
-lzmq tells the library to link with the zmq library.
The $PATH environment variable have nothing to do with this, it just tells the shell where to look for commands.

Resources