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

Hi, I want to call functions from C dll in C ++ / CLI. C functions are declared extern. I followed this dll binding guide: http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/84deabaa-ae82-47cc-aac0-592f5a8dfa22 and then in my C ++ / CLI dll I have 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 should only give signatures in .h, and then write the function code in .cpp, but for testing I write it there. 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 tried to find them, but I can not find a lot of information about them, except that they are due to poor communication ...

UPDATE: Project Management ...

So, for my C ++ / cli project, I wrote .hpp as follows, which includes my 880+ headers:

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

following: 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 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 shell, I turn on my all.hpp, which includes all the other headers ... In doing so, I still get errors.

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

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

I have not indicated the correct path for lib ... Now I have parsed this and I get error LNK1104 saying that it cannot open my .lib

0
source share
2 answers

Decision:

In a C ++ project:

  • In C / C ++ -> General add a directory containing headers to enable
  • In Linker -> General Add the directory of the .lib file
  • In Linker -> Input -> Additional Dependencies add the name of the .lib file

And finally, in my C ++ code, I did not need to override the C function. My code is shown below:

.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(); }; } 
0
source

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

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

Edit Linker - General - Additional library catalogs. Here you can add a .lib file for the current project. There is also a list of global directories in "Tools - Options - Project and Solutions" - "VC ++ Directories" - Library Files.

+1
source

All Articles