Info

This question is closed. Reopen it to edit or answer.

trying to test a c shared library made in MATLAB

1 view (last 30 days)
Matt Scholten
Matt Scholten on 13 Jul 2011
Closed: MATLAB Answer Bot on 20 Aug 2021
Hey everyone, I am trying to test a shared library that I made in MATLAB. PLEASE HELP!
I'm not an expert, so if you need other info, or if i'm just not making sense I can try to clarify, or provide further info.
First off:
I'm using MATLAB 2011a, compiling c with the MATLAB compatible gcc-4.3, and all of this is running on 64 bit opensuse 11.3.
1) I've made a c shared library, header, and wrapper. 2) I've set the environment variable. 3) I'm having trouble creating an executable to test my .so with
Here is a more detailed description of what I've tried: *******
I'm taking a program we made in MATLAB, and making a c shared library out of it to send to our client for testing. Before I can send the library, I need to be sure it works. So I need to make an executable out of it.
I'm not getting any build errors when MATLAB makes the library, and I've done this once on windows successfully. So I don't know what's wrong.
I'm using the deploytool in MATLAB to make the library.
It makes these files:
libatr2.c (wrapper to run libatr2.so) libatr2.h libatr2.so libatr2.exports readme.txt
The readme says to set the environment variable using the following csh command. It does this because MATLAB requires using the MATLAB Compiler Runtime (MCR) to operate the libatr2 files. so my executable has to link to MCR. For now, I've set it to link to the MATLAB libraries instead. MCR is for deployment on systems that are not running MATLAB 2011a.
According to mathworks info, this .so is making wrappers for matlab libraries, and running them in a c shell.
I set the environment variable by editing the .cshrc file that runs when you switch to the csh shell.
This is what I added to .cshrc:
#! /bin/csh
setenv LD_LIBRARY_PATH /usr/local/MATLAB/R2011a/runtime/glnxa64/usr/local/MATLAB/R2011a/sys/os/glnxa64:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/native_threads:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64/server:/usr/local/MATLAB/R2011a/sys/java/jre/glnxa64/jre/lib/amd64:
setenv XAPPLRESDIR /usr/local/MATLAB/R2011a/X11/app-defaults:
Once I switch to the csh shell, I'm seeing that the LD_LIBRARY_PATH is set to the above when I run env. or echo $LD_LIBRARY_PATH... etm. So this step seems correct.
*********
There are a couple ways that I can create an executable binary that will test my .so. Both are using gcc-4.3 which is the c compiler that is compatible with MATLAB 2011a.
One way, and what is simplest, is to run mbuild -v -g ./libatr2.c in MATLAB. this uses gcc-4.3 in the following way:
>> mbuild -v -g ./libatr2.c ---------------------------------------------------------------- -> mbuildopts.sh sourced from directory (DIR = $HOME/.matlab/$REL_VERSION) FILE = /home/suse/.matlab/R2011a/mbuildopts.sh ---------------------------------------------------------------- -> TMW_ROOT = /usr/local/MATLAB/R2011a -> CC = gcc-4.3 -> CC flags: CFLAGS = -ansi -D_GNU_SOURCE -I/usr/local/MATLAB/R2011a/extern/include -DUNIX -DX11 -pthread CDEBUGFLAGS = -g COPTIMFLAGS = -O -DNDEBUG CLIBS = -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/runtime/glnxa64 -lmwmclmcrrt -lm arguments = -> LD = gcc-4.3 -> Link flags: LDFLAGS = -pthread LDDEBUGFLAGS = -g LDOPTIMFLAGS = -O arguments = ----------------------------------------------------------------
-> gcc-4.3 -c -ansi -D_GNU_SOURCE -I/usr/local/MATLAB/R2011a/extern/include -DUNIX -DX11 -pthread -g "./libatr2.c"
-> gcc-4.3 -g -pthread -o libatr2 libatr2.o -Wl,-rpath-link,/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/runtime/glnxa64 -lmwmclmcrrt -lm
*************************
This is the error when I run this mbuild command.
/usr/lib64/gcc/x86_64-suse-linux/4.3/../../../../lib64/crt1.o: In function `_start': /usr/src/packages/BUILD/glibc-2.11.2/csu/../sysdeps/x86_64/elf/start.S:109: undefined reference to `main' collect2: ld returned 1 exit status
I get the same error if I try to make the executable manually using the gcc-4.3 command in the terminal. I have no idea what it means.
I'm trying to make the executable manually like this:
gcc-4.3 -I/usr/local/MATLAB/R2011a/extern/include -L/usr/local/MATLAB/R2011a/bin/glnxa64 -L/usr/local/MATLAB/R2011a/runtime/glnxa64 -lmwmclmcrrt ./libatr2.c -o libatr2
********
There is a settings script that mbuild uses, that I might be edditng wrong I guess. It's about 200 lines long, I'll post it if needed.
I've also contacted MATLAB tech support They said the wrapper (libatr2.c) is intended to be a template, and may not actually run my library unless eddited. Perhaps I should edit it to reference something called main? I'm not sure how to do that if that's the case.
This is really proving to be difficult for me. I appreciate any input you all can give me. -Thanks

Answers (1)

Titus Edelhofer
Titus Edelhofer on 14 Jul 2011
Hi,
I would suggest to try the example in web([docroot '/toolbox/compiler/f2-972343.html#f2-1000836']) first: it has detailed explanations of how to build and run some shared library generated by compiler.
What is esp. interesting for you is to take a look at the example of how to call the library. You can't just take the library itself, Tech Support was right in telling you that your calling application needs to look different ...
Titus

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!