Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Help with LoadLibrary and DLL

Subject: Help with LoadLibrary and DLL

From: omegayen

Date: 21 Dec, 2009 03:45:06

Message: 1 of 7

Hi,

I am try to follow the example on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/

I created two files

dll1.h

    #ifndef MY_API
    #define MY_API extern "C" __declspec(dllexport)
    #endif
    MY_API int add(int a, int b);
    MY_API int sub(int a, int b);

dll1.cpp

    #ifndef MY_API
    #define MY_API extern "C" __declspec(dllexport)
    #endif
    #include "dll1.h"
    int add(int a,int b)
    {
    return a + b;
    }
    int sub(int a,int b)
    {
    return a - b;
    }

I think used Visual Studio 2008 SP1 Version 9 Pro on Windows Vista 64 bit using Matlab 2009b 64 bit to compile a x64 dll file.

I then use load library function such as loadlibrary('D:\dll1\Debug\dll1.dll', 'D:\dll1\dll1.h'); in MATLAB

when I do this i get the error message

??? Error using ==> loadlibrary at 422
Building SimpleDll_thunk_pcwin64 failed. Compiler output is:
SimpleDll_thunk_pcwin64.c
C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(4) : error C2059: syntax error :
'string'
C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(5) : error C2059: syntax error :
'string'

can anyone help tell me why I am getting error C2059 syntax error 'string' I suspect it has something to do with the extern "C" __declspec(dllexport) line but am not sure how to change things up to work. thanks

Subject: Help with LoadLibrary and DLL

From: Philip Borghesani

Date: 21 Dec, 2009 16:52:35

Message: 2 of 7


"omegayen " <omegayen@ameritech.net> wrote in message news:hgmr02$pe7$1@fred.mathworks.com...
> Hi,
>
> I am try to follow the example on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/
>
> I created two files
>
> dll1.h
>
> #ifndef MY_API
> #define MY_API extern "C" __declspec(dllexport)
> #endif
> MY_API int add(int a, int b);
> MY_API int sub(int a, int b);
>
> dll1.cpp
>
> #ifndef MY_API
> #define MY_API extern "C" __declspec(dllexport)
> #endif
> #include "dll1.h"
> int add(int a,int b)
> {
> return a + b;
> }
> int sub(int a,int b)
> {
> return a - b;
> }
>
> I think used Visual Studio 2008 SP1 Version 9 Pro on Windows Vista 64 bit using Matlab 2009b 64 bit to compile a x64 dll file.
>
> I then use load library function such as loadlibrary('D:\dll1\Debug\dll1.dll', 'D:\dll1\dll1.h'); in MATLAB
>
> when I do this i get the error message
> ??? Error using ==> loadlibrary at 422
> Building SimpleDll_thunk_pcwin64 failed. Compiler output is:
> SimpleDll_thunk_pcwin64.c
> C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(4) : error C2059: syntax error :
> 'string'
> C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(5) : error C2059: syntax error :
> 'string'
>
> can anyone help tell me why I am getting error C2059 syntax error 'string' I suspect it has something to do with the extern "C"
> __declspec(dllexport) line but am not sure how to change things up to work. thanks

Try changing dll1.h to:
    #ifndef MY_API
    #define MY_API
    #endif
    MY_API int add(int a, int b);
    MY_API int sub(int a, int b);

The guard block around the #define statement in your cpp code is not needed but will not harm anything.

Phil

Subject: Help with LoadLibrary and DLL

From: omegayen

Date: 21 Dec, 2009 22:30:21

Message: 3 of 7

"Philip Borghesani" <philip_borghesani@mathworks.spam> wrote in message <hgo94j$p6b$1@fred.mathworks.com>...
>
> "omegayen " <omegayen@ameritech.net> wrote in message news:hgmr02$pe7$1@fred.mathworks.com...
> > Hi,
> >
> > I am try to follow the example on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/
> >
> > I created two files
> >
> > dll1.h
> >
> > #ifndef MY_API
> > #define MY_API extern "C" __declspec(dllexport)
> > #endif
> > MY_API int add(int a, int b);
> > MY_API int sub(int a, int b);
> >
> > dll1.cpp
> >
> > #ifndef MY_API
> > #define MY_API extern "C" __declspec(dllexport)
> > #endif
> > #include "dll1.h"
> > int add(int a,int b)
> > {
> > return a + b;
> > }
> > int sub(int a,int b)
> > {
> > return a - b;
> > }
> >
> > I think used Visual Studio 2008 SP1 Version 9 Pro on Windows Vista 64 bit using Matlab 2009b 64 bit to compile a x64 dll file.
> >
> > I then use load library function such as loadlibrary('D:\dll1\Debug\dll1.dll', 'D:\dll1\dll1.h'); in MATLAB
> >
> > when I do this i get the error message
> > ??? Error using ==> loadlibrary at 422
> > Building SimpleDll_thunk_pcwin64 failed. Compiler output is:
> > SimpleDll_thunk_pcwin64.c
> > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(4) : error C2059: syntax error :
> > 'string'
> > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(5) : error C2059: syntax error :
> > 'string'
> >
> > can anyone help tell me why I am getting error C2059 syntax error 'string' I suspect it has something to do with the extern "C"
> > __declspec(dllexport) line but am not sure how to change things up to work. thanks
>
> Try changing dll1.h to:
> #ifndef MY_API
> #define MY_API
> #endif
> MY_API int add(int a, int b);
> MY_API int sub(int a, int b);
>
> The guard block around the #define statement in your cpp code is not needed but will not harm anything.
>
> Phil
>

Hi I made the changed, just renamed functions..

Test4.h

    #ifndef MY_API
    #define MY_API
    #endif
    MY_API int add(int a, int b);
    MY_API int sub(int a, int b);

Test4.cpp

    #ifndef MY_API
    #define MY_API extern "C" __declspec(dllexport)
    #endif
    #include "Test4.h"
    int add(int a,int b)
    {
    return a + b;
    }
    int sub(int a,int b)
    {
    return a - b;
    }

loadlibrary('Test4','Test4.h')
Warning: Warnings messages were produced while parsing. Check the functions you
intend to use for correctness. Warning text can be viewed using:
[notfound,warnings]=loadlibrary(...)
> In loadlibrary at 394
Error loading library intermediate output follows.
The actual error is at the end of this output.
*********

Type '#line4"C:\\Users\\user_name_removed\\Documents\\VisualStudio2008\\Projects\\Test4\\x64\\Debug\\Test4.h"int' was not found. Defaulting to type error.

Found on line 5 of input from line 4 of file C:\\Users\\user_name_removed\\Documents\\Visual Studio 2008\\Projects\\Test4\\x64\\Debug\\Test4.h
*********
??? Error using ==> loadlibrary at 422
Building Test4_thunk_pcwin64 failed. Compiler output is:
Test4_thunk_pcwin64.c
Test4_thunk_pcwin64.c(51) : error C2014: preprocessor command must start as first nonwhite space
Test4_thunk_pcwin64.c(52) : error C2059: syntax error : '{'
Test4_thunk_pcwin64.c(59) : error C2014: preprocessor command must start as first nonwhite space

Subject: Help with LoadLibrary and DLL

From: omegayen

Date: 22 Dec, 2009 04:15:06

Message: 4 of 7

"omegayen " <omegayen@ameritech.net> wrote in message <hgostt$mlk$1@fred.mathworks.com>...
> "Philip Borghesani" <philip_borghesani@mathworks.spam> wrote in message <hgo94j$p6b$1@fred.mathworks.com>...
> >
> > "omegayen " <omegayen@ameritech.net> wrote in message news:hgmr02$pe7$1@fred.mathworks.com...
> > > Hi,
> > >
> > > I am try to follow the example on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/
> > >
> > > I created two files
> > >
> > > dll1.h
> > >
> > > #ifndef MY_API
> > > #define MY_API extern "C" __declspec(dllexport)
> > > #endif
> > > MY_API int add(int a, int b);
> > > MY_API int sub(int a, int b);
> > >
> > > dll1.cpp
> > >
> > > #ifndef MY_API
> > > #define MY_API extern "C" __declspec(dllexport)
> > > #endif
> > > #include "dll1.h"
> > > int add(int a,int b)
> > > {
> > > return a + b;
> > > }
> > > int sub(int a,int b)
> > > {
> > > return a - b;
> > > }
> > >
> > > I think used Visual Studio 2008 SP1 Version 9 Pro on Windows Vista 64 bit using Matlab 2009b 64 bit to compile a x64 dll file.
> > >
> > > I then use load library function such as loadlibrary('D:\dll1\Debug\dll1.dll', 'D:\dll1\dll1.h'); in MATLAB
> > >
> > > when I do this i get the error message
> > > ??? Error using ==> loadlibrary at 422
> > > Building SimpleDll_thunk_pcwin64 failed. Compiler output is:
> > > SimpleDll_thunk_pcwin64.c
> > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(4) : error C2059: syntax error :
> > > 'string'
> > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(5) : error C2059: syntax error :
> > > 'string'
> > >
> > > can anyone help tell me why I am getting error C2059 syntax error 'string' I suspect it has something to do with the extern "C"
> > > __declspec(dllexport) line but am not sure how to change things up to work. thanks
> >
> > Try changing dll1.h to:
> > #ifndef MY_API
> > #define MY_API
> > #endif
> > MY_API int add(int a, int b);
> > MY_API int sub(int a, int b);
> >
> > The guard block around the #define statement in your cpp code is not needed but will not harm anything.
> >
> > Phil
> >
>
> Hi I made the changed, just renamed functions..
>
> Test4.h
>
> #ifndef MY_API
> #define MY_API
> #endif
> MY_API int add(int a, int b);
> MY_API int sub(int a, int b);
>
> Test4.cpp
>
> #ifndef MY_API
> #define MY_API extern "C" __declspec(dllexport)
> #endif
> #include "Test4.h"
> int add(int a,int b)
> {
> return a + b;
> }
> int sub(int a,int b)
> {
> return a - b;
> }
>
> loadlibrary('Test4','Test4.h')
> Warning: Warnings messages were produced while parsing. Check the functions you
> intend to use for correctness. Warning text can be viewed using:
> [notfound,warnings]=loadlibrary(...)
> > In loadlibrary at 394
> Error loading library intermediate output follows.
> The actual error is at the end of this output.
> *********
>
> Type '#line4"C:\\Users\\user_name_removed\\Documents\\VisualStudio2008\\Projects\\Test4\\x64\\Debug\\Test4.h"int' was not found. Defaulting to type error.
>
> Found on line 5 of input from line 4 of file C:\\Users\\user_name_removed\\Documents\\Visual Studio 2008\\Projects\\Test4\\x64\\Debug\\Test4.h
> *********
> ??? Error using ==> loadlibrary at 422
> Building Test4_thunk_pcwin64 failed. Compiler output is:
> Test4_thunk_pcwin64.c
> Test4_thunk_pcwin64.c(51) : error C2014: preprocessor command must start as first nonwhite space
> Test4_thunk_pcwin64.c(52) : error C2059: syntax error : '{'
> Test4_thunk_pcwin64.c(59) : error C2014: preprocessor command must start as first nonwhite space

So I suspected there could be a problem with the 64 bit version opposed to the 32 bit version and was correct.

Thus I took MATLAB 2009b 32 bit version on a machine running Windows XP and tried the exact same thing as posted on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/ and referenced in my original post.

I used Visual C++ 2008 Express Edition to compile the dll using the instructions at http://msdn.microsoft.com/en-us/library/ms235636%28VS.80%29.aspx

Now when I do loadlibrary('dll1','dll1.h')
Warning: Message from C preprocessor:
lcc preprocessor warning: C:\Documents and Settings\user_name_removed\My Documents\Visual Studio 2008\Projects\dll1\Debug\dll1.h:5 No newline at end of file

A warning message is produced as shown above, the the functions is loaded in and works

A = calllib('dll1','add',12,10)

A =

    22

B = calllib('dll1','sub',12,10)

B =

     2

However, neither a 64 bit compiled (with Microsoft Visual Studio 2008 SP1 Version 9) dll or 32 bit dll will work on a machine running 64 bit MATLAB 2009b

I need this to work on the 64 bit version of MATLAB. Can anyone shed some light here.... MATLAB product help also lists Perl as needing to be available on 64 bit platforms. Thanks.

Subject: Help with LoadLibrary and DLL

From: omegayen

Date: 24 Dec, 2009 01:36:03

Message: 5 of 7

"omegayen " <omegayen@ameritech.net> wrote in message <hgph4a$ei9$1@fred.mathworks.com>...
> "omegayen " <omegayen@ameritech.net> wrote in message <hgostt$mlk$1@fred.mathworks.com>...
> > "Philip Borghesani" <philip_borghesani@mathworks.spam> wrote in message <hgo94j$p6b$1@fred.mathworks.com>...
> > >
> > > "omegayen " <omegayen@ameritech.net> wrote in message news:hgmr02$pe7$1@fred.mathworks.com...
> > > > Hi,
> > > >
> > > > I am try to follow the example on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/
> > > >
> > > > I created two files
> > > >
> > > > dll1.h
> > > >
> > > > #ifndef MY_API
> > > > #define MY_API extern "C" __declspec(dllexport)
> > > > #endif
> > > > MY_API int add(int a, int b);
> > > > MY_API int sub(int a, int b);
> > > >
> > > > dll1.cpp
> > > >
> > > > #ifndef MY_API
> > > > #define MY_API extern "C" __declspec(dllexport)
> > > > #endif
> > > > #include "dll1.h"
> > > > int add(int a,int b)
> > > > {
> > > > return a + b;
> > > > }
> > > > int sub(int a,int b)
> > > > {
> > > > return a - b;
> > > > }
> > > >
> > > > I think used Visual Studio 2008 SP1 Version 9 Pro on Windows Vista 64 bit using Matlab 2009b 64 bit to compile a x64 dll file.
> > > >
> > > > I then use load library function such as loadlibrary('D:\dll1\Debug\dll1.dll', 'D:\dll1\dll1.h'); in MATLAB
> > > >
> > > > when I do this i get the error message
> > > > ??? Error using ==> loadlibrary at 422
> > > > Building SimpleDll_thunk_pcwin64 failed. Compiler output is:
> > > > SimpleDll_thunk_pcwin64.c
> > > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(4) : error C2059: syntax error :
> > > > 'string'
> > > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(5) : error C2059: syntax error :
> > > > 'string'
> > > >
> > > > can anyone help tell me why I am getting error C2059 syntax error 'string' I suspect it has something to do with the extern "C"
> > > > __declspec(dllexport) line but am not sure how to change things up to work. thanks
> > >
> > > Try changing dll1.h to:
> > > #ifndef MY_API
> > > #define MY_API
> > > #endif
> > > MY_API int add(int a, int b);
> > > MY_API int sub(int a, int b);
> > >
> > > The guard block around the #define statement in your cpp code is not needed but will not harm anything.
> > >
> > > Phil
> > >
> >
> > Hi I made the changed, just renamed functions..
> >
> > Test4.h
> >
> > #ifndef MY_API
> > #define MY_API
> > #endif
> > MY_API int add(int a, int b);
> > MY_API int sub(int a, int b);
> >
> > Test4.cpp
> >
> > #ifndef MY_API
> > #define MY_API extern "C" __declspec(dllexport)
> > #endif
> > #include "Test4.h"
> > int add(int a,int b)
> > {
> > return a + b;
> > }
> > int sub(int a,int b)
> > {
> > return a - b;
> > }
> >
> > loadlibrary('Test4','Test4.h')
> > Warning: Warnings messages were produced while parsing. Check the functions you
> > intend to use for correctness. Warning text can be viewed using:
> > [notfound,warnings]=loadlibrary(...)
> > > In loadlibrary at 394
> > Error loading library intermediate output follows.
> > The actual error is at the end of this output.
> > *********
> >
> > Type '#line4"C:\\Users\\user_name_removed\\Documents\\VisualStudio2008\\Projects\\Test4\\x64\\Debug\\Test4.h"int' was not found. Defaulting to type error.
> >
> > Found on line 5 of input from line 4 of file C:\\Users\\user_name_removed\\Documents\\Visual Studio 2008\\Projects\\Test4\\x64\\Debug\\Test4.h
> > *********
> > ??? Error using ==> loadlibrary at 422
> > Building Test4_thunk_pcwin64 failed. Compiler output is:
> > Test4_thunk_pcwin64.c
> > Test4_thunk_pcwin64.c(51) : error C2014: preprocessor command must start as first nonwhite space
> > Test4_thunk_pcwin64.c(52) : error C2059: syntax error : '{'
> > Test4_thunk_pcwin64.c(59) : error C2014: preprocessor command must start as first nonwhite space
>
> So I suspected there could be a problem with the 64 bit version opposed to the 32 bit version and was correct.
>
> Thus I took MATLAB 2009b 32 bit version on a machine running Windows XP and tried the exact same thing as posted on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/ and referenced in my original post.
>
> I used Visual C++ 2008 Express Edition to compile the dll using the instructions at http://msdn.microsoft.com/en-us/library/ms235636%28VS.80%29.aspx
>
> Now when I do loadlibrary('dll1','dll1.h')
> Warning: Message from C preprocessor:
> lcc preprocessor warning: C:\Documents and Settings\user_name_removed\My Documents\Visual Studio 2008\Projects\dll1\Debug\dll1.h:5 No newline at end of file
>
> A warning message is produced as shown above, the the functions is loaded in and works
>
> A = calllib('dll1','add',12,10)
>
> A =
>
> 22
>
> B = calllib('dll1','sub',12,10)
>
> B =
>
> 2
>
> However, neither a 64 bit compiled (with Microsoft Visual Studio 2008 SP1 Version 9) dll or 32 bit dll will work on a machine running 64 bit MATLAB 2009b
>
> I need this to work on the 64 bit version of MATLAB. Can anyone shed some light here.... MATLAB product help also lists Perl as needing to be available on 64 bit platforms. Thanks.

Hi, So I have attempted to load in a 64 bit dll on MATLAB 2009b on both a computer running Windows 7 and another one running Windows Vista. Both have Visual Studio 2008 SP1 and SDK.

Neither will allow for the dll to load using loadlibrary after being built as discussed in the previous post.

32 bit MATLAB on Windows loads dll files as expected.

Since I need this is 64 bit, I also tried on Linux on a machine running Ubuntu 9.04 and Matlab 2009 A.

----- Test.h

 int add(int a, int b);
 int sub(int a, int b);

---- Test.cpp

#include "Test.h"
int add(int a,int b)
{
return a + b;
}
int sub(int a,int b)
{
return a - b;
}

------

In order to install gcc/g++ I opened the terminal and executed the following commands.

sudo -i
sudo aptitude update
sudo aptitude install build-essential

I entered the admin password when prompted, downloaded and installed and rebooted.

Next I create the .h and .cpp files as indicated above....and placed them in my 'home folder'

Then I opened the terminal to compile to a .so file (equivalent of dll for Linux)

g++ -fPIC -shared Test.cpp -o Test.so

Then launched MATLAB and typed in

loadlibrary('/home/omegayen/Test','/home/omegayen/Test.h')

This then gave

Warning: The function 'add' was not found in the library
> In loadlibrary at 455
Warning: The function 'sub' was not found in the library
> In loadlibrary at 455

any thoughts on how I can get this to work in Linux? Thanks.

Subject: Help with LoadLibrary and DLL

From: omegayen

Date: 25 Dec, 2009 01:52:04

Message: 6 of 7

"omegayen " <omegayen@ameritech.net> wrote in message <hgugi3$mkg$1@fred.mathworks.com>...
> "omegayen " <omegayen@ameritech.net> wrote in message <hgph4a$ei9$1@fred.mathworks.com>...
> > "omegayen " <omegayen@ameritech.net> wrote in message <hgostt$mlk$1@fred.mathworks.com>...
> > > "Philip Borghesani" <philip_borghesani@mathworks.spam> wrote in message <hgo94j$p6b$1@fred.mathworks.com>...
> > > >
> > > > "omegayen " <omegayen@ameritech.net> wrote in message news:hgmr02$pe7$1@fred.mathworks.com...
> > > > > Hi,
> > > > >
> > > > > I am try to follow the example on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/
> > > > >
> > > > > I created two files
> > > > >
> > > > > dll1.h
> > > > >
> > > > > #ifndef MY_API
> > > > > #define MY_API extern "C" __declspec(dllexport)
> > > > > #endif
> > > > > MY_API int add(int a, int b);
> > > > > MY_API int sub(int a, int b);
> > > > >
> > > > > dll1.cpp
> > > > >
> > > > > #ifndef MY_API
> > > > > #define MY_API extern "C" __declspec(dllexport)
> > > > > #endif
> > > > > #include "dll1.h"
> > > > > int add(int a,int b)
> > > > > {
> > > > > return a + b;
> > > > > }
> > > > > int sub(int a,int b)
> > > > > {
> > > > > return a - b;
> > > > > }
> > > > >
> > > > > I think used Visual Studio 2008 SP1 Version 9 Pro on Windows Vista 64 bit using Matlab 2009b 64 bit to compile a x64 dll file.
> > > > >
> > > > > I then use load library function such as loadlibrary('D:\dll1\Debug\dll1.dll', 'D:\dll1\dll1.h'); in MATLAB
> > > > >
> > > > > when I do this i get the error message
> > > > > ??? Error using ==> loadlibrary at 422
> > > > > Building SimpleDll_thunk_pcwin64 failed. Compiler output is:
> > > > > SimpleDll_thunk_pcwin64.c
> > > > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(4) : error C2059: syntax error :
> > > > > 'string'
> > > > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(5) : error C2059: syntax error :
> > > > > 'string'
> > > > >
> > > > > can anyone help tell me why I am getting error C2059 syntax error 'string' I suspect it has something to do with the extern "C"
> > > > > __declspec(dllexport) line but am not sure how to change things up to work. thanks
> > > >
> > > > Try changing dll1.h to:
> > > > #ifndef MY_API
> > > > #define MY_API
> > > > #endif
> > > > MY_API int add(int a, int b);
> > > > MY_API int sub(int a, int b);
> > > >
> > > > The guard block around the #define statement in your cpp code is not needed but will not harm anything.
> > > >
> > > > Phil
> > > >
> > >
> > > Hi I made the changed, just renamed functions..
> > >
> > > Test4.h
> > >
> > > #ifndef MY_API
> > > #define MY_API
> > > #endif
> > > MY_API int add(int a, int b);
> > > MY_API int sub(int a, int b);
> > >
> > > Test4.cpp
> > >
> > > #ifndef MY_API
> > > #define MY_API extern "C" __declspec(dllexport)
> > > #endif
> > > #include "Test4.h"
> > > int add(int a,int b)
> > > {
> > > return a + b;
> > > }
> > > int sub(int a,int b)
> > > {
> > > return a - b;
> > > }
> > >
> > > loadlibrary('Test4','Test4.h')
> > > Warning: Warnings messages were produced while parsing. Check the functions you
> > > intend to use for correctness. Warning text can be viewed using:
> > > [notfound,warnings]=loadlibrary(...)
> > > > In loadlibrary at 394
> > > Error loading library intermediate output follows.
> > > The actual error is at the end of this output.
> > > *********
> > >
> > > Type '#line4"C:\\Users\\user_name_removed\\Documents\\VisualStudio2008\\Projects\\Test4\\x64\\Debug\\Test4.h"int' was not found. Defaulting to type error.
> > >
> > > Found on line 5 of input from line 4 of file C:\\Users\\user_name_removed\\Documents\\Visual Studio 2008\\Projects\\Test4\\x64\\Debug\\Test4.h
> > > *********
> > > ??? Error using ==> loadlibrary at 422
> > > Building Test4_thunk_pcwin64 failed. Compiler output is:
> > > Test4_thunk_pcwin64.c
> > > Test4_thunk_pcwin64.c(51) : error C2014: preprocessor command must start as first nonwhite space
> > > Test4_thunk_pcwin64.c(52) : error C2059: syntax error : '{'
> > > Test4_thunk_pcwin64.c(59) : error C2014: preprocessor command must start as first nonwhite space
> >
> > So I suspected there could be a problem with the 64 bit version opposed to the 32 bit version and was correct.
> >
> > Thus I took MATLAB 2009b 32 bit version on a machine running Windows XP and tried the exact same thing as posted on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/ and referenced in my original post.
> >
> > I used Visual C++ 2008 Express Edition to compile the dll using the instructions at http://msdn.microsoft.com/en-us/library/ms235636%28VS.80%29.aspx
> >
> > Now when I do loadlibrary('dll1','dll1.h')
> > Warning: Message from C preprocessor:
> > lcc preprocessor warning: C:\Documents and Settings\user_name_removed\My Documents\Visual Studio 2008\Projects\dll1\Debug\dll1.h:5 No newline at end of file
> >
> > A warning message is produced as shown above, the the functions is loaded in and works
> >
> > A = calllib('dll1','add',12,10)
> >
> > A =
> >
> > 22
> >
> > B = calllib('dll1','sub',12,10)
> >
> > B =
> >
> > 2
> >
> > However, neither a 64 bit compiled (with Microsoft Visual Studio 2008 SP1 Version 9) dll or 32 bit dll will work on a machine running 64 bit MATLAB 2009b
> >
> > I need this to work on the 64 bit version of MATLAB. Can anyone shed some light here.... MATLAB product help also lists Perl as needing to be available on 64 bit platforms. Thanks.
>
> Hi, So I have attempted to load in a 64 bit dll on MATLAB 2009b on both a computer running Windows 7 and another one running Windows Vista. Both have Visual Studio 2008 SP1 and SDK.
>
> Neither will allow for the dll to load using loadlibrary after being built as discussed in the previous post.
>
> 32 bit MATLAB on Windows loads dll files as expected.
>
> Since I need this is 64 bit, I also tried on Linux on a machine running Ubuntu 9.04 and Matlab 2009 A.
>
> ----- Test.h
>
> int add(int a, int b);
> int sub(int a, int b);
>
> ---- Test.cpp
>
> #include "Test.h"
> int add(int a,int b)
> {
> return a + b;
> }
> int sub(int a,int b)
> {
> return a - b;
> }
>
> ------
>
> In order to install gcc/g++ I opened the terminal and executed the following commands.
>
> sudo -i
> sudo aptitude update
> sudo aptitude install build-essential
>
> I entered the admin password when prompted, downloaded and installed and rebooted.
>
> Next I create the .h and .cpp files as indicated above....and placed them in my 'home folder'
>
> Then I opened the terminal to compile to a .so file (equivalent of dll for Linux)
>
> g++ -fPIC -shared Test.cpp -o Test.so
>
> Then launched MATLAB and typed in
>
> loadlibrary('/home/omegayen/Test','/home/omegayen/Test.h')
>
> This then gave
>
> Warning: The function 'add' was not found in the library
> > In loadlibrary at 455
> Warning: The function 'sub' was not found in the library
> > In loadlibrary at 455
>
> any thoughts on how I can get this to work in Linux? Thanks.

So I figured out how to get it to work on Linux (Ubuntu 9.04 - Matlab 2009 A)

turns out I was not compiling properly

Test.h
----
int add(int a, int b);
int sub(int a, int b);
---
Test.c
-----
int add(int a,int b)
{
return a + b;
}
int sub(int a,int b)
{
return a - b;
}
---

compile in terminal using

gcc -c -fPIC Test.c -o Test.o
gcc -shared -Wl,-soname,Test.so.1 -o Test.so Test.o

any help on Windows 64 bit still appreciated...

Subject: Help with LoadLibrary and DLL

From: omegayen

Date: 25 Dec, 2009 09:04:03

Message: 7 of 7

"omegayen " <omegayen@ameritech.net> wrote in message <hh15s4$imi$1@fred.mathworks.com>...
> "omegayen " <omegayen@ameritech.net> wrote in message <hgugi3$mkg$1@fred.mathworks.com>...
> > "omegayen " <omegayen@ameritech.net> wrote in message <hgph4a$ei9$1@fred.mathworks.com>...
> > > "omegayen " <omegayen@ameritech.net> wrote in message <hgostt$mlk$1@fred.mathworks.com>...
> > > > "Philip Borghesani" <philip_borghesani@mathworks.spam> wrote in message <hgo94j$p6b$1@fred.mathworks.com>...
> > > > >
> > > > > "omegayen " <omegayen@ameritech.net> wrote in message news:hgmr02$pe7$1@fred.mathworks.com...
> > > > > > Hi,
> > > > > >
> > > > > > I am try to follow the example on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/
> > > > > >
> > > > > > I created two files
> > > > > >
> > > > > > dll1.h
> > > > > >
> > > > > > #ifndef MY_API
> > > > > > #define MY_API extern "C" __declspec(dllexport)
> > > > > > #endif
> > > > > > MY_API int add(int a, int b);
> > > > > > MY_API int sub(int a, int b);
> > > > > >
> > > > > > dll1.cpp
> > > > > >
> > > > > > #ifndef MY_API
> > > > > > #define MY_API extern "C" __declspec(dllexport)
> > > > > > #endif
> > > > > > #include "dll1.h"
> > > > > > int add(int a,int b)
> > > > > > {
> > > > > > return a + b;
> > > > > > }
> > > > > > int sub(int a,int b)
> > > > > > {
> > > > > > return a - b;
> > > > > > }
> > > > > >
> > > > > > I think used Visual Studio 2008 SP1 Version 9 Pro on Windows Vista 64 bit using Matlab 2009b 64 bit to compile a x64 dll file.
> > > > > >
> > > > > > I then use load library function such as loadlibrary('D:\dll1\Debug\dll1.dll', 'D:\dll1\dll1.h'); in MATLAB
> > > > > >
> > > > > > when I do this i get the error message
> > > > > > ??? Error using ==> loadlibrary at 422
> > > > > > Building SimpleDll_thunk_pcwin64 failed. Compiler output is:
> > > > > > SimpleDll_thunk_pcwin64.c
> > > > > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(4) : error C2059: syntax error :
> > > > > > 'string'
> > > > > > C:\Users\user_name_removed\Documents\Visual Studio 2008\Projects\SimpleDll\x64\Debug\SimpleDll.h(5) : error C2059: syntax error :
> > > > > > 'string'
> > > > > >
> > > > > > can anyone help tell me why I am getting error C2059 syntax error 'string' I suspect it has something to do with the extern "C"
> > > > > > __declspec(dllexport) line but am not sure how to change things up to work. thanks
> > > > >
> > > > > Try changing dll1.h to:
> > > > > #ifndef MY_API
> > > > > #define MY_API
> > > > > #endif
> > > > > MY_API int add(int a, int b);
> > > > > MY_API int sub(int a, int b);
> > > > >
> > > > > The guard block around the #define statement in your cpp code is not needed but will not harm anything.
> > > > >
> > > > > Phil
> > > > >
> > > >
> > > > Hi I made the changed, just renamed functions..
> > > >
> > > > Test4.h
> > > >
> > > > #ifndef MY_API
> > > > #define MY_API
> > > > #endif
> > > > MY_API int add(int a, int b);
> > > > MY_API int sub(int a, int b);
> > > >
> > > > Test4.cpp
> > > >
> > > > #ifndef MY_API
> > > > #define MY_API extern "C" __declspec(dllexport)
> > > > #endif
> > > > #include "Test4.h"
> > > > int add(int a,int b)
> > > > {
> > > > return a + b;
> > > > }
> > > > int sub(int a,int b)
> > > > {
> > > > return a - b;
> > > > }
> > > >
> > > > loadlibrary('Test4','Test4.h')
> > > > Warning: Warnings messages were produced while parsing. Check the functions you
> > > > intend to use for correctness. Warning text can be viewed using:
> > > > [notfound,warnings]=loadlibrary(...)
> > > > > In loadlibrary at 394
> > > > Error loading library intermediate output follows.
> > > > The actual error is at the end of this output.
> > > > *********
> > > >
> > > > Type '#line4"C:\\Users\\user_name_removed\\Documents\\VisualStudio2008\\Projects\\Test4\\x64\\Debug\\Test4.h"int' was not found. Defaulting to type error.
> > > >
> > > > Found on line 5 of input from line 4 of file C:\\Users\\user_name_removed\\Documents\\Visual Studio 2008\\Projects\\Test4\\x64\\Debug\\Test4.h
> > > > *********
> > > > ??? Error using ==> loadlibrary at 422
> > > > Building Test4_thunk_pcwin64 failed. Compiler output is:
> > > > Test4_thunk_pcwin64.c
> > > > Test4_thunk_pcwin64.c(51) : error C2014: preprocessor command must start as first nonwhite space
> > > > Test4_thunk_pcwin64.c(52) : error C2059: syntax error : '{'
> > > > Test4_thunk_pcwin64.c(59) : error C2014: preprocessor command must start as first nonwhite space
> > >
> > > So I suspected there could be a problem with the 64 bit version opposed to the 32 bit version and was correct.
> > >
> > > Thus I took MATLAB 2009b 32 bit version on a machine running Windows XP and tried the exact same thing as posted on http://guoliangye.com/2009/01/21/how-to-write-a-dll-in-cc-and-call-it-in-matlab/ and referenced in my original post.
> > >
> > > I used Visual C++ 2008 Express Edition to compile the dll using the instructions at http://msdn.microsoft.com/en-us/library/ms235636%28VS.80%29.aspx
> > >
> > > Now when I do loadlibrary('dll1','dll1.h')
> > > Warning: Message from C preprocessor:
> > > lcc preprocessor warning: C:\Documents and Settings\user_name_removed\My Documents\Visual Studio 2008\Projects\dll1\Debug\dll1.h:5 No newline at end of file
> > >
> > > A warning message is produced as shown above, the the functions is loaded in and works
> > >
> > > A = calllib('dll1','add',12,10)
> > >
> > > A =
> > >
> > > 22
> > >
> > > B = calllib('dll1','sub',12,10)
> > >
> > > B =
> > >
> > > 2
> > >
> > > However, neither a 64 bit compiled (with Microsoft Visual Studio 2008 SP1 Version 9) dll or 32 bit dll will work on a machine running 64 bit MATLAB 2009b
> > >
> > > I need this to work on the 64 bit version of MATLAB. Can anyone shed some light here.... MATLAB product help also lists Perl as needing to be available on 64 bit platforms. Thanks.
> >
> > Hi, So I have attempted to load in a 64 bit dll on MATLAB 2009b on both a computer running Windows 7 and another one running Windows Vista. Both have Visual Studio 2008 SP1 and SDK.
> >
> > Neither will allow for the dll to load using loadlibrary after being built as discussed in the previous post.
> >
> > 32 bit MATLAB on Windows loads dll files as expected.
> >
> > Since I need this is 64 bit, I also tried on Linux on a machine running Ubuntu 9.04 and Matlab 2009 A.
> >
> > ----- Test.h
> >
> > int add(int a, int b);
> > int sub(int a, int b);
> >
> > ---- Test.cpp
> >
> > #include "Test.h"
> > int add(int a,int b)
> > {
> > return a + b;
> > }
> > int sub(int a,int b)
> > {
> > return a - b;
> > }
> >
> > ------
> >
> > In order to install gcc/g++ I opened the terminal and executed the following commands.
> >
> > sudo -i
> > sudo aptitude update
> > sudo aptitude install build-essential
> >
> > I entered the admin password when prompted, downloaded and installed and rebooted.
> >
> > Next I create the .h and .cpp files as indicated above....and placed them in my 'home folder'
> >
> > Then I opened the terminal to compile to a .so file (equivalent of dll for Linux)
> >
> > g++ -fPIC -shared Test.cpp -o Test.so
> >
> > Then launched MATLAB and typed in
> >
> > loadlibrary('/home/omegayen/Test','/home/omegayen/Test.h')
> >
> > This then gave
> >
> > Warning: The function 'add' was not found in the library
> > > In loadlibrary at 455
> > Warning: The function 'sub' was not found in the library
> > > In loadlibrary at 455
> >
> > any thoughts on how I can get this to work in Linux? Thanks.
>
> So I figured out how to get it to work on Linux (Ubuntu 9.04 - Matlab 2009 A)
>
> turns out I was not compiling properly
>
> Test.h
> ----
> int add(int a, int b);
> int sub(int a, int b);
> ---
> Test.c
> -----
> int add(int a,int b)
> {
> return a + b;
> }
> int sub(int a,int b)
> {
> return a - b;
> }
> ---
>
> compile in terminal using
>
> gcc -c -fPIC Test.c -o Test.o
> gcc -shared -Wl,-soname,Test.so.1 -o Test.so Test.o
>
> any help on Windows 64 bit still appreciated...

Okay I figured it on on Windows 64 bit, just updating this in case anyone would like to know

need to create a .c file not a .cpp file

for example
---
//test.c
__declspec(dllexport) int sum(int a, int b) {
    return a + b;
}
---
//test.h
int sum(int, int);
---

Then you must compile using Microsoft Visual Studio 2008 C++ Pro Sp1 and make sure you doing it for x64 configuration.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us