1. 程式人生 > >Qtcreator 編譯靜態庫並使用

Qtcreator 編譯靜態庫並使用

靜態庫的編譯

//.pro檔案
#-------------------------------------------------
#
# Project created by QtCreator 2018-08-10T09:46:45
#
#-------------------------------------------------

QT       -= gui

TARGET = buildlibtest
TEMPLATE = lib
CONFIG += staticlib

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings # depend on your compiler). Please consult the documentation of the # deprecated API in order to know how to port your code away from it. DEFINES += QT_DEPRECATED_WARNINGS # You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line. # You can also select to disable deprecated APIs only up to a certain version of Qt. #DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 SOURCES += \ buildlibtest.cpp HEADERS += \ buildlibtest.h unix { target.path = /usr/lib INSTALLS += target }
//標頭檔案
#ifndef BUILDLIBTEST_H
#define BUILDLIBTEST_H


class Buildlibtest
{

public:
    Buildlibtest();
    int add(int num1,int num2);
};

#endif // BUILDLIBTEST_H
//原始碼
#include "buildlibtest.h"
Buildlibtest::Buildlibtest()
{
}
int Buildlibtest::add(int num1,int num2){
    return (num1+num2);
}

靜態庫的使用

  • 說明:我查詢的部落格對於靜態庫的使用都是一筆帶過,但是對於我這各小白還是折騰了許久,下面就簡要說明一下
  • 將上面得到的.a靜態庫,和原來的.h標頭檔案(共兩個)放入新建立的資料夾中
  • 新建專案,將靜態庫和標頭檔案匯入
  • 按照上面部落格將靜態庫匯入文件時要注意不要“對為debug版本新增“d”作為字尾”和這個選項打勾(本人就是因為這一項打了勾,半天執行不了)
    這裡寫圖片描述
  • 測試程式碼
    這裡寫圖片描述
#-------------------------------------------------
#
# Project created by QtCreator 2018-08-10T10:23:55
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = testlib
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which has been marked as deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0


SOURCES += \
        main.cpp \
        mainwindow.cpp

HEADERS += \
        mainwindow.h \
    buildlibtest.h

win32: LIBS += -L$$PWD/./ -lbuildlibtest

INCLUDEPATH += $$PWD/.
DEPENDPATH += $$PWD/.

win32:!win32-g++: PRE_TARGETDEPS += $$PWD/./buildlibtest.lib
else:win32-g++: PRE_TARGETDEPS += $$PWD/./libbuildlibtest.a

}
#ifndef BUILDLIBTEST_H
#define BUILDLIBTEST_H


class Buildlibtest
{

public:
    Buildlibtest();
    int add(int num1,int num2);
};

#endif // BUILDLIBTEST_H
#ifndef MAINWINDOW_H
#define MAINWINDOW_H

#include <QMainWindow>
#include "buildlibtest.h"
#include <QDebug>
class MainWindow : public QMainWindow
{
    Q_OBJECT

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

#endif // MAINWINDOW_H
#include "mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
{
    Buildlibtest obj;
    qDebug()<<obj.add(1,5);
}

MainWindow::~MainWindow()
{

}