views:

58

answers:

3

Hi all, I have an external library made using C code. I wish to call a function from the library in my c++ project. The original format of the function prototype was.

extern void butterThreeBp(real_T eml_dt, real_T eml_fl, real_T eml_fu, real_T eml_b3[7], real_T eml_a3[7]);

And this caused the following linker error in MSVC2008

error LNK2019: unresolved external symbol "void __cdecl butterThreeBp(double,double,double,double * const,double * const)" (?butterThreeBp@@YAXNNNQAN0@Z) referenced in function "public: void __thiscall myThread::setDRNLc(double)" (?setDRNLc@myThread@@QAEXN@Z)

I then changed this prototype to

extern "C" void ...

and then get the following error:

\butterThreeBp.lib : fatal error LNK1127: library is corrupt

I have rebuilt the library numerous times and am pretty certain that it is not corrupt. Any help would be great. Thanks!!

+1  A: 

IIRC you should add extern "C" only when compiling with C++ compiler.

Something like that:

#ifdef __cplusplus
extern "C" {
#endif

// the declarations

#ifdef __cplusplus
}
#endif  
buratinas
Ah, now this is useful. Now I can wrap the headers for my C libraries in the extern "C" { construct without having to modify the source code of the headers directly (putting the extern "C" bit of code at the beginning of each function declaration). In case anyone has come here via a google search, this is a good link for more info http://www.parashift.com/c++-faq-lite/mixing-c-and-cpp.html#faq-32.3 However, this does still not relieve the reported error message.
learnvst
A: 

Got it! I generated the library code using the MATLAB embedded coder and was using Matlab's Lcc built in compiler to build the library. I am working on the main application in MSVC, therefore I guess there must have been some calling convention problem between the library (Lcc) and my main project (MSVC). This problem was particularly difficult to spot because one of my libraries (ButterOneLp) worked fine, even though it was generated using Lcc. However, another library did not work.

The problem was fixed by using the

mex -setup

. . . command in MATLAB and changing the default compiler to MSVC as opposed to the default Lcc.

I think that Lcc is based on gcc, so if anybody knows how to change the calling convention in the C++ code so that gcc/lcc libraries can be called from MSVC, that would be great. However, that is a whole different question to the original on posted.

The moral of this story is to check that your library and calling application are/were built using the same compiler if you get a "fatal error LNK1127: library is corrupt".

learnvst
Calling convention may not be the problem (in fact given the error - unlikely). I suspect that it is rather that LCC libraries are an entirely different format and also possibly the object files it contains are ELF rather COFF.
Clifford
+2  A: 

You do not have to use the included LCC compiler with MATLAB. The simplest solution is to get MATLAB to use VC++. http://www.mathworks.com/support/compilers/R2010b/index.html

Clifford
Exactomondo Clifford. See the edit to my previous post. I realised that I had forgotten to explicitly write the solution. Thanks for the help!
learnvst
The error is still somewhat insidious though as it causes errors on some libraries and not others. My problem is solved, but I remain curious about why the error is not all-or-nothing.
learnvst