LoginSignup
4
2

More than 3 years have passed since last update.

実行ファイル→動的ライブラリA→動的ライブラリBのリンク構成で、実行ファイルは動的ライブラリBに依存しないか

Last updated at Posted at 2020-09-13

はじめに

拙著「ライブラリを作ろう」に入れられなかったネタを書きます。

実行ファイルが動的ライブラリAを動的リンクし、動的ライブラリAが動的ライブラリBを動的リンクする場合
実行ファイルは、動的ライブラリBを意識せず(依存せず)作れるのか。

スライド1.PNG

理論的には依存しないはずだけど実際どうなのか、サンプルで検証します。
サンプルは、Githubに格納してあります。
動作検証はWindows10のQt/C++で行っています。

次のような構成にします。

スライド2.PNG

  • 実行ファイルプロジェクト:user_libray
  • 動的ライブラリAプロジェクト:make_library
  • 動的ライブラリBプロジェクト:make_library2

ビルドは、動的ライブラリBプロジェクト→動的ライブラリAプロジェクト→実行ファイルプロジェクトの順番に行います。

make_library2プロジェクト

文字を修飾するクラスを用意します。
作成されるライブラリファイルは、libdeco.a(インポートライブラリ)とdeco.dll(動的ライブラリ)です。

decoration.cpp
#include "decoration.h"
#include <QString>

Decoration::Decoration()
{
}

QString Decoration::getDecoration()
{
    return QString("#### ");
}

make_libraryプロジェクト

Decoration.getDecoration()を使用して、文字を修飾します。
作成されるライブラリファイルは、libhello.a(インポートライブラリ)とhello.dll(動的ライブラリ)です。

word.cpp
#include "word.h"
#include "decoration.h"

Word::Word()
{
}

const char* Word::getWord()
{
    Decoration decoration;
    QString deco = decoration.getDecoration();
    QString str = "Hello,World!";
    deco += str;
    return deco.toLocal8Bit().constData();
}

user_librayプロジェクト

main.cpp
#include <QCoreApplication>
#include <QDebug>

#include "word.h"

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    Word* word = new Word();
    qDebug() << word->getWord();
    delete word;

    return a.exec();
}

getWord()で取得した文字列をコンソール出力しています。

実行結果

#### Hello,World!

プロジェクトファイルを見てみます。

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle

SOURCES += \
        main.cpp

win32: LIBS += -L$$PWD/../lib/ -lhello

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

-helloでhelloライブラリは指定しますが、decoライブラリは指定していません。
ヘッダファイルについてもmake_library2ディレクトリは参照していません。

検証結果

実行ファイルはdecoライブラリを意識せずに作成できることが検証できました。

4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2