Results 1 to 20 of 28

Thread: How to use external source code in Qt creator?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: How to use external source code in Qt creator?

    If you create a dynamic library in winblows (you have mentioned mingw so that I suppose it's winblows) you get two final files: the dll and the import library, which is an ordinary .lib. The import library is a small file which contains modules like this:

    Qt Code:
    1. void MyProcInDll()
    2. {
    3. if( ModuleHandle == NULL ) ModuleHandle = LoadModule("MyDll");
    4. if( ProcInstanceOfMyProc == NULL )
    5. {
    6. if( ModuleHandle != NULL ) ProcInstanceOfMyProc = MakeProcInstance(ModuleHandle,"MyProcInDll");
    7. else error;
    8. }
    9.  
    10. if( ProcInstanceOfMyProc != NULL ) ProcInstanceOfMyProc();
    11. else error;
    12. }
    To copy to clipboard, switch view to plain text mode 

    A "static linking" of a dll is linking with the import library. Externals from the dll are resolved by externals of the import library. When you call an entry in the dll, a stub from the import library is called, which in turn makes sure that the dll is loaded and the entry point from the dll is got. Then it calls the entry point in the dll. It also makes sure that LoadModule() and MakeProcInstance() will be called only once (or once per entry) so that the overhead is minimal. The LoadModule() in the stub does not contain a path to the dll. Therefore:

    (1) If you link statically with a dll you link with the corresponding import library.
    (2) You make neither LoadModule() nor MakeProcInstance(), you simply call entries from your library.
    (3) You will have LIBS directive in your profile file. The LIBS directive will reference the import library, not the dll.
    (4) The dll must be on the PATH, otherwise, the LoadModule() in the stub will not find it.

    Static linking is the simplest way of using a dll. Nevertheless, static linking neither allows deciding which dll to link "on fly" nor it allows unlinking the dll during the application run. If you need such things, you must do LoadModule() and MakeProcInstance() yourself and do not link with the import library. This is the "dynamic linking". With dynamic linking, you won't link your application with a dll, you will do it on the application run. Therefore, no LIBS.

    Note that it's a bit different in Linux. There are no import libraries in Linux. The LIBS directive will reference a dll (a "shared object, .so). I am sure that dynamic linking is possible somehow but I have not needed it so far. That's why I don't know how

  2. #2
    Join Date
    Oct 2013
    Posts
    102
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: How to use external source code in Qt creator?

    Thanks guys for all extensive info on library linking, I'll go and try on an example. But for my qt app where using open source code (cpp, h files) I'll take the approach of just referencing the source. Thus I'll use INCLUDEPATH and SOURCES directive like
    Qt Code:
    1. INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
    2. SOURCES += "C:\Users\myuser\Documents\elab_c_proj\mat.cpp"
    To copy to clipboard, switch view to plain text mode 
    However I'm still a bit puzzled why do I need the SOURCES directive. Without it, I have the access to "precalc" function in code, but the comipiling gives me error "undefined refernce to". Please clearify why is this so, am I missing something? In this case it is just on file "mat.cpp" that needs to be refernced via SOURCES, but I don't want to add all source files this way

  3. #3
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: How to use external source code in Qt creator?

    The application project should not contain mat.cpp, mat.cpp belongs to another project. But you need a header (headers) for entries in your library (both in the case of a static lib and in the case of dll). Otherwise, the linker will not know what to do with precalc() external. Therefore, no SOURCES += mat.cpp but

    Qt Code:
    1. HEADERS += mat.hpp
    To copy to clipboard, switch view to plain text mode 

    Again do not do it yourself. Add existing file -> header -> mat.hpp from the Creator and link with mat.lib

    Qt Code:
    1. LIBS += path/mat.lib
    To copy to clipboard, switch view to plain text mode 

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How to use external source code in Qt creator?

    Here is a complete two project example.
    Unzip it, change directory into "example", run qmake and then mingw32-make.
    It should build the library "elab" first, then the program that uses it.

    The client program will be in "example/program/debug/program.exe" and you need to ensure "example/elab/debug/elab.dll" can be found by Windows.
    Attached Files Attached Files

  5. #5
    Join Date
    Oct 2013
    Posts
    102
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: How to use external source code in Qt creator?

    Thank you both for help. I guess I was thinking in wrong direction I thought that there was the third option available (not linking to lib or dll) but to include the source mat.cpp without having to build library from it in first place, I thought that Qt could just "take" cpp and hpp files from the external elab project and build/include it in my main qt app. So, can I import the whole elab project to my qt app and let the qt compile it with my qt app together?

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How to use external source code in Qt creator?

    Yes, of course you can... that's where you were at post three in this thread.

    Here is another example doing just that.
    You have two options for handling the #include directive in main.cpp. Either:
    • Use a relative or full path to mat.h as in this example
    • Use a bare #include "mat.h" and set an INCLUDEPATH in the PRO file,
      Qt Code:
      1. INCLUDEPATH += "../elab"
      To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  7. #7
    Join Date
    Oct 2013
    Posts
    102
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: How to use external source code in Qt creator?

    Thanks ChrisW67. Ok I want to use the later mentioned approach i.e. include just sources of my external elab project and let the Qt creator take care of compilig/including it. In the exmple2 you did
    Qt Code:
    1. HEADERS += ../elab/mat.h
    2. SOURCES += ../elab/mat.cpp main.cpp
    To copy to clipboard, switch view to plain text mode 
    while I did
    Qt Code:
    1. INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
    2. SOURCES += "C:\Users\myuser\Documents\elab_c_proj\mat.cpp"
    To copy to clipboard, switch view to plain text mode 
    I guess that if I use INCLUDEPATH to say where my hpp files are, I don't need additional HEADERS directive, ok? But what I want to clear is: if I don't specify my mat.cpp with SOURCES I can't compile. Therefore I would like (if possible), not to specify each cpp files one by one. Ok now I have just mat.cpp, but I could have mat1.cpp, mat2.cpp,mat3.cpp... Can't I just specify the directory where cpp files are?

  8. #8
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: How to use external source code in Qt creator?

    No. The INCLUDEPATH directive tells the compiler where to search headers specified in your code. INCLUDEPATH contains directories, not particular headers. Therefore:

    The mat.lib project:
    Qt Code:
    1. TEMPLATE = lib
    2. TARGET = mat
    3. CONFIG += staticlib
    To copy to clipboard, switch view to plain text mode 
    I am not sure whether you need to specify "mat" or "mat.lib" in the TEMPLATE directive on winblows. You will see. Add existing file mat.cpp from the creator and compile. You should pass. Because mat.cpp is so simple, no headers are needed right now.

    Having the library, compile the main project:
    Qt Code:
    1. TEMPLATE = app
    2. TARGET = example
    3. INCLUDEPATH = path to mat.hpp
    4. DEPENDPATH = path to mat.hpp
    5. LIBS = path/mat.lib
    To copy to clipboard, switch view to plain text mode 
    The DEPENDPATH is directive for creating a makefile which will make example.exe. Again, I am not sure whether you need to specify "example" or "example.exe" on winblows. Add existing files to the project: main.cpp (source) and mat.hpp (header). You can include by
    Qt Code:
    1. #include <mat.hpp>
    To copy to clipboard, switch view to plain text mode 
    Compile. You should pass.

  9. #9
    Join Date
    Oct 2013
    Posts
    102
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: How to use external source code in Qt creator?

    Thanks Radek, but I don't want to include a prebuilt library (lib, dll) in my qt app. I want (if possible) somehow referenco to source code in my case mat.cpp, to get access to "precalc" function in design mode and to get it compiled to final qt app exe. Therefore if I do:
    Qt Code:
    1. INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
    2. SOURCES += "C:\Users\myuser\Documents\elab_c_proj\mat.cpp"
    To copy to clipboard, switch view to plain text mode 
    compiling passed ok, my refernced mat.cpp becomes mat.o in qt app release folder. Everything ok, but as I said, I don't want to refernce each mat.cpp on by one. I know, now I have only one, but I could have 10 of them (mat1.cpp,mat2.cpp,...mat10.cpp) and I would like to refernce all of them with one directive if possible "kind of SOURCEDIRECTORIES". Is that possible?

  10. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: How to use external source code in Qt creator?

    Therefore I would like (if possible), not to specify each cpp files one by one. Ok now I have just mat.cpp, but I could have mat1.cpp, mat2.cpp,mat3.cpp... Can't I just specify the directory where cpp files are?
    No. Think about it for a moment. INCLUDEPATH gives the compiler (preprocessor) places to look for named header files it finds in the specified source files. If you had an equivalent for implementation files where would the names for source files come from? You could assume that it it would include every compilable file (C++, C, and on my machine, Fortran, assembler or Objective C) in each of the listed folders but for any substantial project this is likely to be unnecessarily slow, continually recompiling things, and frought with problems of pulling in unneeded files, clashing alternate implementations etc.

    I think you may be able to do
    Qt Code:
    1. SOURCES += ../elab/*.cpp
    To copy to clipboard, switch view to plain text mode 
    And have qmake expand the list for you. I would not recommend that for anything more than a handful-of-files project though.

    Ultimately though, reusing bits of compiled code between executables is what libraries are for.

  11. #11
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: How to use external source code in Qt creator?

    Arcull, C++ isn't Basic. There is no such thing like "source code library" in C++. Either you include a source file in the project and the file get compiled and used to build your app or the file does not exist for the project at all. Period. Libraries exist in C++ only at the object file level: you must build the library first and then you can use it in subsequent projects. C++ is a compiled language.

    Even if you succeed in specifying a directory where .cpp files are, all source files from that directory will be included in your project. That's something you surely do not want.

  12. #12
    Join Date
    Oct 2013
    Posts
    102
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: How to use external source code in Qt creator?

    Sorry I was away fro 2 days. Thanks for a detailed clarification Radek. Even if
    Qt Code:
    1. SOURCES += ../elab/*.cpp
    To copy to clipboard, switch view to plain text mode 
    does work as expected, meaning it includes all cpp files to my qt app, I'll consider your good advice and include just the ones I need, let's say just mat.cpp in my case. Ok, with this, the experiment part of using uncompiled source code in my qt app is closed. I know how to and tested on example.

    Now I would like to test the "lib" and "dll" option as well. I did try to open the "example" project from ChrisW67 in Qt creator. The "elab" project compiled to a dll ok, but I couldn't compile the "program", I got "undeclared refernce to precalc", meaning that the linker couldn't find the elab.dll. I double checked the folder containing the elab.dll was added to the system path enviromental variable, but still no go I alswo tried to append the
    Qt Code:
    1. CONFIG += dll
    To copy to clipboard, switch view to plain text mode 
    to "elab.pro" file, rebuild it, still no go. Besides why doesn't the "Add library wizzard" allow me to select a "dll" folder, is it meant just for libs?

    Ok, the same problem with "lib" option. I appended
    Qt Code:
    1. CONFIG += staticlib
    To copy to clipboard, switch view to plain text mode 
    to "elab.pro" (removed config += dll) and built the lib. But I can't use it my program, same thing "undeclared refence to precalc". In the using lib option I could try also the "Add library wizzard" which automatically added some code in the pro file, but that didn't help either. Using "lib" shouldn't require to add lib folder to path enviromental varialbes, right? Can you please point out, what am I missing here. Thank you again for your help and patience

  13. #13
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: How to use external source code in Qt creator?

    (1) If precalc() is "undeclared" then the header mat.hpp is missing. The error comes from the compiler, not from the linker. Add mat.hpp to your project and also check the LIBS directive. It should be
    Qt Code:
    1. LIBS += full_path/mat.lib
    To copy to clipboard, switch view to plain text mode 

    (2) In winblows, you cannot link with the dll directly. You need the import library as has been discussed above. You can link only with libraries (the .lib files). That's why the library wizard refuses to cooperate if you want him to add a .dll. The compiled dll project should contain mat.dll and mat.lib. Link with mat.lib. Check the LIBS directive, it should be the same as above.

    Note on linux-ported things: In linux, the .lib files are the .a files, the dlls are the .so files. You can rename if you need.

  14. #14
    Join Date
    Oct 2013
    Posts
    102
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: How to use external source code in Qt creator?

    Ok, let's discuss the "lib" case please.
    If precalc() is "undeclared" then the header mat.hpp is missing. The error comes from the compiler, not from the linker.
    sorry it was a typo, the error is
    Qt Code:
    1. C:\Users\myuser\Documents\example\example\program\main.cpp:11: error: undefined reference to `precalc(int, int)'
    To copy to clipboard, switch view to plain text mode 
    so it is linking problem? In qt app I tried both
    Qt Code:
    1. LIBS += -L"C:\Users\myuser\Documents\example\example\build-elab-Desktop_Qt_5_1_1_MSVC2010_32bit-Release\release" -lelab
    2. LIBS += "C:\Users\myuser\Documents\example\example\build-elab-Desktop_Qt_5_1_1_MSVC2010_32bit-Release\release\elab.lib"
    To copy to clipboard, switch view to plain text mode 
    and I have
    Qt Code:
    1. INCLUDEPATH += "C:\Users\myuser\Documents\example\example\elab"
    To copy to clipboard, switch view to plain text mode 
    where the mat.h is. Don't know what's missing here

  15. #15
    Join Date
    Apr 2013
    Location
    Prague
    Posts
    258
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    3
    Thanked 65 Times in 59 Posts

    Default Re: How to use external source code in Qt creator?

    Then it is a linker complaint. The source code should be okay (providing mat.h is a part of your project). By mat.hpp I meant mat.h. Now:

    (1) Does the (...)\release directory contain elab.lib?
    (2) Does the elab lib contain precalc()? Use lib /list elab.lib for the check. You should see "precalc".
    (3) Use the second form (simply "path/library.lib") of LIBS. The first form can cause problems. Do not use quotation marks around unless you absolutely must. Also, many software has troubles with blanks in file and directory names. Use rather names without blanks (it's not our case but an experience).
    Last edited by Radek; 6th October 2013 at 15:51.

Similar Threads

  1. Qt Creator Source code export from Qt Creator
    By Gera777 in forum Qt Tools
    Replies: 2
    Last Post: 17th May 2013, 14:21
  2. Source code to *.ui
    By Zergi in forum Qt Tools
    Replies: 3
    Last Post: 28th September 2011, 18:12
  3. no source code
    By banlinhtienphong in forum General Programming
    Replies: 1
    Last Post: 25th July 2011, 17:19
  4. Qt Creator How to make Creator be aware of the Qt source code project?
    By kartwall in forum Qt Tools
    Replies: 5
    Last Post: 27th September 2010, 08:39
  5. Where is the source code?
    By Fletcher in forum Newbie
    Replies: 1
    Last Post: 10th December 2009, 20:45

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.