从 C++CLI Visual studio 2010 调用使用 VS 2005 编译的本机 C - 无法打开 .lib 文件...
您好,我想从 C dll 到 C++/CLI 调用函数。 C 函数被声明为 extern。我按照本教程链接了 dll: http://social.msdn.microsoft.com/Forums/en/Vsexpressvc/thread/84deabaa-ae82-47cc-aac0-592f5a8dfa22 然后在我的 C++/CLI dll 中,我有以下内容:
// 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);
}
};
}
我知道我应该只在 .h 中给出签名,然后在 .cpp 中编写函数代码,但为了测试,我将其编写在同一个地方。 这些是我得到的错误:
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)
我尝试查找它们,但除了它们是由于错误链接而导致的事实之外,找不到有关它们的太多信息...
更新:项目的组织...
所以对于我的 c++/cli项目我编写了一个 .hpp,如下所示,其中包括我的 880+ 标头:
extern "C" {
#include "accrued.h"
...
#include "gtobf.h" // this contains GtoCalcImpliedVolatility2
...
#include "../includep/zcsmthp.h"
}
以下是 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 */
然后在我的 c++/cli 包装器中,我包括我的 all.hpp,其中包括所有其他标头... 尽管如此,我仍然收到错误。
我确信该函数已导出,因为我已经使用 P/Invoke 从 C# 中使用了 dll...
!!!!!!!!!!!!!!!编辑:
我没有指出该库的正确路径...现在我已经解决了这个问题,并且收到 LNK1104 错误,指出它无法打开我的 .lib
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 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
GtoCalcImpliedVolatility2 C 函数在哪里实现?如果位于单独的 .c 或 .cpp 文件中,请将此文件添加到项目中。如果放置在 Dll 中,请将 .lib 文件添加到项目链接器依赖项中。
一些细节:打开项目 - 属性 - 配置属性 - 链接器。所有 .lib 文件名都添加到附加依赖项列表中。确保可以通过提供完整路径或将 .lib 目录添加到链接器搜索列表来找到 .lib 文件。
编辑。
链接器 - 常规 - 附加库目录。在这里您可以添加当前项目的.lib文件路径。工具 - 选项 - 项目和解决方案 - VC++ 目录 - 库文件中也有全局目录列表。
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.
解决方案:
在C++项目中:
最后,在我的 C++ 代码中,我不需要重新定义 C 函数。下面显示了我的代码:
.cpp:
// 这是主 DLL 文件。
.h:
Solution:
In the C++ project:
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.
.h: