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: http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/84deabaa-ae82-47cc-aac0-592f5a8dfa22 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开发者_如何学JAVA 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
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.
Edit. 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.
Solution:
In the C++ project:
- In C/C++ -> General add the directory containing the headers to include
- 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 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();
};
}
精彩评论