Page 1 of 2 12 LastLast
Results 1 to 20 of 28

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

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

    Default How to use external source code in Qt creator?

    Hi guys. I'm new to Qt an C++. I started developing an app on Windows platform Qt 5.1.1 Qt Creator v. 2.8.1 and would like to include some external source code in my project. So to learn how to do it, I wrote a simple c program consisting of 3 files, elab.cpp
    Qt Code:
    1. #include <iostream>
    2. #include "mat.h"
    3.  
    4. int sumup(int a, int b) {
    5. return (a + b);
    6. }
    7.  
    8. int main() {
    9. using namespace std;
    10.  
    11. int a = precalc(3,4);
    12. cout << "The sum of 3 and 4 is " << a;
    13.  
    14. int c = sumup(6,2);
    15. cout << "The sum of 6 and 2 is " << c;
    16.  
    17. return 0;
    18. }
    To copy to clipboard, switch view to plain text mode 
    mat.h
    Qt Code:
    1. #ifndef ADD_H
    2. #define ADD_H
    3.  
    4. int precalc(int x, int y);
    5.  
    6. #endif
    To copy to clipboard, switch view to plain text mode 
    and mat.cpp
    Qt Code:
    1. int precalc(int x, int y) {
    2. return (x + y);
    3. }
    To copy to clipboard, switch view to plain text mode 
    If I compile both cpp-s to exe with mingw the program runs ok. Now I wonder how do I include it in my new Qt app. I want for instance invoke my function "precalc" to be precise. So far I tried to alter the pro file by adding lines like
    Qt Code:
    1. SOURCES += "C:\Users\myuser\Documents\elab_c_proj"
    2. INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
    3. LIBS += "C:\Users\myuser\Documents\elab_c_proj"
    4. HEADERS += "C:\Users\myuser\Documents\elab_c_proj"
    To copy to clipboard, switch view to plain text mode 
    I realized that adding just the INCLUDEPATH directive is enough to get access to my function when coding. However the problem arises when I try to build the new qt app, compiler/linker don't know how, tells me somehing like "undefined reference to precalc". Since I'm new to C++ I'm not sure I understand the concept correctly, so my questions are:
    1)what am I missing, doing wrong?
    2)is it enough to just add the source code (cpp and h files) via INCLUDEPATH and the qmake knows how to build my external source code to final qt app binary staticaly?
    3)or must my external app be precompiled first (say outside qt creator) like a lib file so that the qmake knows how to link to it?
    4) can I make a kind of dynamic "dll" from my external app? How would I refer to it in this case from qt app?

    Much thanks in advance for your help,

    best regard, Arcull

  2. The following user says thank you to arcull for this useful post:

    b06 (16th April 2015)

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

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

    SOURCES is a list of source files to build into the project, not a list of paths.
    HEADERS is a list of header files (not paths) in your project that contain Qt specific elements that need to be preprocessed by moc. You can list all your header files without harm though.
    INCLUDEPATH is adapted by qmake and passed to your compiler (via Makefile) so that it knows where to search for header files that are #included in source files but are not immediately to hand.
    LIBS is adapted by qmake and passed to your linker so it knows where to search for libraries and which libraries to search for.

    Of your four values above only INCLUDEPATH is technically correct but unnecessary in this case.

    It is worth reading the qmake tutorial and some of the other stuff in the docs.

    Assuming your three files are in the same directory, then this elab.pro file:
    Qt Code:
    1. TEMPLATE = app
    2. QT -= core
    3. TARGET = elab
    4. SOURCES += elab.cpp mat.cpp
    5. HEADERS += mat.h
    To copy to clipboard, switch view to plain text mode 
    is likely adequate.
    Line 1 is not strictly required as "app" is the default. Line 2 tells qmake not to link Qt libraries (your application is not a Qt app) but normally you are building a Qt application so this would be different. Line 5 is not strictly necessary as mat.h does not require Qt moc pre-processing.

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

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

    Thanks ChrisW67 fro the explanation, however I still have question about this. My intention is to learn to use exteral source code in my qt app, thus the test c++ app with 3 files which are not in the same folder as my qt app.
    Assuming your three files are in the same directory, then this elab.pro file:
    Qt Code:
    Switch view

    TEMPLATE = app
    QT -= core
    TARGET = elab
    SOURCES += elab.cpp mat.cpp
    HEADERS += mat.h
    No they are not, but can I add the sources like you did, but with absolute paths, I mean like this:
    Qt Code:
    1. TEMPLATE = app
    2. QT -= core
    3. TARGET = elab
    4. SOURCES += C:\Users\myuser\Documents\elab_c_proj\elab.cpp C:\Users\myuser\Documents\elab_c_proj\mat.cpp
    5. HEADERS += C:\Users\myuser\Documents\elab_c_proj\mat.h
    To copy to clipboard, switch view to plain text mode 
    As I said till now my qt app had just INCLUDEPATH directive like this (all others like HEADER,LIBS.. were just my tries to make the qt compile)
    Qt Code:
    1. INCLUDEPATH += "C:\Users\myuser\Documents\elab_c_proj"
    To copy to clipboard, switch view to plain text mode 
    because all of the 3 external c++ files were in that directory. And I could access my "precalc" function in design time, however when I tried to build the project, it failed with error "undefined reference to precalc". From this I assume that qmake knows where my external source are, but can't compile them and build/link in my qt app. As I said I'm new to C++ so if you could answer my 3 beginner questions, that would help me a lot, here they go:
    1)is it enough to just add the source code (cpp and h files) via INCLUDEPATH and the qmake knows how to build my external source code to final qt app binary staticaly? Or should I use HEADERS,SOURCES directives as in your example.
    2)As opposite option to 1. question: or must my external app be precompiled first (say outside qt creator) like a lib file so that the qmake knows how to link to it?
    3) can I make a kind of dynamic "dll" from my external app? How would I refer to it in this case from qt app?

    I appreciate your help very much, thanks again.

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

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

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

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

    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.

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

    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.

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

    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.

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

    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.

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

    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.

  11. #10
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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.

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

    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

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

    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

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

    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 

  15. #14
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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

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

    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?

  17. #16
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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

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

    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?

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

    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.

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

    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?

  21. #20
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    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.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.