在 QT4 应用程序中包含静态外部库
这里是新 C++ 程序员。我正在创建一个 QT4 应用程序,它已经足够大到我想要开始使用 log4cplus 的地方。我想我已经很接近了,但 qmake 仍然不合作。
我在 Windows 机器上运行,并在 cygwin 下将 log4cplus 编译为静态库($ ./configure --enable-static
)。
第一个问题
当我编译 log4cplus 时,我得到了两个文件。
- liblog4cplus.a
- liblog4cplus.dll.a
我需要同时包含它们吗? .dll.a
文件是怎么回事?
第二个问题
当我编译 moc 成功但尝试使用任何 log4cplus 类时 gcc 失败时。我不确定它是否找不到头文件或者是否找不到实际的库。
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
我的项目位于C:\work\workspace\tilex
。
我的目录结构是这样的:
tilex
/lib
/<*.a files>
/external
/log4cplus
/<header files>
我的 .pro 文件的相关部分。 (我已经尝试了所有这些变量的几种排列,但仍然得到相同的结果)
INCLUDEPATH += C:\\work\\workspace\\tilex\\external
QMAKE_LIBDIR += C:\\work\\workspace\\tilex\\lib
LIBS += -Lliblog4cplus.a
我的主文件(无需 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();
}
Newish C++ programmer here. I am creating a QT4 application and it's gotten large enough to where I want to start using log4cplus. I think I'm close but qmake is still not cooperating.
I am running on a Windows machine, and I compiled log4cplus as a static library under cygwin ($ ./configure --enable-static
).
First Question
When I compiled log4cplus I got two files.
- liblog4cplus.a
- liblog4cplus.dll.a
Do I need to include both of them? What's with the .dll.a
file?
Second Question
When I compile moc succeeds but gcc fails when trying to use any of log4cplus classes. 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
.
and my directory structure is this:
tilex
/lib
/<*.a files>
/external
/log4cplus
/<header files>
Relevant portion of my .pro file. (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).
#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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设其他一切都正确,您应该将 LIBS 更改为:
-L 标志告诉 g++ 设置查找库的路径。 -l 标志告诉它链接指定的库(删除 lib- 前缀和文件类型)。
如果当您说您的应用程序变得“大”时,您的意思是大小而不是复杂性,那么您可能想问自己为什么首先使用静态链接(看看这个 线程)。
Assuming everything else is correct, you should change your LIBS to this:
The -L flag tells g++ to set a path to look for libraries. The -l flag tells it to link a specified library (with the lib- prefix and the filetype removed).
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).
您需要在链接行上放置两件事:
-L 告诉链接器将该目录添加到其搜索路径中。
-l
告诉链接器查找前面带有lib
并附加.a
或.so
的文件。因此,-lfoo
告诉链接器在链接器搜索路径中查找libfoo.a
或libfoo.so
。或者,您可以直接将库的完整路径放在链接行上,而不使用任何
-L
或-l
:You need to put two things on your link line:
and
The
-L
tells the linker to add that directory to its search path. The-l
tells the linker to look for the file withlib
prepended and.a
or.so
appended. So-lfoo
tells the linker to look forlibfoo.a
orlibfoo.so
in the linker search path.Alternatively you could directly put the full path to the library on the link line without using any
-L
or-l
: