[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Dynamic calling of C++ functions in Octave-2.1.39
From: |
Andy Adler |
Subject: |
Re: Dynamic calling of C++ functions in Octave-2.1.39 |
Date: |
Thu, 5 Dec 2002 22:11:19 -0500 (EST) |
Ravish wrote:
> Now i want to call some C++ functions which i have written using MS Visual
> C++ as my IDE
Here is a recipe using the MSVC++ comand line tools:
Summary: create a dll from the MSVC++. Export the functions
you need as "C" (ie. no name mangling). Link to this dll
from octave.
SAMPLE FILES:
-------- d_gcc.cc -------------
#include <octave/oct.h>
extern "C" {
int msgbox_plus_one( const char * text );
}
DEFUN_DLD (d_gcc,args, , "usage d_gcc('string')" ) {
octave_value_list retval;
retval(0) = (double) msgbox_plus_one( args(0).string_value().c_str() );
return retval;
}
-------- d_vcc.cpp ------------
#include <windows.h>
extern "C" {
BOOL WINAPI DllMain( HINSTANCE, DWORD, LPVOID ) { return 1;}
int __declspec(dllexport)
msgbox_plus_one( const char * text ){
return MessageBox( NULL, text, "TITLE",
MB_ABORTRETRYIGNORE | MB_SETFOREGROUND) + 1;
}
}
---------------
Proceedure:
1. Compile d_vcc with MSVC++ (or using your IDE)
$ cl -nologo -LD d_vcc.cpp user32.lib
d_vcc.cpp
Creating library d_vcc.lib and object d_vcc.exp
Note that only the *dll file is useful here.
Unfortunately, mkoctfile doesn't recognize *dll files
automatically, so you have to do the work yourself.
2. First to mkoctfile -v to get the commands
$ mkoctfile -v d_gcc.cc
g++ -c -I/usr/include/octave-2.1.40 -I/usr/include/octave-2.1.40/octave
-I/usr/include -mieee-fp -g -O2 -Wall d_gcc.cc -o d_gcc.o
g++ -shared -Wl,--export-all-symbols -Wl,--enable-auto-import -o d_gcc.oct
d_gcc.o -L/usr/lib/octave-2.1.40 -loctave.dll -lcruft.dll -loctinterp.dll
d_gcc.o(.text+0x39d): In function `_Z6Fd_gccRK17octave_value_listi':
/usr/include/c++/3.2/bits/basic_string.h:125: undefined reference to
`_msgbox_plus_one'
3. Now, copy the last line and add your *dll name
$ g++ -shared -Wl,--export-all-symbols -Wl,--enable-auto-import -o
d_gcc.oct d_gcc.o -L/usr/lib/octave-2.1.40 -loctave.dll -lcruft.dll
-loctinterp.dll d_vcc.dll
$ ls -l
total 2687
-rwxr-xr-x 1 adler None 882396 Dec 5 21:29 d_gcc.oct
-rwxr-xr-x 1 adler None 28672 Dec 5 21:22 d_vcc.dll
$ strip *.dll *.oct
$ ls -l
-rwxr-xr-x 1 adler None 242176 Dec 5 21:30 d_gcc.oct
-rwxr-xr-x 1 adler None 20992 Dec 5 21:30 d_vcc.dll
Note that one of the frustrating things with dld functions with
cygwin octave is that they are *much* bigger than comparative
linux ones.
4. Now open octave and run d_gcc and what the MessageBox popup.
Notes:
- Try to avoid doing tricky things in DllMain
- Use extern "C"
- You can't include both <windows.h> and <octave/oct.h> as
they have major namespace fights.
Comment:
I would like to propose a patch to mkoctfile which would allow
arguments to be passed directly to the linker. Is there a standard
switch for compilers that implies this function?
Andy
--
Andy Adler,
Assistant Professor, School of Information Technology and Engineering,
University of Ottawa, Tel:1(613)562-5800 X 2345, Email:address@hidden
-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.
Octave's home on the web: http://www.octave.org
How to fund new projects: http://www.octave.org/funding.html
Subscription information: http://www.octave.org/archive.html
-------------------------------------------------------------
- Re: Dynamic calling of C++ functions in Octave-2.1.39,
Andy Adler <=