61321

Calling a native C compiled with VS 2005 from C++/CLI Visual studio 2010 - Cannot open .lib file…

Question:

Hi I want to call functions from a C dll to C++/CLI. The C functions are declared extern. I followed this tutorial for linking the dll: <a href="http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/84deabaa-ae82-47cc-aac0-592f5a8dfa22" rel="nofollow">http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/84deabaa-ae82-47cc-aac0-592f5a8dfa22</a> and then in my C++/CLI dll I have the following:

// testWrapper.h #pragma once using namespace System; namespace testWrapper { extern "C" int GtoCalcImpliedVolatility2( double premium, int optionType, double underPrice, double strike, double yearsToExpiry, double yearsToPayment, double volGuess, double discountRate, double dividendRate, double growthRate, double *impliedVol); public ref class MyNamesSplitterClass { private: public: int TestSomething() { double vol; int _status = GtoCalcImpliedVolatility2( 0.098328, //premium 'P', //optionType 0.950000, //underPrice 1.050000, //strike 0.900000, //yearsToExpiry 0.95, //yearsToPayment 0.01, //volGuess 0.02, //discountRate 0.03, //dividendRate 0.04,//growthRate &vol); } }; }

I know that I'm supposed to only give signatures in the .h and then write function code in .cpp but for the sake of testing, I'm writing it in the same place. These are the errors I get:

Error 3 error LNK1120: 2 unresolved externals (etc...) Error 1 error LNK2028: unresolved token (0A000006) "int __cdecl testWrapper::GtoCalcImpliedVolatility2(double,int,double,double,double,double,double,double,double,double,double *)" (?GtoCalcImpliedVolatility2@testWrapper@@$$FYAHNHNNNNNNNNPAN@Z) referenced in function "public: int __clrcall testWrapper::MyNamesSplitterClass::TestSomething(void)" (?TestSomething@MyNamesSplitterClass@testWrapper@@$$FQ$AAMHXZ) Error 2 error LNK2019: unresolved external symbol "int __cdecl testWrapper::GtoCalcImpliedVolatility2(double,int,double,double,double,double,double,double,double,double,double *)" (?GtoCalcImpliedVolatility2@testWrapper@@$$FYAHNHNNNNNNNNPAN@Z) referenced in function "public: int __clrcall testWrapper::MyNamesSplitterClass::TestSomething(void)" (?TestSomething@MyNamesSplitterClass@testWrapper@@$$FQ$AAMHXZ)

I have tried looking them up but can't find much information about them apart from the fact that they are due to bad linking...

UPDATE: Organisation of projects...

So for my c++/cli project I have written a .hpp as follows that includes my 880+ headers:

extern "C" { #include "accrued.h" ... #include "gtobf.h" // this contains GtoCalcImpliedVolatility2 ... #include "../includep/zcsmthp.h" }

the following is gtobf.h:

#ifndef ALIB_GTOBF_H #define ALIB_GTOBF_H #include "cgeneral.h" #include "optprop.h" /* TOptionProperties */ #include "cmemory.h" /* FREE macro */ #include "rtnewton.h" /* TObjectFunc */ #ifdef __cplusplus extern "C" { #endif /*f * Calculates volatility implied by the price of a given option. */ //#define GTO_EXPORT(type) __declspec(dllexport) type GTO_EXPORT(int ) GtoCalcImpliedVolatility2( double premium, /* (I) Option premium */ int optionType, /* (I) GtoOPTION_PUT/GtoOPTION_CALL */ double underPrice, /* (I) Underlyer price */ double strike, /* (I) Strike/exercise price */ double yearsToExpiry, /* (I) Years to option's expiry */ double yearsToPayment, /* (I) Years till option pays off */ double volGuess, /* (I) Volatility guess (.06 for 6%) */ double discountRate, /* (I) Discount rate (ann. compound) */ double dividendRate, /* (I) Dividend rate (ann. compound) */ double growthRate, /* (I) Growth rate (ann. compound) */ double *impliedVol); /* (O) Implied Volatility */ //...other functions #ifdef __cplusplus } #endif #endif /* ALIB_GTOBF_H */

Then in my c++/cli wrapper I include my all.hpp that includes all the other headers... With all this I am still getting the errors.

I am sure that the function is exported because I have used the dll from C# using P/Invoke...

!!!!!!!!!!!!!!! EDIT:

I had not indicated the correct path for the lib... Now I have sorted that out and I am getting a LNK1104 error saying it can't open my .lib

Answer1:

Where GtoCalcImpliedVolatility2 C function is implemented? If in separate .c or .cpp file, add this file to the project. If it is placed in Dll, add .lib file to the project linker dependencies.

Some details: open Project - Properties - Configuration Properties - Linker. All .lib file name to the list of additional dependencies. Ensure that .lib file can be found by providing full path or adding .lib directory to the linker search list.

<strong>Edit.</strong> Linker - General - Additional Library Directories. Here you can add .lib file path for the current project. There is also global directories list in Tools - Options - Project and Solutions - VC++ Directories - Library files.

Answer2:

Solution:

In the C++ project:

<ul><li>In C/C++ -> General add the directory containing the headers to include</li> <li>In Linker -> General Add the directory of the .lib file</li> <li>In Linker -> Input -> Additional Dependencies add the name of the .lib file</li> </ul>

And finally, in my C++ code, I didn't need to redefine the C function. The following shows my code:

.cpp: // This is the main DLL file.

#include "stdafx.h" #include "FinLib.h" namespace FinLib { void Class1::CalcImpliedVolatility() { double volat = 0.0; int _status = GtoCalcImpliedVolatility2(//defined in all.h(dll) 0.098328, //premium 'P', //optionType 0.950000, //underPrice 1.050000, //strike 0.900000, //yearsToExpiry 0.95, //yearsToPayment 0.01, //volGuess 0.02, //discountRate 0.03, //dividendRate 0.04,//growthRate &volat); Console::WriteLine(volat); } }

.h:

// FinLib.h #pragma once #include "all.hpp" //this line I will move to stdafx.h after (includes all the headers) using namespace System; namespace FinLib { public ref class Class1 { public: void CalcImpliedVolatility(); }; }

Recommend

  • included header for static library giving dllimport error
  • Unresolved ___stdio_common_vsprintf_s, what library has this?
  • Passing a path to Labview DLL in Python
  • MASM: Accessing to global C variable from assembly
  • How to create a texture from a bitmap file?
  • angular and jQuery conflict with $
  • Firefox 26 C++ Addon XPCOM dependency error
  • C++/CLR managed unit test has linker errors
  • Linking errors when upgrading a VS2003 solution in C++
  • Nested struct unmarshalling
  • C++ syntax: void CLASS functionName()?
  • Unresolved Externals in C++: Visual C++ mangles method signature differently from mangled method in
  • Visual Studio 2010 library linking errors
  • Trying to SVN Checkout Open Source BitSharp Library
  • VB6 declaration for C++ function gives “Bad DLL calling convention”
  • error LNK2019: unresolved external symbol
  • Externals defined in webpack.config still getting error module not found
  • Link error when using lex and yacc output in Visual Studio
  • LNK 2019 - Unresolved External Symbol
  • Android Launcher application compilation on Eclipse
  • Linker error when function definition is in cpp file
  • How do I make a static library Release/Debug independent in VS2010 Express?
  • Linking To V8 Snapshot
  • including Python.h in C++ file CDT
  • How to revert to previous XCode version?
  • Excel's Macro-Recorder usage
  • How to use carriage return with multiple line?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Where to put my custom functions in Wordpress?
  • Build own AppleScript numerical error handling
  • Weird JavaScript statement, what does it mean?
  • Websockets service method fails during R startup
  • Google cloud sdk not working when python points python3
  • Numpy divide by zero. Why?
  • Is there a mandatory requirement to switch app.yaml?
  • Hits per day in Google Big Query
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • need help with bizarre java.net.HttpURLConnection behavior
  • Linking SubReports Without LinkChild/LinkMaster
  • XCode 8, some methods disappeared ? ex: layoutAttributesClass() -> AnyClass