调用使用VS 2005从C ++ / CLI编译的本机C Visual Studio 2010 - 无法打开.lib文件... [英] Calling a native C compiled with VS 2005 from C++/CLI Visual studio 2010 - Cannot open .lib file...

查看:507
本文介绍了调用使用VS 2005从C ++ / CLI编译的本机C Visual Studio 2010 - 无法打开.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 {
externCint 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()
{
双体积
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中写入功能代码,但为了测试的缘故,我在同一个地方写它。
这些是我得到的错误:

 错误3错误LNK1120:2未解决的外部b 
$ b错误1错误LNK2028:未解析的标记(0A000006)int __cdecl testWrapper :: GtoCalcImpliedVolatility2(double,int,double,double,double,double,double,double,double,double,double *) ?__clrcall testWrapper :: MyNamesSplitterClass :: TestSomething(void)(?TestSomething @ MyNamesSplitterClass @ testWrapper @@ $$ FQ $ AAMHXZ)

错误2错误LNK2019:未解析的外部符号int __cdecl testWrapper :: GtoCalcImpliedVolatility2(double,int,double,double,double,double,double,double,double,double,double *)(?GtoCalcImpliedVolatility2 @ testWrapper @ @ $$ $$$ $ AAMHXZ)

我已经尝试查找他们,但是找不到关于他们的很多信息,除了他们是由于错误的链接...



更新:组织项目...



所以对于我的c ++ / cli项目,我写了一个.hpp如下所示,包括我的880+标题:

  externC{

#include .h
...
#includegtobf.h//包含GtoCalcImpliedVolatility2
...
#include../includep/zcsmthp.h

}

以下是gtobf.h:

  #ifndef ALIB_GTOBF_H 
#define ALIB_GTOBF_H

#includecgeneral.h
#include optprop.h/ * TOptionProperties * /
#includecmemory.h/ *免费宏* /
#includertnewton.h/ * TObjectFunc * /


#ifdef __cplusplus
externC
{
#endif

/ * f
*计算价格隐含的波动率给定选项。
* /
//#define GTO_EXPORT(type)__declspec(dllexport)type
GTO_EXPORT(int)GtoCalcImpliedVolatility2(
double premium,/ * b $ b int optionType,/ *(I)GtoOPTION_PUT / GtoOPTION_CALL * /
double underPrice,/ *(I)underlyer price * /
双击,
double yearsToExpiry,/ *(I)到期权到期的期限* /
double yearsToPayment,/ *(I)期限到期的年份* /
double volGuess,/ *猜想(.06 6%)* /
double discountRate,/ *(I)贴现率(年化)* /
double dividendRate,/ *(I) * /
double growthRate,/ *(I)增长率(年化合物)* /
double * impliedVol); / *(O)隐含波动率* /

//...other functions

#ifdef __cplusplus
}
#endif

#endif / * ALIB_GTOBF_H * /

然后在我的c ++ / cli包装中, all.hpp包括所有其他头部...
所有这一切,我仍然得到错误。



我相信函数导出,因为我已经使用来自C#的DLL使用P /调用...



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



我没有指出lib的正确路径...现在我已经排序了,我得到一个LNK1104错误,它无法打开我.lib

解决方案

解决方案:



/ p>


  • 在C / C ++ - >常规添加包含头文件的目录

  • > General添加.lib文件的目录

  • 在链接器 - >输入 - >附加依赖关系中添加.lib文件的名称



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



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

  #includestdafx.h

#includeFinLib.h

命名空间FinLib {

void Class1 :: CalcImpliedVolatility()
{
double volat = 0.0;
int _status = GtoCalcImpliedVolatility2(// 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
#includeall.hpp//这行我将移动到stdafx.h after(包括所有的头文件)
using namespace System;

命名空间FinLib {

public ref class Class1
{
public:

void CalcImpliedVolatility

};
}


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

解决方案

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

    };
}

这篇关于调用使用VS 2005从C ++ / CLI编译的本机C Visual Studio 2010 - 无法打开.lib文件...的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆