简体   繁体   English

如何使Qt不省略方法?

[英]How to make qt not omit methods?

I have following Qt project structure: 我有以下Qt项目结构:

static library #1 静态库#1

public class LibraryClass
{
     public int Test();
}

static library #2 (reference to library #1) 静态库2(对库1的引用)

void SomeMethod()
{
   ...
   x = library1Instance.Test();
   ...
}

executable (reference to library #1 and library #2) 可执行文件(对库1和库2的引用)

void SomeOtherMethod()
{
   ...
   library2Instance.SomeMethod();
   ...
}

Libraries #1 and library #2 compile without errors. 库#1和库#2编译没有错误。 But when I'm trying to compile executable, I got error "undefined reference to LibaryClass::Test()". 但是,当我尝试编译可执行文件时,出现错误“对LibaryClass :: Test()的未定义引用”。 Which means that libary #1 is linked without LibraryClass::Test() method. 这意味着库#1是在没有LibraryClass :: Test()方法的情况下链接的。

If I create another class in library #1, and make that class reference to LibaryClass::Test() method, everything will compile. 如果我在库#1中创建另一个类,并对该类引用LibaryClass :: Test()方法,则所有内容都会编译。

As far as I understood, compiler omits method LibaryClass::Test() because it can found no internal reference to it. 据我了解,编译器省略了方法LibaryClass :: Test(),因为它找不到内部引用。 So, I should somehow to mark it as method for export or something. 因此,我应该以某种方式将其标记为导出方法或其他方法。 How can I do it? 我该怎么做?

You should make a global definition in a file included in all projects: 您应该在所有项目中包含的文件中进行全局定义:

#if defined(MAKEDLL)
# define MY_EXPORT Q_DECL_EXPORT
#else
# define MY_EXPORT Q_DECL_IMPORT
#endif

In libraries, add this line to .pro files: 在库中,将此行添加到.pro文件中:

DEFINES   += MAKEDLL

In .pro of executable, add nothing. 在可执行文件.pro中,不添加任何内容。 Then, in library define LibraryClass as 然后,在库中将LibraryClass定义为

public MY_EXPORT class LibraryClass
{
    ...
}

For static method: 对于静态方法:

MY_EXPORT void SomeMethod()
{
   ...
   x = library1Instance.Test();
   ...
}

In executable, instantiate the class and call the method as usual. 在可执行文件中,实例化该类并照常调用该方法。 The linker will resolve the dependencies if your subprojects (library1, library2, and executable) are built to the same directory. 如果您的子项目(library1,library2和可执行文件)构建在同一目录中,则链接程序将解决依赖关系。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM