Qt QTimeSpan错误LNK2019

时间:2013-06-21 12:01:50

标签: c++ qt

我使用的是Windows 7,QCreator,Qt 5.0,我的电脑是x64。

使用QTimeSpan时出现以下错误:

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) public: __cdecl QTimeSpan::~QTimeSpan(void)" (__imp_??1QTimeSpan@@QEAA@XZ) referenced in function "private: void __cdecl MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked@MainWindow@@AEAAXXZ)

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) class QTimeSpan __cdecl operator*(class QTimeSpan const &,int)" (__imp_??D@YA?AVQTimeSpan@@AEBV0@H@Z) referenced in function "class QTimeSpan __cdecl operator*(int,class QTimeSpan const &)" (??D@YA?AVQTimeSpan@@HAEBV0@@Z)

mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) class QTimeSpan __cdecl operator-(class QTime const &,class QTime const &)" (__imp_??G@YA?AVQTimeSpan@@AEBVQTime@@0@Z) referenced in function "private: void __cdecl MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked@MainWindow@@AEAAXXZ)


mainwindow.obj:-1: error: LNK2019: unresolved external symbol "__declspec(dllimport) class QTime __cdecl operator+(class QTime const &,class QTimeSpan const &)" (__imp_??H@YA?AVQTime@@AEBV0@AEBVQTimeSpan@@@Z) referenced in function "private: void __cdecl MainWindow::on_pushButton_clicked(void)" (?on_pushButton_clicked@MainWindow@@AEAAXXZ)

debug\Midpoint3_1.exe:-1: error: LNK1120: 4 unresolved externals

我尝试将Qt / libs目录添加到计算机的“环境变量”中的路径但是失败了。

这是源代码: //Midpoint3_1.pro

#-------------------------------------------------
#
# Project created by QtCreator 2013-07-01T03:58:17
#
#-------------------------------------------------

QT       += core gui
QT += widgets

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Midpoint3_1
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp\


HEADERS  += mainwindow.h \
    ../../../../../../../../Qt/Qt5.0.2/5.0.2/msvc2012_64/include/QTimeSpan/qtimespan.h

FORMS    += \
    mainwindow.ui

// mainwindow.h

#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>

namespace Ui {
class MainWindow;
}

class MainWindow : public QMainWindow
{
    Q_OBJECT

public:
    explicit MainWindow(QWidget *parent = 0);
    ~MainWindow();

private slots:
    void on_pushButton_clicked();

private:
    Ui::MainWindow *ui;
};

#endif // MAINWINDOW_H

// MainWindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QtGui>
#include <QtCore>
#include "QTimeSpan/qtimespan.h"
#include <QTime>


MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->horizontalSlider,SIGNAL(valueChanged(int))
           ,ui->percentage,SLOT(setValue(int)));
}

void MainWindow::on_pushButton_clicked()
{
    QTime vtime1 = ui->time1->time();
    QTime vtime2 = ui->time2->time();
    QTimeSpan vspan = vtime2 - vtime1;
    int vpercentage = ui->percentage->value();
    QTime vtime3 = vtime1 + (vpercentage/100) * vspan;
    //ui->time3->setText(QString );
}


MainWindow::~MainWindow()
{
    delete ui;
}

// main.h

#include "mainwindow.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
}

还有一个mainwindow.ui,这是源代码:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>284</width>
    <height>259</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Time Midpoint Calculator</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <widget class="QWidget" name="layoutWidget">
    <property name="geometry">
     <rect>
      <x>20</x>
      <y>10</y>
      <width>241</width>
      <height>191</height>
     </rect>
    </property>
    <layout class="QVBoxLayout" name="verticalLayout_2">
     <item>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout">
         <item>
          <widget class="QLabel" name="label">
           <property name="text">
            <string>Time 1</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QTimeEdit" name="time1"/>
         </item>
        </layout>
       </item>
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_2">
         <item>
          <widget class="QLabel" name="label_2">
           <property name="text">
            <string>Time 2</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QTimeEdit" name="time2">
           <property name="time">
            <time>
             <hour>12</hour>
             <minute>0</minute>
             <second>0</second>
            </time>
           </property>
          </widget>
         </item>
        </layout>
       </item>
       <item>
        <widget class="QSlider" name="horizontalSlider">
         <property name="minimumSize">
          <size>
           <width>128</width>
           <height>19</height>
          </size>
         </property>
         <property name="maximumSize">
          <size>
           <width>20000</width>
           <height>19</height>
          </size>
         </property>
         <property name="maximum">
          <number>100</number>
         </property>
         <property name="singleStep">
          <number>1</number>
         </property>
         <property name="value">
          <number>50</number>
         </property>
         <property name="orientation">
          <enum>Qt::Horizontal</enum>
         </property>
        </widget>
       </item>
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout_3">
         <item>
          <widget class="QLabel" name="label_3">
           <property name="text">
            <string>Percentage</string>
           </property>
          </widget>
         </item>
         <item>
          <widget class="QSpinBox" name="percentage">
           <property name="maximumSize">
            <size>
             <width>60</width>
             <height>22</height>
            </size>
           </property>
           <property name="value">
            <number>50</number>
           </property>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </item>
     <item>
      <widget class="QPushButton" name="pushButton">
       <property name="text">
        <string>Calculate</string>
       </property>
      </widget>
     </item>
     <item>
      <layout class="QHBoxLayout" name="horizontalLayout_4">
       <item>
        <widget class="QLabel" name="label_4">
         <property name="text">
          <string>Output Time</string>
         </property>
        </widget>
       </item>
       <item>
        <widget class="QLabel" name="time3">
         <property name="text">
          <string>6:00:00 AM</string>
         </property>
        </widget>
       </item>
      </layout>
     </item>
    </layout>
   </widget>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>284</width>
     <height>21</height>
    </rect>
   </property>
   <widget class="QMenu" name="menuHelp">
    <property name="title">
     <string>Help</string>
    </property>
    <addaction name="actionThe_Algorithm"/>
    <addaction name="actionThe_Algorithm_2"/>
    <addaction name="actionAbout_Midpoint"/>
   </widget>
   <addaction name="menuHelp"/>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
  <action name="actionThe_Algorithm">
   <property name="text">
    <string>What is 'Fraction'?</string>
   </property>
  </action>
  <action name="actionThe_Algorithm_2">
   <property name="text">
    <string>The Algorithm</string>
   </property>
  </action>
  <action name="actionAbout_Midpoint">
   <property name="text">
    <string>About Midpoint</string>
   </property>
  </action>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

有关qtimespan.h的源代码,请参阅here

以下是程序的样子:

enter image description here

1 个答案:

答案 0 :(得分:0)

我认为您正在尝试在C ++应用程序中包含并链接到“C”库。如果你的QT是一个C库,那么调用约定就是__cdecl,但是C ++可能正在寻找与__stdcall或其他约定的链接。 在这种情况下,如果您确定使用C库与CPP应用程序链接,那么请在

中包含C标头

#ifdef _cplusplus

extern“C”{

#ENDIF

#include&lt;你的听众在这里&gt; //例如QtGui和QTCore等标题

#ifdef _cplusplus

}

#ENDIF

这将确保您能够在没有CPP应用程序任何问题的情况下使用C库