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()
{

}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章