从C++/CLI调用使用VS 2005编译的本机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我有以下几点:从C++/CLI调用使用VS 2005编译的本机C Visual Studio 2010 - 无法打开.lib文件...

// 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 *)" (?[email protected]@@[email protected]) referenced in function "public: int __clrcall testWrapper::MyNamesSplitterClass::TestSomething(void)" ([email protected]@[email protected]@$$FQ$AAMHXZ) 

Error 2 error LNK2019: unresolved external symbol "int __cdecl testWrapper::GtoCalcImpliedVolatility2(double,int,double,double,double,double,double,double,double,double,double *)" ([email protected]@@[email protected]) referenced in function "public: int __clrcall testWrapper::MyNamesSplitterClass::TestSomething(void)" ([email protected]@[email protected]@$$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

解决方案:

在C++项目:

  • 在C/C++ - >常规添加包含头文件的目录,包括
  • 在链接器 - >常规添加的目录。 lib文件
  • 在链接器 - >输入 - >其他依赖项中添加名称。lib文件

最后,在我的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(); 

    }; 
} 

凡GtoCalcImpliedVolatility2 C函数是实施?如果在单独的.c或.cpp文件中,请将此文件添加到项目中。如果它被放置在Dll中,则将.lib文件添加到项目链接器依赖项中。

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

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

+0

功能是在DLL中。我在Project-> Properties-> config props-> linker-> input-> additional dependencies中添加了.lib文件的名称。 我应该把alib的完整路径还是只是它的名字?如果我只输入名称,VS在哪里查找文件? – nche