Saturday, December 28, 2013

QtCreator and linking minGW .a libraries

Started with my coding side project again and messed with the linking of .a shared library files. These "archive" files are the same thing as Visual C++ .lib files, but QtCreator does not like them and so they must be manually linked. Lib files can be added through dialog.

First step that I failed to do (really basic mistake :)) is to export the functions in dll in the first place. Otherwise one can link to libraries forever and still get unresolved errors. To export functions or classes in Qt Creator library project, simply add Q_DECL_EXPORT to function or class declaration in header. For example: int Q_DECL_EXPORT mult(int a, int b);

Next thing is to figure out, how to add .a libraries to .pro file so that they link. I found it out with a round trip as I tried to generate .lib based on existing .dll. This is actually really simple and involves a few steps (tips from page
  • open Visual C++ command prompt: Start -> Microsoft Visual Studio 2010 -> Visual Studio Command Promt;
  • type command: dumpbin /exports C:\yourpath\yourlib.dll
  • this dumpbin command actually gave me a sign that my functions are not exported as they were not listed in output
  • copy the names from this section:
ordinal hint RVA      name

1    0 00017770 jcopy_block_row
2    1 00017710 jcopy_sample_rows
3    2 000176C0 jdiv_round_up
4    3 000156D0 jinit_1pass_quantizer
5    4 00016D90 jinit_2pass_quantizer
6    5 00005750 jinit_c_coef_controller
  • paste names into a text file with .def extension and add EXPORTS as the first line:
  • run command: lib /def:C:\mypath\mylib.def /OUT:C:\mypath\mylib.lib
  • copy lib file to include directory and add it using Qt library adding dialog: right click in .pro file and "Add Library...
The added command block can also understand .a files so you don't actually need to create .lib files. It looks like this:

win32:CONFIG(release, debug|release): LIBS += -L$$PWD/include/ \
else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/include/ \
else:unix: LIBS += -L$$PWD/include/ -lgraphclasses -lsimplemaths
INCLUDEPATH += $$PWD/include
DEPENDPATH += $$PWD/include

The -l addition in the beginning replaces the "lib" part in library file name, so "libsimplemaths" becomes "-lsimplemaths".

Basically this is it but it took me some hours to find out why my simple dll does not link. Tried VC++ dll's and lib's also but there are other problems with compiler differences and they don't work with minGW.

No comments:

Post a Comment