Using DLL in Qt project
I am trying to use TinyCC (http://tinycc.org) in one of my projects. I managed to compile their example to use the libtcc to compile a C program inside a C program and it runs just fine. For that I am using gcc (I am on Windows and I use MinGW):
gcc examples/libtcc_test.c -Llib -Ilibtcc libtcc.dll
Now I created a new Qt console project and tried to do the same. However, I couldn't even get past the compile stage.
I added the headers but I keep getting
undefined reference to 'tcc_new()'for all those tcc_*() functions I have to call that are part of the library. I get these errors in the example project when I don't pass libtcc.dll to the gcc command. Therefore, I assume that the MinGW compiler I am using in QtCreator lacks that DLL too and therefore can't link. I stumbled across this tutorial that explains how to use a DLL in a Qt project: https://www.youtube.com/watch?v=9JTooLxhmC0
At 05:00 you can see that he simply adds the DLL to the LIBS variable. I tried to do the same but I then I can't compile because I get the following error:
C:\Users\joel.bodenmann\Documents\junk\TccTest\libtcc.dll:-1: error: file not recognized: File format not recognized
Apparently it tries to compile that DLL file. However, in the linked tutorial that works just fine.
Also, the TinyCC package contains some *.def files. Do I have to use them? I didn't have to use them in the manual example listed above.
Can somebody tell me what I need to do to use a DLL in a Qt project? Google doesn't bring up many helpful things. After all it's working in that tutorial so I expect this to be some minor issue.
Note: In the non-Qt example the compiler needs the *.a file and the *.dll file passed as the last argument. I don't quite understand why. I thought that the *.a file is simply a static library for GCC. Why does it need the DLL then?
You do not link against dll. DLL contains code loaded at runtime.
But linker needs to find a declaration of function to make a call.
Static libs ( normally *.lib) on Windows contain both: declaration and executable code.
If you build dll normally *.lib file is also produced, but such file contains only declaration.
So you link against name.lib file, but provide only name.dll file to user.
I am not sure if GCC use a extension instead of lib.
@alex_malyu Thank you for your reply!
Well, that is how I understood things too. But I really started questioning my knowledge once I saw that video I linked. After all he gets the very same errors that I get - then he adds the *.dll to the LIBS variable in the *.pro file and his problem is gone.
Regarding the *.lib file: All the TCC download (for windows) comes with is one *.dll file, one *.a file and a bunch of *.def files. I have never seen *.def files before. The content looks like this:
LIBRARY libtcc.dll EXPORTS pstrcat pstrcpy pstrncpy tcc_add_file tcc_add_include_path tcc_add_library tcc_add_library_path tcc_add_symbol tcc_add_sysinclude_path tcc_basename tcc_compile_string tcc_define_symbol tcc_delete tcc_error tcc_error_noabort tcc_fileextension tcc_free tcc_get_symbol tcc_malloc tcc_mallocz tcc_memstats tcc_new tcc_output_file tcc_parse_args tcc_print_stats tcc_realloc tcc_relocate tcc_run tcc_set_error_func tcc_set_lib_path tcc_set_options tcc_set_output_type tcc_strdup tcc_undefine_symbol tcc_warning
Now, all those functions listed in that file is what I need. Can I generate a *.lib file from that or somehow directly use it?
I am not sure why it work for him,
but proper way is
unix:LIBS += -L/usr/local/lib -lmath
win32:LIBS += c:/mylibs/math.lib
Again GCC may give it .a extension. If I were you I would try to link against it and made sure appropriate dll is located at the same folder as your executable when you are going to execute it.
But last supposed not to affect your linking. dll file does not have information linker needs to link against it.
.deffile contains the names of the exported symbols from the library. It's pretty much a legacy format to specify which symbols are exported, or at least I've not known anyone using it actively for years. Still, from time to time you're going to stumble upon it. You can create, with the proper tool (only the last step is relevant to your case, since you have the
.libfile that you can use to link with.
After you've obtained the library file, link in the usual manner (in the project file):
LIBS += -Lc:/Path/to/lib/file -lnameoflibfile
Bear in mind that the name is simply the name of the library stripped of prefixes and suffixes, so if you want to link against
libtcc.libyou just provide
-ltccto the link line.
Since you are using MinGW you don't have to bother about lib or def files.
LIBS += -L<path> -ltcc
This should do the work. Make sure you use Qt built using g same MinGW version
@kshegunov Thank you very much for your reply!
That actually worked out.
@asanka424 Thank you for your suggestion. When I started trying to get this working that was the first thing I tried and I didn't succeed, sadly. However, I started a new project completely from scratch and now it works just fine using the method your described.
Thank you for your help guys, highly appreciated!