MT4 DLL / TA-LIB链接器错误

时间:2015-10-09 21:02:41

标签: c++ c++11 dll linker-errors mt4

这是我第二次尝试C ++程序,所以我还在学习。

我正在尝试创建一个DLL,以便与使用Visual Studio 2013社区的ta-lib技术分析库的Metatrader 4一起使用。

但是,当我构建解决方案时,我收到以下链接器错误,我不知道如何修复它们。

Error   4   error LNK2005: "private: __thiscall type_info::type_info(class type_info const &)" (??0type_info@@AAE@ABV0@@Z) already defined in libcmt.lib(typinfo.obj)   C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\iDEMA\MSVCRT.lib(ti_inst.obj)  iDEMA
Error   5   error LNK2005: "private: class type_info & __thiscall type_info::operator=(class type_info const &)" (??4type_info@@AAEAAV0@ABV0@@Z) already defined in libcmt.lib(typinfo.obj) C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\iDEMA\MSVCRT.lib(ti_inst.obj)  iDEMA
Error   2   error LNK2005: _free already defined in libcmt.lib(free.obj)    C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\iDEMA\MSVCRT.lib(MSVCR120.dll) iDEMA
Error   3   error LNK2005: _malloc already defined in libcmt.lib(malloc.obj)    C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\iDEMA\MSVCRT.lib(MSVCR120.dll) iDEMA
Warning 1   warning LNK4098: defaultlib 'MSVCRT' conflicts with use of other libs; use /NODEFAULTLIB:library    C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\iDEMA\LINK iDEMA
Error   7   error LNK1169: one or more multiply defined symbols found   C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\Release\iDEMA.dll  iDEMA
Error   6   error LNK2005: _DllMain@12 already defined in uafxcw.lib(dllmodul.obj)  C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\iDEMA\* CIL library *(* CIL module *)  iDEMA

我的头文件是这个

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the IDEMA_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// IDEMA_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.

#ifdef IDEMA_EXPORTS
#define IDEMA_API __declspec(dllexport)
#else
#define IDEMA_API __declspec(dllimport)
#endif

#pragma pack(push,1)
struct RateInfo
{
    __int64           ctm;
    double            open;
    double            low;
    double            high;
    double            close;
    unsigned __int64  vol_tick;
    int               spread;
    unsigned __int64  vol_real;
};
#pragma pack(pop)

enum ENUM_PRICE
{
    PRICE_OPEN,
    PRICE_LOW,
    PRICE_HIGH,
    PRICE_CLOSE
};

// This class is exported from the iDEMA.dll
class IDEMA_API CiDEMA {
public:
    CiDEMA(void);
    double iDEMA(RateInfo, int, int, int, ENUM_PRICE);
};

extern IDEMA_API int niDEMA;

IDEMA_API int fniDEMA(void);

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include "C:\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_libc.h"

#pragma once

#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"       // main symbols

我的代码就是这个

// iDEMA.cpp : Defines the exported functions for the DLL application.
//

#include "stdafx.h"
#include "iDEMA.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

#define WIN32_LEAN_AND_MEAN
#define MT4_EXPFUNC __declspec(dllexport)

using namespace std;

BOOL APIENTRY DllMain(HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved)
{
    //---
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    //---
    return(TRUE);
}

MT4_EXPFUNC double __stdcall iDEMA(const RateInfo* rates, const int rates_total, const int period, const int shift, const ENUM_PRICE applied_price)
{
    //---
    if (rates == NULL)
    {
        printf("iDEMA: NULL array\n");
        return(0.0);
    }
    //---
    if (rates_total<0 || rates_total<2 * period)
    {
        printf("iDEMA: wrong rates_total number (%d)\n", rates_total);
        return(0.0);
    }
    //---
    if (period<2 || period>100000)
    {
        printf("iDEMA: wrong period number (%d)\n", period);
        return(0.0);
    }
    //---
    if (shift<0 || shift >= rates_total)
    {
        printf("iDEMA: wrong shift number (%d)\n", shift);
        return(0.0);
    }
    //---
    if (applied_price<0 || applied_price>3)
    {
        printf("iDEMA: wrong applied price (%d)\n", applied_price);
        return(0.0);
    }
    //---

    TA_RetCode retCode;
    retCode = TA_Initialize();

    if (retCode != TA_SUCCESS)
    {
        printf("TA_LIB initialistion failed (%d)!\n", retCode);
        return(0.0);
    }

    vector<TA_Real> dataArray;
    const TA_Real* dataArray_c_pointer = dataArray.data();

    // DEMA = ( 2 * EMA(n)) - (EMA(EMA(n)) ), where n= period

    for (int nitem = rates_total - 1 - shift - (2 * period); nitem < rates_total - 1 - shift; nitem++)
    {
        TA_Real value = 0.0;

        switch (applied_price)
        {
        case PRICE_OPEN: value = rates[nitem].open; break;
        case PRICE_LOW: value = rates[nitem].low; break;
        case PRICE_HIGH: value = rates[nitem].high; break;
        case PRICE_CLOSE: value = rates[nitem].close; break;
        }

        dataArray[nitem] = value;
    }

    TA_Integer outBegin = 0, outElements = 0;

    int beginIndx, endIndx;
    beginIndx = endIndx = dataArray.size() - 1;
    int array_size = endIndx - beginIndx + 1;
    cout << "beginIndx = " << to_string(beginIndx) << endl << "endIndx = " << to_string(endIndx) << endl << "array_size = " << to_string(array_size) << endl;

    vector<TA_Real> outDema(array_size, 0.0);
    TA_Real* outDema_c_pointer = outDema.data();

    retCode = TA_DEMA(beginIndx, endIndx, dataArray_c_pointer, period, &outBegin, &outElements, outDema_c_pointer);

    double value = 0.0;

    if (retCode == TA_SUCCESS)
    {
        cout << "outBegin = " << outBegin << " outElements = " << outElements << endl;
        int  lastElement = outElements - 1;;
        cout << "outDema.at(" << to_string(lastElement) << ") = " << to_string(outDema.at(lastElement)) << endl;

        delete dataArray_c_pointer;
        delete outDema_c_pointer;

        value = outDema.at(lastElement);
    }

    retCode = TA_Shutdown();

    return value;
}

我使用nuGet安装了库。

请帮忙编译这个DLL。

提前致谢。

更新10/10/15

我删除了包nuGet并使用了来自不同目录的静态链接。

我现在收到这些链接器错误:

Error   2   error LNK1169: one or more multiply defined symbols found   C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\Release\iDEMA.dll  iDEMA
Error   1   error LNK2005: _DllMain@12 already defined in mfcs120u.lib(dllmodul.obj)    C:\Users\Documents\Visual Studio 2013\Projects\iDEMA\iDEMA\* CIL library *(* CIL module *)  iDEMA

仍然不明白如何修复它们!

任何帮助将不胜感激。

感谢。

2 个答案:

答案 0 :(得分:2)

尝试像这样包装标题:

HttpRequest

如果VS抱怨extern关键字,请转到Project Properties&gt; C / C ++&gt;高级&gt;编译为并检查编译为C ++。

对于更新的答案,看起来您的编译器看到了相同符号的两个定义。

尝试使用

extern "C"
{
   #include "stdafx.h"
   #include "iDEMA.h"
}

他们在不同的地方看。如果它没有帮助,试试extern&#34; C&#34;与您的旧项目(更新10/10/15之前)。

也许您已经在第一次尝试中添加了一些dll或lib到您的PATH,并且在&#34;更新10/10/15&#34;之后,您再也不需要它们了。

还读这个 answer 还有这个 MSDN Building Visual C++ page

编辑: 如果可能,请创建一个新项目。也许,在你的尝试中,你搞砸了什么。

答案 1 :(得分:1)

管理以在修改标头和源文件后将其链接。这是最终结果:

// The following ifdef block is the standard way of creating macros which make exporting 
// from a DLL simpler. All files within this DLL are compiled with the IDEMA_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see 
// IDEMA_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.

#define WIN32_LEAN_AND_MEAN
#define MT4_EXPFUNC __declspec(dllexport)

#pragma pack(push,1)
struct RateInfo
{
    __int64           ctm;
    double            open;
    double            low;
    double            high;
    double            close;
    unsigned __int64  vol_tick;
    int               spread;
    unsigned __int64  vol_real;
};

enum ENUM_PRICE
{
    PRICE_OPEN,
    PRICE_LOW,
    PRICE_HIGH,
    PRICE_CLOSE
};
#pragma pack(pop)

// This class is exported from the iDEMA.dll
class MT4_EXPFUNC CiDEMA {
public:
    CiDEMA(void);
    double iDEMA(RateInfo, int, int, int, ENUM_PRICE);
};

extern MT4_EXPFUNC int niDEMA;

MT4_EXPFUNC int fniDEMA(void);

#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <string>
#include "C:\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_libc.h"

#pragma once

#ifndef __AFXWIN_H__
#error "include 'stdafx.h' before including this file for PCH"
#endif

#include "resource.h"       // main symbols

源文件:

// iDEMA.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "iDEMA.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

using namespace std;

MT4_EXPFUNC double __stdcall iDEMA(const RateInfo* rates, const int rates_total, const int period, const int shift, const ENUM_PRICE applied_price)
{
    //---
    if (rates == NULL)
    {
        printf("iDEMA: NULL array\n");
        return(0.0);
    }
    //---
    if (rates_total<0 || rates_total<2 * period)
    {
        printf("iDEMA: wrong rates_total number (%d)\n", rates_total);
        return(0.0);
    }
    //---
    if (period<2 || period>100000)
    {
        printf("iDEMA: wrong period number (%d)\n", period);
        return(0.0);
    }
    //---
    if (shift<0 || shift >= rates_total)
    {
        printf("iDEMA: wrong shift number (%d)\n", shift);
        return(0.0);
    }
    //---
    if (applied_price<0 || applied_price>3)
    {
        printf("iDEMA: wrong applied price (%d)\n", applied_price);
        return(0.0);
    }
    //---

    TA_RetCode retCode;
    retCode = TA_Initialize();

    if (retCode != TA_SUCCESS)
    {
        printf("TA_LIB initialistion failed (%d)!\n", retCode);
        return(0.0);
    }

    vector<TA_Real> dataArray;
    const TA_Real* dataArray_c_pointer = dataArray.data();

    // DEMA = ( 2 * EMA(n)) - (EMA(EMA(n)) ), where n= period

    for (int nitem = rates_total - 1 - shift - (2 * period); nitem < rates_total - 1 - shift; nitem++)
    {
        TA_Real value = 0.0;

        switch (applied_price)
        {
        case PRICE_OPEN: value = rates[nitem].open; break;
        case PRICE_LOW: value = rates[nitem].low; break;
        case PRICE_HIGH: value = rates[nitem].high; break;
        case PRICE_CLOSE: value = rates[nitem].close; break;
        }

        dataArray[nitem] = value;
    }

    TA_Integer outBegin = 0, outElements = 0;

    int beginIndx, endIndx;
    beginIndx = endIndx = dataArray.size() - 1;
    int array_size = endIndx - beginIndx + 1;
    cout << "beginIndx = " << to_string(beginIndx) << endl << "endIndx = " << to_string(endIndx) << endl << "array_size = " << to_string(array_size) << endl;

    vector<TA_Real> outDema(array_size, 0.0);
    TA_Real* outDema_c_pointer = outDema.data();

    retCode = TA_DEMA(beginIndx, endIndx, dataArray_c_pointer, period, &outBegin, &outElements, outDema_c_pointer);

    double value = 0.0;

    if (retCode == TA_SUCCESS)
    {
        cout << "outBegin = " << outBegin << " outElements = " << outElements << endl;
        int  lastElement = outElements - 1;;
        cout << "outDema.at(" << to_string(lastElement) << ") = " << to_string(outDema.at(lastElement)) << endl;

        delete dataArray_c_pointer;
        delete outDema_c_pointer;

        value = outDema.at(lastElement);
    }

    retCode = TA_Shutdown();

    return value;
}