初始化嵌入在静态库中的qt资源 [英] Initializing qt resources embedded in static library

查看:800
本文介绍了初始化嵌入在静态库中的qt资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有下面的情况:我需要在独立的静态库创建窗口小部件,然后将与最终应用程序(visual c ++ 9.0,qt 4.5)链接。
此静态小部件库包含一些资源(图标),并由几个.cpp文件(每个包含独立的小部件)组成。据我所知,我必须初始化qt资源系统,如果我使用它们(资源)在静态库,调用Q_INIT_RESOURCE(resource_file_name)。我用下面的代码(在静态库中的每个.cpp文件)解决这个问题:

 

# include< QAbstractButton>

namespace {
struct StaticLibInitializer
{
StaticLibInitializer()
{
Q_INIT_RESOURCE(qtwidgets_custom_resources);
}
};
StaticLibInitializer staticLibInitializer;
}

// ...小部件代码....

而不是我的第一种方法,我已经创建了单独的init.cpp文件在静态库项目与初始化代码(以避免包括初始化代码在每个.cpp文件),但是这没有工作。 p>

为什么这不起作用?



这种使用StaticLibInitializer的方法在各种编译器和平台之间是安全和可移植的吗?

解决方案

它没有工作,因为你设法受到静态初始化顺序fiasco



您不能移动初始化静态对象的代码超出使用这些静态对象的翻译单元(可以将其视为源文件)。不是你做的方式。如果你想使用你使用的方案来初始化这些静态对象,而不是只将声明移动到你的init.hpp头,但在每个使用静态的文件中留下 StaticLibInitializer staticLibInitializer; 对象。

上面的通知假设每个小部件只使用自己的资源。如果你有一个窗口部件的资源被另一个窗口部件使用的情况,你再次运行到静态初始化顺序fiasco。您可以使用下面的代码管理这种情况

  StaticLibInitializer 
{
void initialize $ b {
static Q_INIT_RESOURCE(qtwidgets_custom_resources);
}

StaticLibInitializer()
{
initialize();
}
}

以确保StaticLibInitializer的乘法实例化将初始化给定资源只有一次,然后实例化StaticLibInitializer为您将要使用在给定的翻译单元中的每个资源。


I have next situation: I need to create widget in standalone static library, which then will be linked with final application (visual c++ 9.0, qt 4.5). This static widget library contains some resources (icons), and consist of a several .cpp files (each contains standalone widget). As far as I know, i must initialize qt resource system, if i use them (resources) in static library, with call to "Q_INIT_RESOURCE( resource_file_name )". I solved this with next code (in every .cpp file in static library):


#include <QAbstractButton>

namespace {
struct StaticLibInitializer
{
    StaticLibInitializer()
    {
    	Q_INIT_RESOURCE(qtwidgets_custom_resources);
    }
};
StaticLibInitializer staticLibInitializer;
} 

// ... widget code ....

Instead of my first approach, I have created separate init.cpp file in static library project with initialization code (to avoid including initialization code in every .cpp file), but this didn't work.

Why this didn't work ?

Is this approach with StaticLibInitializer is safe and portable among various compilers and platforms ?

解决方案

It didn't work because you managed to get hit by static initialization order fiasco.

You can't move your code that initializes static objects outsize the translation unit (you can read it as source file) where these static objects are used. Not the way you did it. If you want to use the scheme you are using to initialize these static objects than move only declarations to your init.hpp header but leave instatiations StaticLibInitializer staticLibInitializer; in each file which uses static objects.
Above advice assumes each widget uses only its own resources. If you have situation in which one widget's resources are used by another widget you run into static initialization order fiasco again. You can manage this situation by using code like this

StaticLibInitializer
{
    void initialize()
    {
        static Q_INIT_RESOURCE(qtwidgets_custom_resources);
    }

    StaticLibInitializer()
    {
         initialize();
    }
}

to make sure multiply instantiations of StaticLibInitializer will initialize given resource only once and then instantiate StaticLibInitializer for every resource you are going to use in given translation unit.

这篇关于初始化嵌入在静态库中的qt资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆