How to load shared library created in Matlab - matlab

I tried to create an OpenModelica model with liquid flow and Media.
And I have a function in Matlab that calculates the PDE (partial differential equation) in pdetool.
I would like to create a shared dynamic library (.so file) in Matlab by MCR and load it in the model.
My platform: OpenModelica used on Linux or Mac OS. MCR on Linux and Mac OS installed.
In Matlab I can generate only DLL (may be it's possible to generate libmyfunc.so?).
When I try to compile the model with extern C function I got an error:
#omc +s test_matlab_so.mo func_mathlab.mo
#make -f test_matlab_so.makefile
/usr/bin/clang -Wimplicit-function-declaration -O0 -falign-functions -march=native -I"/opt/openmodelica/include/omc/c" -I. -DOPENMODELICA_XML_FROM_FILE_AT_RUNTIME -c -o test_matlab_so_functions.o test_matlab_so_functions.c
clang: warning: optimization flag '-falign-functions' is not supported
clang: warning: argument unused during compilation: '-falign-functions'
In file included from test_matlab_so_functions.c:7:
In file included from ./test_matlab_so_includes.h:4:
./shared_train/src/lib_summ.c:90:8: warning: implicit declaration of function 'GetModuleFileName' is invalid in C99 [-Wimplicit-function-declaration]
if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH))
^
./shared_train/src/lib_summ.c:90:26: warning: implicit declaration of function 'GetModuleHandle' is invalid in C99 [-Wimplicit-function-declaration]
if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH))
^
./shared_train/src/lib_summ.c:90:55: error: use of undeclared identifier 'path_to_dll'
if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH))
^
./shared_train/src/lib_summ.c:90:68: error: use of undeclared identifier '_MAX_PATH'
if (!GetModuleFileName(GetModuleHandle("lib_summ"), path_to_dll, _MAX_PATH))
^
./shared_train/src/lib_summ.c:94:37: error: use of undeclared identifier 'path_to_dll'
mclGetEmbeddedCtfStream(path_to_dll);
^
test_matlab_so_functions.c:19:16: warning: implicit declaration of function '_mlfSumm' is invalid in C99 [-Wimplicit-function-declaration]
_v_out_ext = _mlfSumm(_v_a_ext, _v_b_ext);
^
3 warnings and 3 errors generated.
make: *** [test_matlab_so_functions.o] Error 1
Can someone help me with integrating OpenModelica and Matlab?

I don't have much experience with Linux but I did the same thing in Windows using MCR. Check this out:
http://de.mathworks.com/matlabcentral/answers/94471-how-do-i-create-a-c-c-shared-library-with-matlab-compiler-that-can-be-used-in-a-microsoft-visual-c
When you do this, you will get a DLL as well as a static library. You need to copy both of them into the folder that your modelica code is located at and then set the library in the "Library" attibute of your modelica function which calls the external function.
Keep in mind that if you are using a 64 or 32 bit dymola, you are supposed to create the DLL using the same version of matlab correspondingly.

Related

install opencv for perl (centos)

I'm trying to do face detection on perl, and I found out about OpenCV and the perl module Image::ObjectDetect on CPAN here and here. I've done a clean install of CentOS 6.5 (minimal) on a virtual machine to test this, and then:
yum -y update
yum -y install perl cpan opencv
Ok, and considering I never user cpan before, I tried the obvious (after some short google searches)
perl -MCPAN -e shell
[accept default]
install Image::ObjectDetect
that gave me some errors:
Package opencv was not found in the pkg-config search path.
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
No package 'opencv' found
After googling about that error, I did:
export PKG_CONFIG_PATH=/usr/local/lib/pkgconfig
mkdir /usr/local/lib/pkgconfig
vi /usr/local/lib/pkgconfig/opencv.pc
the content of the file:
prefix=/usr
exec_prefix=${prefix}
includedir=${prefix}/include
libdir=${exec_prefix}/lib
Name: opencv
Description: The opencv library
Version: 2.x.x
Cflags: -I${includedir}/opencv -I${includedir}/opencv2
Libs: -L${libdir} -lopencv_calib3d -lopencv_imgproc -lopencv_contrib -lopencv_legacy -l
After I ran the install again, I noticed I was missing gcc:
/bin/sh: gcc: command not found
yum -y install gcc
Running the install again now gives me:
...
Note (probably harmless): No library found for -lopencv_calib3d
Note (probably harmless): No library found for -lopencv_imgproc
Note (probably harmless): No library found for -lopencv_contrib
Note (probably harmless): No library found for -lopencv_legacy
Note (probably harmless): No library found for -l
Writing Makefile for Image::ObjectDetect
Could not read '/root/.cpan/build/Image-ObjectDetect-0.12-BsNNjF/META.yml'. Falling back to other methods to determine prerequisites
cp lib/Image/ObjectDetect.pm blib/lib/Image/ObjectDetect.pm
cp lib/Image/ObjectDetect.xs blib/lib/Image/ObjectDetect.xs
/usr/bin/perl /usr/share/perl5/ExtUtils/xsubpp -typemap /usr/share/perl5/ExtUtils/typemap ObjectDetect.xs > ObjectDetect.xsc && mv ObjectDetect.xsc ObjectDetect.c
gcc -c -I/usr/include/opencv -I/usr/include/opencv2 -Isrc -I/usr/include/opencv -I/usr/include/opencv2 -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m32 -march=i686 -mtune=atom -fasynchronous-unwind-tables -DVERSION=\"0.12\" -DXS_VERSION=\"0.12\" -fPIC "-I/usr/lib/perl5/CORE" ObjectDetect.c
In file included from ObjectDetect.xs:7:
/usr/lib/perl5/CORE/cv.h:13: error: redefinition of ‘struct xpvcv’
/usr/lib/perl5/CORE/cv.h:94: error: conflicting types for ‘xpvcv_allocated’
/usr/lib/perl5/CORE/cv.h:94: note: previous declaration of ‘xpvcv_allocated’ was here
ObjectDetect.xs:8:21: error: highgui.h: No such file or directory
ObjectDetect.xs: In function ‘XS_Image__ObjectDetect_new’:
ObjectDetect.xs:19: error: ‘CvHaarClassifierCascade’ undeclared (first use in this function)
ObjectDetect.xs:19: error: (Each undeclared identifier is reported only once
ObjectDetect.xs:19: error: for each function it appears in.)
ObjectDetect.xs:19: error: ‘cascade’ undeclared (first use in this function)
ObjectDetect.xs:22: warning: implicit declaration of function ‘cvLoad’
ObjectDetect.xs: In function ‘XS_Image__ObjectDetect_xs_detect’:
ObjectDetect.xs:37: error: ‘IplImage’ undeclared (first use in this function)
ObjectDetect.xs:37: error: ‘img’ undeclared (first use in this function)
ObjectDetect.xs:37: error: ‘gray’ undeclared (first use in this function)
ObjectDetect.xs:37: warning: left-hand operand of comma expression has no effect
ObjectDetect.xs:39: error: ‘CvMemStorage’ undeclared (first use in this function)
ObjectDetect.xs:39: error: ‘storage’ undeclared (first use in this function)
ObjectDetect.xs:40: error: ‘CvHaarClassifierCascade’ undeclared (first use in this function)
ObjectDetect.xs:40: error: ‘cascade’ undeclared (first use in this function)
ObjectDetect.xs:41: error: ‘CvSeq’ undeclared (first use in this function)
ObjectDetect.xs:41: error: ‘objects’ undeclared (first use in this function)
ObjectDetect.xs:42: error: ‘CvRect’ undeclared (first use in this function)
ObjectDetect.xs:42: error: ‘rect’ undeclared (first use in this function)
ObjectDetect.xs:46: warning: implicit declaration of function ‘cvLoadImage’
ObjectDetect.xs:50: warning: implicit declaration of function ‘cvCreateImage’
ObjectDetect.xs:50: warning: implicit declaration of function ‘cvSize’
ObjectDetect.xs:51: warning: implicit declaration of function ‘cvCvtColor’
ObjectDetect.xs:51: error: ‘CV_BGR2GRAY’ undeclared (first use in this function)
ObjectDetect.xs:52: warning: implicit declaration of function ‘cvEqualizeHist’
ObjectDetect.xs:54: warning: implicit declaration of function ‘cvCreateMemStorage’
ObjectDetect.xs:55: error: expected expression before ‘)’ token
ObjectDetect.xs:56: warning: implicit declaration of function ‘cvHaarDetectObjects’
ObjectDetect.xs:58: error: ‘CV_HAAR_DO_CANNY_PRUNING’ undeclared (first use in this function)
ObjectDetect.xs:65: error: expected expression before ‘)’ token
ObjectDetect.xs:74: warning: implicit declaration of function ‘cvReleaseMemStorage’
ObjectDetect.xs:75: warning: implicit declaration of function ‘cvReleaseImage’
ObjectDetect.xs: In function ‘XS_Image__ObjectDetect_DESTROY’:
ObjectDetect.xs:86: error: ‘CvHaarClassifierCascade’ undeclared (first use in this function)
ObjectDetect.xs:86: error: ‘cascade’ undeclared (first use in this function)
ObjectDetect.xs:88: error: expected expression before ‘)’ token
ObjectDetect.xs:89: warning: implicit declaration of function ‘cvReleaseHaarClassifierCascade’
make: *** [ObjectDetect.o] Error 1
JIRO/Image-ObjectDetect-0.12.tar.gz
/usr/bin/make -- NOT OK
Warning (usually harmless): 'YAML' not installed, will not store persistent state
Running make test
Can't test without successful make
Running make install
Make had returned bad status, install seems impossible
Failed during this command:
JIRO/Image-ObjectDetect-0.12.tar.gz : make NO
Any help? Thanks.
Glad to see someone using OpenCV with Perl! I hope you can get it working and show us some nice asps and scripts :-)
You seem to be building from source and installing packages (with yum) - this sometimes complicates matters when it comes to dependencies. For example, you may have a version of OpenCV that is too new for the CPAN module. Image::ObjectDetect` is fairly old
(the last release was several years ago http://metacpan.org/pod/Image::ObjectDetect) whereas OpenCV is being actively developed.
Note this line:
ObjectDetect.xs:8:21: error: highgui.h: No such file or directory
I came across a similar error compiling applications that depend on OpenCV after a recent update. Either the header files were missing; the applications Makefiles did not know how to locate them; or other development libraries and headers in the chain of dependencies needed updating as well.
You may get better luck solving this by posting to the OpenCV development list where the error message may look more familiar.
See: http://groups.yahoo.com/neo/groups/OpenCV/info
You could try installing some other modules to see if it is perl, XS, gcc or your version of OpenCV that is at fault. Here's another module that uses OpenCV:
https://github.com/shisful/Image-OpenCV

fortran: using pgplot with eclipse photran

So I am really not a Fortran programmer at all, but I have some code I am working with that uses the pgplot graphics libraries in fortran. I am able to get the code running in fortran from the console using the following commands.
gfortran -o simple discrete.f -lpgplot -IX11
discrete.f is the name of the file. So this works when I run it from the console. However, I want to run the code in the eclipse photran plugin so that I can use the debugger so that I can step through the code and observe some of the variables. I tried to put the -lpgplot and -IX11 flags in Eclipse, but I am getting errors about pgbbuf and pgnumb_ functions as undefined reference (which means that the -lpgplot function is not working when called from eclipse.
Can anyone please tell me how to set the flags correctly to run this code in Photran so that I can compile it and see the pgplot figure come up?
I am adding the console output:
02:57:05 **** Clean-only build of configuration Debug for project burglar_code_martin ****
make clean
rm -rf ./burglary_model.o burglar_code_martin
02:57:05 Build Finished (took 61ms)
02:57:05 **** Build of configuration Debug for project burglar_code_martin ****
make all
Building file: ../burglary_model.f
Invoking: GNU Fortran Compiler
gfortran -funderscoring -I/usr/lib -O0 -g -Wall -c -fmessage-length=0 -lpgplot -IX11 -o "burglary_model.o" "../burglary_model.f"
Warning: Nonconforming tab character in column 1 of line 225
Warning: Nonconforming tab character in column 1 of line 228
../burglary_model.f:155.27:
call probcheck(frac,1,outc)
1
Warning: Rank mismatch in argument 'problist' at (1) (rank-1 and scalar)
../burglary_model.f:54.33:
call probcheck(robprob,1,outcome)
1
Warning: Rank mismatch in argument 'problist' at (1) (rank-1 and scalar)
../burglary_model.f:95.30:
call probcheck(placeprob,1,outcome)
1
Warning: Rank mismatch in argument 'problist' at (1) (rank-1 and scalar)
../burglary_model.f:206.36:
subroutine output(A,burgsatloc,t,l,pic,screen,A0,Bbar)
1
Warning: Unused dummy argument 'burgsatloc' at (1)
../burglary_model.f:206.44:
subroutine output(A,burgsatloc,t,l,pic,screen,A0,Bbar)
1
Warning: Unused dummy argument 'pic' at (1)
../burglary_model.f:206.51:
subroutine output(A,burgsatloc,t,l,pic,screen,A0,Bbar)
1
Warning: Unused dummy argument 'screen' at (1)
../burglary_model.f:145.32:
integer l,pic,screen,i,j,k,PGOPEN,burgsatloc(512,*),nbar,outc
1
Warning: Unused variable 'k' declared at (1)
../burglary_model.f:146.15:
real rand,red,green,blue
1
Warning: Unused variable 'rand' declared at (1)
../burglary_model.f:143.53:
subroutine initialize(burgsatloc,B,l,pic,screen,rbar,Bbar)
1
Warning: Unused dummy argument 'screen' at (1)
../burglary_model.f:4.58:
integer i,j,n,l,pic,screen,guy,burgsatloc(512,512),k,
1
Warning: Unused variable 'k' declared at (1)
Finished building: ../burglary_model.f
Building target: burglar_code_martin
Invoking: GNU Fortran Linker
gfortran -lpgplot -IX11 -o "burglar_code_martin" ./burglary_model.o
./burglary_model.o: In function `output_':
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:241: undefined reference to `pgbbuf_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:242: undefined reference to `pgnumb_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:244: undefined reference to `pgenv_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:246: undefined reference to `pglab_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:247: undefined reference to `pgimag_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:253: undefined reference to `pgebuf_'
./burglary_model.o: In function `initialize_':
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:165: undefined reference to `pgopen_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:168: undefined reference to `pgpap_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:169: undefined reference to `pgask_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:170: undefined reference to `pgscir_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:175: undefined reference to `pgscr_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:181: undefined reference to `pgscr_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:187: undefined reference to `pgscr_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:193: undefined reference to `pgscr_'
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:199: undefined reference to `pgscr_'
./burglary_model.o: In function `implicit':
/media/jaimini/workspace/eclipsejuno/burglar_code_martin/Debug/../burglary_model.f:109: undefined reference to `pgclos_'
collect2: ld returned 1 exit status
make: *** [burglar_code_martin] Error 1
02:57:05 Build Finished (took 209ms)

VHDL/ModelSim - Could Not Find Entity

I am trying to simulate my VHDL file, but am running into the following error:
# ** Error: (vcom-11) Could not find work.lab1.
#
# ** Error: (vcom-1195) Cannot find expanded name "work.lab1".
#
# ** Error: Unknown expanded name.
# ** Error: VHDL Compiler exiting
# ** Error: c:/altera/12.1/modelsim_ase/win32aloem/vcom failed.
# Error in macro ./DE2_TOP_run_msim_rtl_vhdl.do line 8
# c:/altera/12.1/modelsim_ase/win32aloem/vcom failed.
# while executing
# "vcom -93 -work work"
I compiled the code successfully through both Quartus II and the ModelSim compiler before attempting to simulate. I do have a lab1 entity and architecture in my code (I can even see it in the Design Units tab of the Quartus Project Navigator), so I don't really understand this error. Anyone know what's causing this?
When the simulator is compiling the toplevel (DE2_TOP) it want to know how the used components are like. So, you should have compiled the lowerlevel components before compiling the upperlevel components.
What I do most of the times to fix this is compiling all components in correct order and then use the 'vmake' ('vmake -work work > work.vmake') command of Modelsim to generate a makefile out of the library (work). Once you have the makefile you can execute it with (make -f work.vmake). And all files will be compiled in order.
Note: Verilog is much more relaxed in those things...

Perl Cross Compilation: MAKE

I've been trying to Cross Compile Perl-5.14.2 for NetBSD target from Linux machine.
Configure script successfully invoked with following options,
./Configure -des -Dusecrosscompile
-Dtargetarch=netbsd
-Dosvers=3.0.0
-Dtargethost=10.184.22.48
-Dtargetuser=username
-Dusrinc=.../sb1-mips/usr/include
-Dlibpth=.../xc-sb1-mips/netbsd/Lib/lib
-Dcc=.../mips64-netbsdelf-gcc
Once after Configured, make wasn't successful.
It breaks at different places right from creating miniperlmain.o file.
For example following error occurred while generating gv.o file:
gv.c: In function `Perl_try_amagic_un':
gv.c:2018: error: void value not ignored as it ought to be
gv.c:2029: error: void value not ignored as it ought to be
gv.c:2029: warning: cast from pointer to integer of different size
gv.c: In function `Perl_try_amagic_bin':
gv.c:2061: error: void value not ignored as it ought to be
gv.c:2063: error: void value not ignored as it ought to be
make: *** [gv.o] Error 1
Now to get rid of these errors I've passed two flags namely -DPERL_IMPLICIT_CONTEXT and -DPERL_GCC_BRACE_GROUPS_FORBIDDEN to C Compiler CCFLAGS which solved the problem.
Since it is a custom build, want to know about all of the CCFLAGS that have to be passed in order to achieve a successful build?
What does each flag mean?
Is their any documentation available?
Use vagrant. It has some OpenBSD and FreeBSD images. Hopefuly those will be compatible with NetBSD.
You can get a build environment very fast with that. Then you can transfer and build your code happily :)

How do I use the BLAS library provided by MATLAB?

I have noticed that MATLAB provides the BLAS and LAPACK headers among others:
$ ls ${MATLAB_DIR}/extern/include/
blas.h engine.h lapack.h mat.h mclmcr.h mex.h mwutil.h
blascompat32.h fintrf.h libmatlbm.mlib matrix.h mclmcrrt.h mwdebug.h tmwtypes.h
emlrt.h io64.h libmatlbmx.mlib mclcppclass.h mcr.h mwservices.h
I would like to use them for my compiled code, so I tried to compile the following:
ex_blas.c
#include <blas.h>
int main()
{
return 0;
}
with the following command:
$ mex ex_blas.c
But this returns an error:
In file included from ex_blas.c:1:
/Applications/MATLAB_R2009b.app/extern/include/blas.h:51: error: syntax error before ‘*’ token
/Applications/MATLAB_R2009b.app/extern/include/blas.h:62: error: syntax error before ‘*’ token
/Applications/MATLAB_R2009b.app/extern/include/blas.h:77: error: syntax error before ‘*’ token
/Applications/MATLAB_R2009b.app/extern/include/blas.h:92: error: syntax error before ‘*’ token
/Applications/MATLAB_R2009b.app/extern/include/blas.h:103: error: syntax error before ‘ptrdiff_t’
/Applications/MATLAB_R2009b.app/extern/include/blas.h:122: error: syntax error before ‘ptrdiff_t’
Is the blas.h provided by MATLAB usable by a 3rd party application outside of MATLAB? If yes, what is causing the error above?
I'm not sure if it will completely resolve your problems, but you may also have to include stddef.h since that is where the ptrdiff_t type is defined.