c/c++语言开发共享从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 …

!!!!!!!!!!!!!!! 编辑:

我没有指出lib的正确路径……现在我已经对它进行了排序,我收到LNK1104错误,说它无法打开我的.lib

    哪里实现了GtoCalcImpliedVolatility2 C函数? 如果在单独的.c或.cpp文件中,请将此文件添加到项目中。 如果它放在Dll中,请将.lib文件添加到项目链接器依赖项中。

    一些细节:打开项目 – 属性 – 配置属性 – 链接器。 所有.lib文件名都列为其他依赖项列表。 确保通过提供完整路径或将.lib目录添加到链接器搜索列表来找到.lib文件。

    编辑。 链接器 – 通用 – 附加库目录。 在这里,您可以为当前项目添加.lib文件路径。 工具 – 选项 – 项目和解决方案 – VC ++目录 – 库文件中还有全局目录列表。

    解:

    在C ++项目中:

    最后,在我的C ++代码中,我不需要重新定义C函数。 以下显示了我的代码:

    .cpp://这是主DLL文件。

     #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(); }; } 

      以上就是c/c++开发分享从C ++ / CLI Visual Studio 2010调用使用VS 2005编译的本机C – 无法打开.lib文件…相关内容,想了解更多C/C++开发(异常处理)及C/C++游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/c-cdevelopment/523180.html

      (0)
      上一篇 2020年12月11日
      下一篇 2020年12月11日

      精彩推荐