c++ - MT4 DLL/TA-LIB 链接器错误

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

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

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

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

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吗。

提前致谢。

15 年 10 月 10 日更新

我删除了包 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

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

我们将不胜感激。

谢谢。

最佳答案

尝试像这样包装您的 header :

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

如果 VS 提示 extern 关键字,请转到“项目属性”>“C/C++”>“高级”>“编译为”并选中“编译为 C++”。

对于更新后的答案,您的编译器似乎看到了同一符号的两个定义。

尝试使用

#include <stdafx.h>
#include <iDEMA.h>

他们在不同的地方看。如果它没有帮助,请在您的旧项目中尝试使用 extern“C”(在 2015 年 10 月 10 日更新之前)。

也许您在第一次尝试时向您的 PATH 添加了一些 dll 或 lib,在“更新 10/10/15”之后,您不再需要它们了。

另请阅读 answer 和这个 MSDN Building Visual C++ page .

编辑: 如果可能,创建一个新项目。也许,在您的尝试中,您搞砸了一些事情。

关于c++ - MT4 DLL/TA-LIB 链接器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33047135/

相关文章:

c++ - 确保使用双重比较和交换指令,用于无锁堆栈?

c++ - 无效 C 风格强制转换后的方法调用

c++ - 将 shared_ptr 转换为不同命名空间中相同类型的 shared_ptr

windows - 如何 Hook Windows 中的所有新进程

c++ - 如何将着色器嵌入到 UWP-DLL 库中?

c++ - 将一个 DeviceContext 的内容复制到另一个 DeviceContext

c++ - 将所有工作汇总到一个循环中还是将其分成多个循环更好?

c++ - 分配指针的方式有什么区别?

c++ - 我的代码是否等同于 Boost.Asio 教程中给出的代码?

c++ - 没有 DllRegisterServer 等的 COM dll