From: Bruno on
Hi!

I want to build a static library (.lib) with some subroutines to be used by my s-function. As a simple test, this library (let's call it mylib) only contains a subroutine (let's call it subrout) that sums its two inputs and outputs the solution.

This library was built with Microsoft Visual Studio 2005 Professional Edition.

Once in Matlab, I add the library to the same directory in which I am working. Then I use the mex command in the following way "mex sfunction.c mylib.lib".

While compiling I get:" warning c4013: 'subrout' undefined; assuming extern returning int ".
While linking I get "error LNK2019: unresolved external symbol _subrout referenced in function _mdlOutputs."

I have had this error in the past and it was always because I forgot to include some library or it wasn't in its right place. In dispair I even added the library directly in the linker flags in MEXOPTS.bat; still the problem remains. For this reason I think that the possibility of Matlab not "seeing" the library could be ruled out.

Does anyone have any idea about how to solve this problem? Could it be some definition I missed while building the library in Microsoft Visual Studio?

Thanks in advance for your help,
Bruno
From: James Tursa on
"Bruno " <brocaaugusto(a)hotmail.com> wrote in message <htgt4o$rl$1(a)fred.mathworks.com>...
>
> Once in Matlab, I add the library to the same directory in which I am working. Then I use the mex command in the following way "mex sfunction.c mylib.lib".
>
> While compiling I get:" warning c4013: 'subrout' undefined; assuming extern returning int ".

You are missing a prototype for the function subrout. The first time the compiler sees this function reference is when it is called, so it assumes it is an extern function returning int. You should put prototypes for *all* of your functions at the beginning of your code.

> While linking I get "error LNK2019: unresolved external symbol _subrout referenced in function _mdlOutputs."

How was the library mylib.lib compiled? Is the subrout name actually exported so the linker can see it?

James Tursa
From: Bruno on
"James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hth13q$6k5$1(a)fred.mathworks.com>...
> "Bruno " <brocaaugusto(a)hotmail.com> wrote in message <htgt4o$rl$1(a)fred.mathworks.com>...
> >
> > Once in Matlab, I add the library to the same directory in which I am working. Then I use the mex command in the following way "mex sfunction.c mylib.lib".
> >
> > While compiling I get:" warning c4013: 'subrout' undefined; assuming extern returning int ".
>
> You are missing a prototype for the function subrout. The first time the compiler sees this function reference is when it is called, so it assumes it is an extern function returning int. You should put prototypes for *all* of your functions at the beginning of your code.
>
> > While linking I get "error LNK2019: unresolved external symbol _subrout referenced in function _mdlOutputs."
>
> How was the library mylib.lib compiled? Is the subrout name actually exported so the linker can see it?
>
> James Tursa

Hi James!

Thank for your reply. I will add prototypes to fix that warning. In what concerns the compiling of mylib.lib, I created a new win32 project in Visual Studio and selected static library. Then, added my .c file with the definition of subrout and a .h file with its declaration. Finally I built the project with no errors or warnings. I do not know how to make sure the name subrout was exported so that the linker sees it.

I don't know if the compiler flags help, but here they are:
"/O2 /GL /D "WIN32" /D "NDEBUG" /D "_LIB" /D "_UNICODE" /D "UNICODE" /FD /EHsc /MD /Zc:wchar_t- /Fo"Release\\" /Fd"Release\vc80.pdb" /W3 /nologo /c /Zi /TC /errorReport:prompt"

Best Regards,
Bruno
From: Bruno on
"Bruno " <brocaaugusto(a)hotmail.com> wrote in message <hth3ig$lhb$1(a)fred.mathworks.com>...
> "James Tursa" <aclassyguy_with_a_k_not_a_c(a)hotmail.com> wrote in message <hth13q$6k5$1(a)fred.mathworks.com>...
> > "Bruno " <brocaaugusto(a)hotmail.com> wrote in message <htgt4o$rl$1(a)fred.mathworks.com>...
> > >
> > > Once in Matlab, I add the library to the same directory in which I am working. Then I use the mex command in the following way "mex sfunction.c mylib.lib".
> > >
> > > While compiling I get:" warning c4013: 'subrout' undefined; assuming extern returning int ".
> >
> > You are missing a prototype for the function subrout. The first time the compiler sees this function reference is when it is called, so it assumes it is an extern function returning int. You should put prototypes for *all* of your functions at the beginning of your code.
> >
> > > While linking I get "error LNK2019: unresolved external symbol _subrout referenced in function _mdlOutputs."
> >
> > How was the library mylib.lib compiled? Is the subrout name actually exported so the linker can see it?
> >
> > James Tursa
>
> Hi James!
>
> Thank for your reply. I will add prototypes to fix that warning. In what concerns the compiling of mylib.lib, I created a new win32 project in Visual Studio and selected static library. Then, added my .c file with the definition of subrout and a .h file with its declaration. Finally I built the project with no errors or warnings. I do not know how to make sure the name subrout was exported so that the linker sees it.
>
> I don't know if the compiler flags help, but here they are:
> "/O2 /GL /D "WIN32" /D "NDEBUG" /D "_LIB" /D "_UNICODE" /D "UNICODE" /FD /EHsc /MD /Zc:wchar_t- /Fo"Release\\" /Fd"Release\vc80.pdb" /W3 /nologo /c /Zi /TC /errorReport:prompt"
>
> Best Regards,
> Bruno

I figured out that I acctually did not export the subrout so that the linker could see it. Everything seems to working now. Thank you very much for your tip.

Bruno