记录一次从MinGw转到MSVC编译器的错误经历

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/czyt1988/article/details/84074683

MinGW和MSVC兼容度并不那么好,由于中文的问题,sa一直使用的是MinGW来进行编译,但说实话,在windows上MinGW编译出来的程序在体积和速度上和MSVC还是有点差距的,因此,sa最终版打算使用msvc编译器。
于是,前几天用Qt5.9 MSVC2015版进行了一下编译结果发现了许多问题,有语法的问题,也有非常讨厌的链接问题。

下面是一些记录:

  • MinGW比MSVC宽松很多,类似于需要返回值得函数,如果不返回值MinGW不会产生错误,只会进行警告,MSVC则会直接报错

  • 编写库时,模板类是不能导出的,我在编写时并没留意这点,模板类也进行了导出,MSVC会报错,MinGW毫无提示直接忽略,这点MSVC貌似处理更合理

然后遇到了该死的链接问题,首先遇到了莫名其妙的重定义问题error LNK2005

  • 在链接上原本能通过MinGW成功链接,但在MSVC却出现了error LNK2005错误,sa使用的绘图引擎是qwt,在signACommonUI库中定义了如下函数:

完整代码见:master/src/signACommonUI/Chart2D/SAFigureOptCommands.h 的SAFigureReplaceSeriesDataInIndexsCommand

template<typename T,typename TQwtSeries>
class SAFigureReplaceSeriesDataInIndexsCommand : public SAFigureOptCommand
{
public:
    SAFigureReplaceSeriesDataInIndexsCommand(SAChart2D* chart
                                        ,QwtSeriesStore<T> *curve
                                        ,const QString &cmdName
                                        ,const QVector<int>& inRangIndexs
                                        ,const QVector<T>& inRangNewData
                                        , QUndoCommand *parent = Q_NULLPTR);
    SAFigureReplaceSeriesDataInIndexsCommand(SAChart2D* chart
                                        ,QwtSeriesStore<T> *curve
                                        ,const QString &cmdName
                                        ,const QVector<int>& inRangIndexs
                                        ,const QVector<T>& inRangOldData
                                        ,const QVector<T>& inRangNewData
                                        , QUndoCommand *parent = Q_NULLPTR);

    virtual void redo();
    virtual void undo();
private:
    QVector<T> m_inRangOldData;
    QVector<int> m_inRangIndexs;
    QVector<T> m_inRangNewData;
    QwtSeriesStore<T> *m_curve;
};
...

///
/// \brief 序列数据QPointF的替换
///
class SA_COMMON_UI_EXPORT SAFigureReplaceXYSeriesDataInIndexsCommand
        : public SAFigureReplaceSeriesDataInIndexsCommand<QPointF,QwtPointSeriesData>
{
public:
    using SAFigureReplaceSeriesDataInIndexsCommand::SAFigureReplaceSeriesDataInIndexsCommand;
};

///
/// \brief 序列数据QwtPlotMultiBarChart的替换
///
class SA_COMMON_UI_EXPORT SAFigureReplaceMultiBarSeriesDataInIndexsCommand
        : public SAFigureReplaceSeriesDataInIndexsCommand<QwtSetSample,QwtSetSeriesData>
{
public:
  using SAFigureReplaceSeriesDataInIndexsCommand::SAFigureReplaceSeriesDataInIndexsCommand;
};

链接错误提示:

qwtd.lib(qwtd.dll) : error LNK2005: "public: void __thiscall QwtSeriesStore<class QwtSetSample>::setData(class QwtSeriesData<class QwtSetSample> *)" (?setData@?$QwtSeriesStore@VQwtSetSample@@@@QAEXPAV?$QwtSeriesData@VQwtSetSample@@@@@Z) 已经在 SAFigureOptCommands.obj 中定义
....

这里所有调用QwtSeriesStoresetData`处都有链接错误提示

看提示就是重定义了QwtSeriesStore<class QwtSetSample>::setData函数,这个函数在模板类的redo()函数里调用:

template<typename T,typename TQwtSeries>
void SAFigureReplaceSeriesDataCommand<T,TQwtSeries>::redo()
{
    ......
    m_curve->setData(new TQwtSeries(curveDatas));
}

完整代码见:SAFigureOptCommands.h 的SAFigureReplaceSeriesDataInIndexsCommand

说明编译器认为我又定义过QwtSeriesStore<class QwtSetSample>::setData函数,看一下信息,说的是qwtd.lib中的QwtSeriesStoresetData函数在我写的这个类之后编译的,也就是说,编译器先编译了我写的这个类,再去看qwtd.lib发现有重复符号。

解决方法就是引用qwtd.lib含有这些实例化模板的类的头文件,让编译器知道,这些模板的实例化已经在qwtd.lib中存在了。

终于是把signACommonUI库编译完成,接下来到主程序signA,结果又有链接错误:LNK2019LNK2001

mainwindow.obj:-1: error: LNK2019: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall SAWaitCursor::SAWaitCursor(void)" (__imp_??0SAWaitCursor@@QAE@XZ),该符号在函数 "private: void __thiscall MainWindow::initPlugin(void)" (?initPlugin@MainWindow@@AAEXXZ) 中被引用
SAChartDatasViewWidget.obj:-1: error: LNK2001: 无法解析的外部符号 "__declspec(dllimport) public: __thiscall SAWaitCursor::SAWaitCursor(void)" (__imp_??0SAWaitCursor@@QAE@XZ)

一看就是编译器没找到SAWaitCursor,具体代码见:master/src/signACommonUI/SAWaitCursor.hmaster/src/signACommonUI/SAWaitCursor.cpp

在你看到之前其实只有一个头文件:


class SA_COMMON_UI_EXPORT SAWaitCursor
{
public:
    SAWaitCursor()
    {
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    }
    ~SAWaitCursor()
    {
        QApplication::restoreOverrideCursor();
    }
    void setWaitCursor()
    {
        QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
    }
    void setCursor(const QCursor cur = QCursor(Qt::WaitCursor))
    {
        QApplication::setOverrideCursor(cur);
    }
    void release()
    {
        QApplication::restoreOverrideCursor();
    }
};


#ifndef SA_SET_AUTO_WAIT_CURSOR
#define SA_SET_AUTO_WAIT_CURSOR() \
    SAWaitCursor __sa__wait__cursor;\
    Q_UNUSED(__sa__wait__cursor);
#endif

略一思索,可能是没有cpp导致的问题,于是新建对应的cpp文件,并把在头文件实现的内容写到cpp中,编译出来的lib和原来的大小居然也不一样,这样顺利完成了signA的编译

猜你喜欢

转载自blog.csdn.net/czyt1988/article/details/84074683
今日推荐