0

我使用的是 Windows 7、QCreator、Qt 5.0,我的 PC 是 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

//主窗口.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

//主窗口.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 的源代码,请参见此处

程序如下所示:

在此处输入图像描述

4

1 回答 1

0

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

#ifdef _cplusplus

外部“C”{

#万一

#include <你的听众在这里> //例如QtGui和QTCore等头文件在这里

#ifdef _cplusplus

}

#万一

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

于 2013-06-21T12:41:28.640 回答