简体   繁体   English

在QT4应用程序中包括静态外部库

[英]Include static external library in QT4 application

Newish C++ programmer here. 这里是新的C ++程序员。 I am creating a QT4 application and it's gotten large enough to where I want to start using log4cplus. 我正在创建一个QT4应用程序,它已经足够大到我想开始使用log4cplus的地方。 I think I'm close but qmake is still not cooperating. 我想我已经接近了,但是qmake仍然没有合作。

I am running on a Windows machine, and I compiled log4cplus as a static library under cygwin ( $ ./configure --enable-static ). 我在Windows机器上运行,并且将log4cplus编译为cygwin( $ ./configure --enable-static )下的静态库。

First Question 第一个问题
When I compiled log4cplus I got two files. 当我编译log4cplus时,我得到了两个文件。

  • liblog4cplus.a liblog4cplus.a
  • liblog4cplus.dll.a liblog4cplus.dll.a

Do I need to include both of them? 我是否需要包括两个人? What's with the .dll.a file? .dll.a文件是什么?

Second Question 第二个问题
When I compile moc succeeds but gcc fails when trying to use any of log4cplus classes. 当我编译moc成功但尝试使用任何log4cplus类时gcc失败。 I'm not sure if it can't find the header files or if it can't find the actual library. 我不确定它找不到头文件还是找不到实际的库。

g++ -c -g -frtti -fexceptions -mthreads -Wall -DUNICODE -DQT_LARGEFILE_SUPPORT -DQT_DLL -DQT_GUI_LIB -DQT_CORE_LIB -DQT_THREAD_SUPPORT -DQT_NEEDS_QMAIN -I"..\..\..\Qt\2010.05\qt\include\QtCore" -I"..\..\..\Qt\2010.05\qt\include\QtGui" -I"..\..\..\Qt\2010.05\qt\include" -I"external" -I"..\..\..\Qt\2010.05\qt\include\ActiveQt" -I"debug" -I"..\..\..\Qt\2010.05\qt\mkspecs\win32-g++" -o debug\qrc_tilex.o debug\qrc_tilex.cpp
g++ -enable-stdcall-fixup -Wl,-enable-auto-import -Wl,-enable-runtime-pseudo-reloc -mthreads -Wl -Wl,-subsystem,windows -o debug\tilex.exe object_script.tilex.Debug  -L"c:\work\workspace\tilex\lib" -L"c:\Qt\2010.05\qt\lib" -lmingw32 -lqtmaind -Lliblog4cplus.a -lQtGuid4 -lQtCored4 
./debug\main.o: In function `Z5qMainiPPc':
C:\work\workspace\tilex/main.cpp:37: undefined reference to `log4cplus::Logger::getDefaultHierarchy()'
C:\work\workspace\tilex/main.cpp:37: undefined reference to `log4cplus::BasicConfigurator::BasicConfigurator(log4cplus::Hierarchy&)'
C:\work\workspace\tilex/main.cpp:51: undefined reference to `log4cplus::BasicConfigurator::~BasicConfigurator()'
C:\work\workspace\tilex/main.cpp:51: undefined reference to `log4cplus::BasicConfigurator::~BasicConfigurator()'
mingw32-make[1]: Leaving directory `C:/work/workspace/tilex'
collect2: ld returned 1 exit status
mingw32-make[1]: *** [debug\tilex.exe] Error 1
mingw32-make: *** [debug] Error 2

My project resides in C:\\work\\workspace\\tilex . 我的项目位于C:\\work\\workspace\\tilex

and my directory structure is this: 我的目录结构是这样的:

tilex
     /lib
         /<*.a files>
     /external
              /log4cplus
                        /<header files>

Relevant portion of my .pro file. 我的.pro文件的相关部分。 (I've tried several permutations of all these variables, and still get the same result) (我已经尝试了所有这些变量的几种排列,但仍然得到相同的结果)

INCLUDEPATH += C:\\work\\workspace\\tilex\\external
QMAKE_LIBDIR += C:\\work\\workspace\\tilex\\lib
LIBS += -Lliblog4cplus.a

My main file (which compiles and runs fine without log4cplus). 我的主文件(无需log4cplus即可编译并正常运行)。

#include "Tilex.h"
#include <QtGui>
#include <QApplication>

#include <log4cplus/logger.h>
#include <log4cplus/configurator.h>

using namespace log4cplus;

int main(int argc, char *argv[])
{
    Q_INIT_RESOURCE(tilex);
    QApplication app(argc, argv);

    // Fails
    BasicConfigurator config;
//    config.configure();
//    Logger::getInstance()
//    Logger logger = Logger::getInstance("main");
//    LOG4CPLUS_WARN(logger, "Hello, World!");
    // !

    Tilex mainWin;
    mainWin.show();
    return app.exec();
}

Assuming everything else is correct, you should change your LIBS to this: 假设其他所有内容都正确,则应将LIBS更改为此:

LIBS += -llog4cplus

The -L flag tells g++ to set a path to look for libraries. -L标志告诉g ++设置查找库的路径。 The -l flag tells it to link a specified library (with the lib- prefix and the filetype removed). -l标志告诉它链接指定的库(已删除lib-前缀和文件类型)。

If by you mean large as in size instead of complexity when you are saying your application is getting 'large', you might want to ask yourself why you are using static linking in the first place (look at this thread ). 如果说应用程序正在变大时,如果您指的是大小而不是复杂性,那么您可能想问自己为什么首先使用静态链接(请看此线程 )。

You need to put two things on your link line: 您需要在链接行上放置两件事:

-Ldirectory\where\log4c\library\file\lives

and

-llog4cplus

The -L tells the linker to add that directory to its search path. -L告诉链接器将该目录添加到其搜索路径。 The -l tells the linker to look for the file with lib prepended and .a or .so appended. -l告诉链接器查找带有lib前缀和.a.so的文件。 So -lfoo tells the linker to look for libfoo.a or libfoo.so in the linker search path. 因此, -lfoo告诉链接程序在链接程序搜索路径中查找libfoo.alibfoo.so

Alternatively you could directly put the full path to the library on the link line without using any -L or -l : 或者,您可以直接将链接库的完整路径放在链接行上,而无需使用任何-L-l

g++ blah blah directory\where\log4c\library\file\lives\liblog4cplus.a blah blah

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

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