使用 QMake 意想不到的结果构建库和应用程序

问题描述 投票:0回答:0

我有一个简单的 Qt5/C++ 测试项目 (test2lib),它应该创建一个可执行应用程序(即 test2lib),以及一堆 .so 文件(每个动物一个,例如 dog.so)。 libfolder/ 目录包含每个动物的一个 .cpp/.h 文件(例如:dog.h、cat.h)。每个动物文件都是 testinterface 类的后代。

我正在结合各种 SO 帖子(如何为动态加载构建 QLibrary 文件以及如何从单个 qmake 文件创建可执行文件 + .so 文件)来构建我的项目。虽然我的项目确实构建了,但我最终得到了可执行文件,但没有 cat.so 或 dog.so 我哪里出错了?

我的qmake是:

QT -= gui
TEMPLATE = app
CONFIG += c++17 console
CONFIG -= app_bundle

SOURCES += \
        main.cpp \
        testinterface.cpp

HEADERS += \
        testinterface.h
    
    # Default rules for deployment.
    qnx: target.path = /tmp/$${TARGET}/bin
    else: unix:!android: target.path = /opt/$${TARGET}/bin
    !isEmpty(target.path): INSTALLS += target
    
    # Get a list of all the animal .cpp files in the models directory
    ANIMAL_SOURCES = $$files(libfolder/*.cpp)
    
    # Create a library for each animal .cpp file
    for(FILE, ANIMAL_SOURCES) {
        # Extract the name of the animal from the file name
        ANIMAL = $$basename($$dirname(FILE))
        # Create the library target for this animal
        LIBNAME = $${ANIMAL}.so
        $${ANIMAL}.target = $$LIBNAME
        $${ANIMAL}.sources = $$FILE
        # Add the library target to the list of targets
        SUBDIRS += $$ANIMAL
    }
    
    # Build each animal type library
    define_build_subdirs {
        for(dir, SUBDIRS) {
            message("Building $$dir")
            SUBDIR = $$dir
            include($$dir/$${SUBDIR}.pro)
        }
    }

主文件夹包含main.cpp和testinterface.h如下:

#ifndef TESTINTERFACE_H
#define TESTINTERFACE_H


    class TestInterface
    {
    public:
        TestInterface();
        virtual ~TestInterface()
        {
        }
    
        virtual int getValues() = 0;
    };
    
    #endif // TESTINTERFACE_H

最后在 libfolder 中我有 dog.h 包含:

#ifndef DOG_H
#define DOG_H

#include "testinterface.h"
#define TESTDLL_LIBSHARED_EXPORT


class TESTDLL_LIBSHARED_EXPORT Dog : public TestInterface
{

public:
    Dog();
    virtual ~Dog();

    int a;
    int b;
    int c;

    int getValues() override; // MSVC may not support "override"
};

// return pointer to interface!
// Dog can and should be completely hidden from the application
extern "C" TESTDLL_LIBSHARED_EXPORT TestInterface *create_Dog()
{
    return new Dog();
}

#endif // DOG_H

我计划稍后添加库的动态加载等......但我似乎在我的项目开始之前就偏离了轨道。

qt5 shared-libraries qmake
© www.soinside.com 2019 - 2024. All rights reserved.