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
    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?

    Quote Originally Posted by Radek View Post
    You can add existing files to the project by right clicking the project folder in the project tree of the Creator. The popup menu contains item "Add Existing Files". Add existing files A few recommendations:

    (1) Keep all project files in some manageable structure. Do not allow project files scattered over your disk. Copy all existing files to subdirectories of your project directory.
    (2) Patch the profile file yourself as little as possible and let the Creator to manage the profile file. Specify TEMPLATE, QT, TARGET, INCLUDEPATH, DEPENDPATH, LIBS. The rest can be done from the Creator: Add files, add existing files, remove files, rename files, resources, forms (a new .ui file -> add existing files).
    Thanks Radek I did try this option as well and the project compiled ok. However my intention is to konw how linking goes "done manualy" and learn from a real example.

    I did a few experiments with directive in "pro" file and here are my findings:
    1) in order to include the source code of my c++ app, the "precalc" function in mat.cpp file, to be precise, I need to add:
    Qt Code:
    1. INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
    To copy to clipboard, switch view to plain text mode 
    by this I get access to my "precalc" function in design time.
    Qt Code:
    1. SOURCES += "C:\Users\myuser\Documents\elab_c_proj\mat.cpp"
    To copy to clipboard, switch view to plain text mode 
    by this qmake compiles "mat.cpp" to "mat.o" and adds it to the my qt project release folder.
    This way the compilation goes ok, however I still need to solve a few puzzles. So I did compile mat.ccp in my "elab_c_proj" folder manualy with mingw which produced "mat.o" file, afterwards I removed the "SOURCES" directive in my qt project and added
    Qt Code:
    1. LIBS += "C:\Users\myuser\Documents\elab_c_proj\mat.o"
    To copy to clipboard, switch view to plain text mode 
    I cleaned the qt project and compiled it just ok. However the "mat.o" file is not copied in my qt project release directory. I guess it is builded in my final exe of qt projcect file, am I right? So I guess I did successfuly staticaly link my "precalc" function. Now the question is how do I link to it dynamically? Thanks again for your help.

  2. #2
    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?

    Most of profile directives are transformed to command line options for compiler and linker. The INCLUDEPATH directories are added to the INCLUDE parameter of the compiler. LIBS is not intended for object files but for user specified libraries (you do not have any in your small project). The contents of LIBS is passed to the linker. Well, you can (ab)use LIBS for passing additional object files. They will be processed by the linker, too. The SOURCES and HEADERS directives specify, which source and header files are known to the Creator. These files will be processed.

    Do not "do it manually", even if you lose your profile file. Compiling your app is not so straightforward. First, a makefile is created or updated. Then, there is MOC compiler which will process GUI classes. Also, resources will be converted to binary files. Then, the makefile will be executed. Updated source files will be compiled and all object files, along with libraries, will be linked to the resulting app, library or dll.

    Write only the "kernel" of the profile file and let the Qt environment to manage the rest. This way, you avoid blunders, linking invalid object files, ignoring needed files and talking about "missing files" and "unresolved references". Note that all files belonging to your project are in the directory tree. If some file isn't there and it should be processed then you need to add the file even if you aren't about to modify it.

    As to the mat.o file: Placing the file on the LIBS directive causes treating mat.o as an external library. Even if you change the source, the old object file from elab_c_proj will be linked. If you add mat.cpp to the project, you get duplicate references most likely: the same public is offered by the mat.o processed during compiling your project and by the old mat.o in elab_c_proj. This is surely nothing you tried to achieve.

  3. #3
    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 for the clarification. So from all said, it is better to add external resource via "Add library" wizzard. But in think this is meant if you do already have a compiled libaray. I my case I don't, in my little C project I have just 3 source files, and the wizzard won't lete me finish adding anything if I don't select a directory containg at lease one "lib" file. Ok, I can play a "smart one" and compile my mat.ccp to mat.o and rename it to mat.lib, but that is probably a bad idea. I see here just 3 options, please correct me if I'm wrong:
    1)I don't use "Add library", but
    a)add the source folder of c project via INCLUDEPATH
    b)add the source (mat.cpp) via SOURCES directive
    This way qmake should always "copy" sources from my folder of C project when compiling and will alway build/link it to final exe of my qt projcet.
    2)I use "Add library", but I should somehow make a valid libaray file first in my C project (honestly, I don't know how, so a simple snippet of command of mingw would be very helpfull). By this linker of qt will always "include" my library in final exe, but if I alter the source of original mat.cpp I must rebuild the library and rebulid the qt project.
    3) I use "Add library" and bulid a dll from my mat.cpp (don't know the command in mingw, so please help). This way I just need to rebuild my source library in case of changes in code, while by qt app remains intact.
    Much thanks again for your effort, regards.

  4. #4
    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 I understand well then mat.cpp pretends a library in your training example. Then you need 2 projects: one for the library mat.lib and one for the app. The library project will contain mat.cpp and a profile with:

    Qt Code:
    1. TEMPLATE = lib
    2. CONFIG += staticlib
    To copy to clipboard, switch view to plain text mode 

    for a static library mat.lib or

    Qt Code:
    1. TEMPLATE = lib
    2. CONFIG += dll
    To copy to clipboard, switch view to plain text mode 

    for a dll. In this case, you get a dll and an import library. Now, the app. It will be

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

    In case of dll, you are linking with the import library. You need to place the dll on your PATH somewhere. Or you can link the dll dynamically with LoadModule() and MakeProcInstance(). In the later case, you will not specify LIBS because the linker has nothing common with your dll.

    As to renaming object files to libs: you cannot. Even if a .lib is a set of object files, it also contains a header with a list of exported things. A linker can survive such renaming but it need not.

  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?

    If I understand well then mat.cpp pretends a library in your training example. Then you need 2 projects: one for the library mat.lib and one for the app. The library project will contain mat.cpp and a profile with:
    Correct, I have two projects. The qt app is the one I'm actively working on, and the other cpp project written with notepad (outside qt designer) is just for the purpose of learning how to link the external source code to my qt project In my qt project I intend to use some opensource code (consisiting of cpp and h files), therefore this little training cpp app.
    The library project will contain mat.cpp and a profile with:

    Qt Code:
    Switch view

    TEMPLATE = lib
    CONFIG += staticlib

    To copy to clipboard, switch view to plain text mode

    for a static library mat.lib or

    Qt Code:
    Switch view

    TEMPLATE = lib
    CONFIG += dll

    To copy to clipboard, switch view to plain text mode

    for a dll. In this case, you get a dll and an import library.
    Very good, now I just need to figure out how to do this outside qt designer, with notepad and mingw. If you have some tutorial link on this subject it would help, but I definitely need to dig deeper into this.

    Now my understanding is, that linking (from qt designer prespective) to a static "lib" library in qt app goes with directive
    Qt Code:
    1. LIBS += full_path/mat.lib
    To copy to clipboard, switch view to plain text mode 
    but to link to a dynamic library "dll" I need the LoadModule() and MakeProcInstance() functions to load my mat.dll, or as an alternative I just add the dll folder to my PATH system variable in windows.
    As to renaming object files to libs: you cannot. Even if a .lib is a set of object files, it also contains a header with a list of exported things. A linker can survive such renaming but it need not.
    Ok I knew it was a bad idea Well, I hope I'm not theorizing too much, but I have an open source cpp code at disposal to include in my qt app and would like to know what are the right approaches to use it. Much thanks for your help and effort.
    Last edited by arcull; 3rd October 2013 at 21:21.

  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?

    No, you use LIBS for either static or dynamic linking of a third party library and nothing changes in the source. If you use the split form of the LIBS variable in the consuming project's pro file then you typically do not need to change it if the consumed library is switched from static to dynamic.
    Qt Code:
    1. TEMPLATE = app
    2. SOURCES += main.cpp
    3. INCLUDEPATH += "/path/to/external/library/includefolder"
    4. LIBS += -L"/path/to/external/library/binfolder" -lname
    To copy to clipboard, switch view to plain text mode 
    See Declaring other libraries

    At run time a DLL must be somewhere Windows can find it.

  7. #7
    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

  8. #8
    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

  9. #9
    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 

  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?

    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

  11. #11
    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?

  12. #12
    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

  13. #13
    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?

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.