Appeler un C natif compilé avec VS 2005 à partir de C ++ / CLI Visual Studio 2010 - Impossible d'ouvrir le fichier .lib…

StackOverflow https://stackoverflow.com/questions/7428789

Question

Salut je veux appeler les fonctions d'une dll C à C ++ / CLI. Les fonctions C sont déclarées externes. J'ai suivi ce tutoriel pour lier la DLL: http://social.msdn.microsoft.com/forums/en/vsexpressvc/thread/84deaaa-ae82-47cc-aac0-592f5a8dfa22 Et puis dans ma DLL C ++ / CLI, j'ai ce qui suit:

    // 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);
      }
    };
}

Je sais que je suis censé ne donner des signatures que dans le .h, puis écrire du code de fonction dans .cpp mais pour les tests, je l'écris au même endroit. Ce sont les erreurs que j'obtiens:

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)

J'ai essayé de les chercher mais je ne trouve pas beaucoup d'informations à leur sujet en dehors du fait qu'ils sont dus à une mauvaise liaison ...

Mise à jour: Organisation des projets ...

Donc, pour mon projet C ++ / CLI, j'ai écrit un .hpp comme suit qui inclut mes en-têtes 880+:

extern "C" {

#include "accrued.h"
...
#include "gtobf.h" // this contains GtoCalcImpliedVolatility2
...
#include "../includep/zcsmthp.h"

}

Ce qui suit est 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 */

Ensuite, dans mon emballage C ++ / CLI, j'inclus mon all.hpp qui inclut tous les autres en-têtes ... Avec tout cela, je reçois toujours les erreurs.

Je suis sûr que la fonction est exportée car j'ai utilisé la DLL de C # en utilisant p / invoke ...

!!!!!!!!!!!!!!! ÉDITER:

Je n'avais pas indiqué le bon chemin pour la lib ... Maintenant, je l'ai réglé et j'obtiens une erreur LNK1104 disant qu'il ne peut pas ouvrir mon .lib

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top