Trouble Linking when compiling a MEX file - c

I am trying to compile a mex file that I wrote in order to run a pre-existing C program in MATLAB. I am getting the following errors:
>> mex src/main.c -Iinclude -I/Users/my_name/mpfr-3.1.1/src/ -I/Users/my_name/gmp-5.0.5 -Lsrc0 -output cpdetect_c
Undefined symbols for architecture x86_64:
"_binomial_main", referenced from:
_cpdetect in main.o
"_gaussian1_main", referenced from:
_cpdetect in main.o
"_gaussianU_main", referenced from:
_cpdetect in main.o
"_mpfr_clears", referenced from:
_cpdetect in main.o
"_mpfr_exp10", referenced from:
_cpdetect in main.o
"_mpfr_inits2", referenced from:
_cpdetect in main.o
"_mpfr_set_d", referenced from:
_cpdetect in main.o
"_poisson_main", referenced from:
_cpdetect in main.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am confused because these functions are all part of files that I #Include in my code. How do I get the compiler to find them?
I am running MATLAB R2013a on Mac OS X 10.8 with Xcode version 4.6.3.
Thanks for any advice!
UPDATE1:
#Shai: Contents of my src0 folder:
>> ls src0
Makefile gaussian1.o test_gaussian1.c test_tools.c
Makefile.am loadtraj.c test_gaussian1_point_calc.c test_tools.o
Makefile.in loadtraj.o test_load tools.c
binomial.c main.c test_load.c tools.o
binomial.o main.o test_load.o
cpdetect poisson.c test_poisson.c
gaussian1.c poisson.o test_tools
This C program (originally meant to run on the command line) came from a friend and already had the src0 folder present. I modified the code files in the 'src' folder when writing the mex function, but I didn't touch the src0 folder.
UPDATE 2:
I realized I was pointing to the wrong location for mpfr. I am now using the following compile command:
>> mex src/main.c src/binomial.c src/gaussian1.c src/gaussianU.c src/poisson.c -Iinclude -I/usr/local/include -lmpfr -output cpdetect_c
and it is giving the following errors:
Undefined symbols for architecture x86_64:
"_mpfr_mul_d", referenced from:
_gaussian1_calc_constant_part in gaussian1.o
_gaussianU_calc_constant_part in gaussianU.o
"_mpfr_printf", referenced from:
_find_gaussian1_change_point in gaussian1.o
_gaussian1_point_calc in gaussian1.o
_gaussianU_calc_constant_part in gaussianU.o
_find_gaussianU_change_point in gaussianU.o
_find_poisson_change_point in poisson.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
mex: link of ' "cpdetect_c.mexmaci64"' failed.
This is strange. It's not like the compiler can't find any of the mpfr functions - there are lots of others that it seems to be finding without any trouble (and if I deliberately omit the -lmpfr flag, I get a much longer list of undefined symbols). I did confirm that mpfr_mul_d and mpfr_printf are part of the mpfr distribution I installed. Any ideas? Thanks.

The answer was provided by Amro - it is necessary to both point to the location of the library (using the flag -L/usr/local/lib in this case) and to tell the compiler to link to it (using the flag -lmpfr). Thanks also to Shai for help troubleshooting this.

Related

How to fix " Undefined symbols for architecture x86_64 " in C?

I have a header file included in the main but when I compile the main, I have an error saying that the linker failed.
I tried to find the object files but I cannot find them.
I think the problem may come from my machine. I am kind of a beginner so I don't know how to solve this
When I try compiling my code I get this error:
Undefined symbols for architecture x86_64:
"_intClassic", referenced from:
_main in main-53b7e4.o
"_intQuadrature", referenced from:
_main in main-53b7e4.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#zwol #JonathanLeffer I have 3 files in my project main.c, integral.h
and integral.c. integral.c contains the code of the functions
intClassic and intQuadrature that allow me to calculate different
types of integral. In integral.h I declared the functions and
structures I use. Finally in the main I included integral.h .
Also $ gcc -o output file1.o file2.o can this command help me ?
In the same directory as your files, try running the command
gcc main.c integral.c -o integral
This should take the 2 files and compile them into a program called ./integral

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

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.

Can gcc -o and -S be used together

Can I use for example
gcc -o -S output.s abs.c
to generate an assembly file with name output.s? It seems like I can't. When I try to do that, I got following error message.
Undefined symbols for architecture x86_64: "_main", referenced from:
implicit entry/start for main executable ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit
code 1 (use -v to see invocation)
I do not intend to use the linker, just try to examine the assembly code.
-o must be followed by the name of the output file. So, this would work:
gcc -S abc.c -o output.s

Getting Apple Mach-O Linker error C99. How do i fix it?

Ld /Users/ashutoshagarwal/Library/Developer/Xcode/DerivedData/c-cnyfflmvjyaashaoduqduqqsfegd/Build/Products/Debug/c normal x86_64
cd /Users/ashutoshagarwal/Desktop/c
setenv MACOSX_DEPLOYMENT_TARGET 10.9
/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.9.sdk -L/Users/ashutoshagarwal/Library/Developer/Xcode/DerivedData/c-cnyfflmvjyaashaoduqduqqsfegd/Build/Products/Debug -F/Users/ashutoshagarwal/Library/Developer/Xcode/DerivedData/c-cnyfflmvjyaashaoduqduqqsfegd/Build/Products/Debug -filelist /Users/ashutoshagarwal/Library/Developer/Xcode/DerivedData/c-cnyfflmvjyaashaoduqduqqsfegd/Build/Intermediates/c.build/Debug/c.build/Objects-normal/x86_64/c.LinkFileList -mmacosx-version-min=10.9 -framework Foundation -Xlinker -dependency_info -Xlinker /Users/ashutoshagarwal/Library/Developer/Xcode/DerivedData/c-cnyfflmvjyaashaoduqduqqsfegd/Build/Intermediates/c.build/Debug/c.build/Objects-normal/x86_64/c_dependency_info.dat -o /Users/ashutoshagarwal/Library/Developer/Xcode/DerivedData/c-cnyfflmvjyaashaoduqduqqsfegd/Build/Products/Debug/c
Undefined symbols for architecture x86_64:
"_add_history", referenced from:
_main in main.o
"_readline", referenced from:
_main in main.o
_source 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)
I am getting Apple-Mach-O Linker (Id) Error, have sent hours trying to fix this one, Dunno what to do.
It looks like you are using the GNU readline and GNU history libraries, which are available, by default, on the mac in libedit.dylib:
$ nm /usr/lib/libedit.dylib | fgrep readline
0000000000009899 T _readline
000000000001f444 D _readline_echoing_p
000000000001f400 D _rl_readline_name
000000000001f3f8 D _rl_readline_version
$ nm /usr/lib/libedit.dylib | fgrep add_history
000000000000acbc T _add_history
(the T indicates that the symbol is in the library's text section).
Therefore you need to add -ledit to your linker command line. If you are using Xcode then you can add libedit.dylib to the list of libraries to link against or if you are using make then you probably have to edit the LIBS variable in your Makefile.
The error message:
Undefined symbols for architecture x86_64: "_add_history", referenced from: _main in main.o
"_readline", referenced from: _main in main.o _source 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)'
Tells you that the linker cannot find the symbols readline and add_history. Without seeing your source code, and the precise linker and compiler command-lines, it's hard to speculate the exact cause. A likely cause is that you failed to link with external libraries, or other modules of your program.

Resources