Page 2 of 2 FirstFirst 12
Results 21 to 28 of 28

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

  1. #21
    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?

    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.

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

    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

  3. #23
    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?

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

  4. #24
    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?

    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

  5. #25
    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?

    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 16:51.

  6. #26
    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?

    (1) Does the (...)\release directory contain elab.lib?
    It does.
    (2)Does the elab lib contain precalc()? Use lib /list elab.lib for the check. You should see "precalc".
    It probably doesnt. Running the lib /list elab.lib I get just
    Qt Code:
    1. release mat.obj
    To copy to clipboard, switch view to plain text mode 
    So I guess my elab.lib is not ok. How do I fix it in Qt? I open elab project in Qt designer and I have 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 
    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 
    and elab.pro
    Qt Code:
    1. TEMPLATE = lib
    2. CONFIG += staticlib
    3. TARGET = elab
    4. QT -= core
    5. HEADERS = mat.h
    6. SOURCES = mat.cpp
    To copy to clipboard, switch view to plain text mode 
    Shouldn't this build a valid "lib"?
    (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).
    Ok I used the second form, no spaces in path, but I don't think this is the problem.

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

    Ok, I made some progress in using "elab" project as a static lib library.
    (2)Does the elab lib contain precalc()? Use lib /list elab.lib for the check. You should see "precalc".
    It probably doesnt. Running the lib /list elab.lib I get just
    Qt Code:
    1. release mat.obj
    To copy to clipboard, switch view to plain text mode 
    It does, lib.exe doesn't show public symbols in library, you need to run
    Qt Code:
    1. dumpbin.exe /all elab.lib
    To copy to clipboard, switch view to plain text mode 
    to get more info. By the way both tools are in included in MS Visual Studio installation.

    So I went on and tried to compile both (elab and program project) targetting different platforms (mingw, msvc2010,...) and found out that if I build the "elab" with mingw, I get a "libelab.a" file, which I can use in "program" compiled with mingw, while all other combinations (mingw-elab & msvc2010-program,msvc2010-elab & msvc2010-program,msvc2010-elab & mingw-program) do not work. So the actuall problem is not the code of "elab" and "program" but the convetions of calling external libraries. These depend on the compiler/linker, they obviously used some default values in my cases, but googling a bit revealed you can request them somehow. I tried using something like this in my "elab":
    Qt Code:
    1. int __stdcall precalc(int x, int y);
    To copy to clipboard, switch view to plain text mode 
    and
    Qt Code:
    1. int __cdecl precalc(int x, int y);
    To copy to clipboard, switch view to plain text mode 
    in declaration and implementation part of my function, but still no luck. I guess I should give some additional directives to compiler, but don't know how yet. So maybe someone with deeper knowledge in "cross compiling" could explain how to do it properly in somple words.

    Meanwhile I'll try to build an example of using "elab" as a dynamic link library. Thank your for your help so far. Best regards.

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

    Hi, it's time to revive this thread It took me a while to figure out all the missing puzzles and get the things working. So I went and prepared "all"(the ones I know) possible scenarios of external code usage (source code, static linking, dynamic linking with import libraries, dynamic linking with public symbols), in case someone will find it usefull. Bellow are listed the important files of working examples. PS: I renamed previos names of functions to avoid confusion.
    1. source code
      elab.pro
      Qt Code:
      1. TEMPLATE = lib
      2. TARGET = elab
      3. QT -= core
      4. HEADERS = mat.h
      5. SOURCES = mat.cpp
      To copy to clipboard, switch view to plain text mode 
      mat.h
      Qt Code:
      1. #ifndef ADD_H
      2. #define ADD_H
      3.  
      4. class elabclass {
      5. public:
      6. int sumints(int x, int y);
      7. };
      8.  
      9. int multipyints(int x, int y);
      10.  
      11. #endif
      To copy to clipboard, switch view to plain text mode 
      mat.cpp
      Qt Code:
      1. #include "mat.h"
      2.  
      3. int elabclass::sumints(int x, int y) {
      4. return (x + y);
      5. }
      6.  
      7. int multipyints(int x, int y) {
      8. return (x *y);
      9. }
      To copy to clipboard, switch view to plain text mode 
      program.pro
      Qt Code:
      1. TEMPLATE = app
      2. TARGET = program
      3.  
      4. INCLUDEPATH += "C:\Users\myuser\Documents\example_source\example\elab"
      5. SOURCES += "C:\Users\myuser\Documents\example_source\example\elab\mat.cpp" main.cpp
      To copy to clipboard, switch view to plain text mode 
      main.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. int a,b,c;
      11. a = b = c = 0;
      12.  
      13. elabclass myelab;
      14. a = myelab.sumints(3,4);
      15. cout << "sum of 3 and 4 is " << a << endl;
      16.  
      17. b = multipyints(2,5);
      18. cout << "2 times 5 is " << b << endl;
      19.  
      20. c = sumup(6,2);
      21. cout << "sum of 6 and 2 is " << c;
      22.  
      23. return 0;
      24. }
      To copy to clipboard, switch view to plain text mode 
    2. static linking
      elab.pro
      Qt Code:
      1. TEMPLATE = lib
      2. CONFIG += staticlib
      3. TARGET = elab
      4. QT -= core
      5. HEADERS = mat.h
      6. SOURCES = mat.cpp
      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 __cdecl precalc(int x, int y);
      5.  
      6. class elabclass {
      7. public:
      8. int sumints(int x, int y);
      9. };
      10.  
      11. int multiplyints(int x, int y);
      12.  
      13. #endif
      To copy to clipboard, switch view to plain text mode 
      mat.cpp
      Qt Code:
      1. #include "mat.h"
      2.  
      3. int elabclass::sumints(int x,int y) {
      4. return (x + y);
      5. }
      6.  
      7. int multiplyints(int x, int y) {
      8. return (x * y);
      9. }
      To copy to clipboard, switch view to plain text mode 
      program.pro
      Qt Code:
      1. TEMPLATE = app
      2. TARGET = program
      3.  
      4. # So that the compiler will find the mat.h file
      5. INCLUDEPATH += "C:\Users\myuser\Documents\example_static\example\elab"
      6.  
      7. # So that the linker will filnd the link library
      8. LIBS += "C:\Users\myuser\Documents\example_static\example\build-elab-Desktop_Qt_5_1_1_MinGW_32bit-Release\release\libelab.a"
      9. //LIBS += "C:\Users\myuser\Documents\example_static\example\build-elab-Desktop_Qt_5_1_1_MSVC2010_32bit-Release\release\elab.lib"
      10.  
      11. SOURCES += \
      12. main.cpp
      To copy to clipboard, switch view to plain text mode 
      main.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. int a,b,c;
      11. a = b = c = 0;
      12.  
      13. elabclass myelab;
      14. a = myelab.sumints(3,4);
      15. cout << "sum of 3 and 4 is " << a << endl;
      16.  
      17. b = multiplyints(2,5);
      18. cout << "2 times 5 is " << b << endl;
      19.  
      20. c = sumup(6,2);
      21. cout << "The sum of 6 and 2 is " << c << endl;
      22.  
      23. printf("end of program");
      24.  
      25. return 0;
      26. }
      To copy to clipboard, switch view to plain text mode 
    3. dynamic linking with import libs
      elab.pro
      Qt Code:
      1. TEMPLATE = lib
      2. CONFIG += dll
      3. TARGET = elab
      4. QT -= core
      5. HEADERS = mat.h \
      6. elab_global.h
      7. SOURCES = mat.cpp
      8. DEFINES += ELAB
      To copy to clipboard, switch view to plain text mode 
      mat.h
      Qt Code:
      1. #ifndef MAT
      2. #define MAT
      3. #include "elab_global.h";
      4.  
      5. class ELAB_SPEC elabclass {
      6. public:
      7. int sumints(int x, int y);
      8. };
      9.  
      10. ELAB_SPEC int multiplyints(int x, int y);
      11.  
      12. #endif // MAT_H
      To copy to clipboard, switch view to plain text mode 
      mat.cpp
      Qt Code:
      1. #include "mat.h"
      2.  
      3. int elabclass:: sumints (int x, int y) {
      4. return (x + y);
      5. }
      6.  
      7. int multiplyints (int x, int y) {
      8. return(x * y);
      9. }
      To copy to clipboard, switch view to plain text mode 
      program.pro
      Qt Code:
      1. TEMPLATE = app
      2. TARGET = program
      3. SOURCES += main.cpp
      4. INCLUDEPATH += "C:\Users\myuser\Documents\example_dynamic_import-libs\example\elab"
      5. //LIBS += "C:\Users\myuser\Documents\example_dynamic_import-libs\example\build-elab-Desktop_Qt_5_1_1_MSVC2010_32bit-Release\release\elab.lib"
      6. LIBS += "C:\Users\myuser\Documents\example_dynamic_import-libs\example\build-elab-Desktop_Qt_5_1_1_MinGW_32bit-Release\release\libelab.a"
      To copy to clipboard, switch view to plain text mode 
      main.cpp
      Qt Code:
      1. #include <iostream>
      2. #include <QLibrary>
      3. #include <QtDebug>
      4. #include "elab_global.h"
      5. #include "mat.h"
      6.  
      7. int sumup(int a, int b) {
      8. return (a + b);
      9. }
      10.  
      11. int main() {
      12. using namespace std;
      13. int a,b,c;
      14. a = b = c =0;
      15.  
      16. elabclass myelab;
      17. a = myelab.sumints(3,4);
      18. cout << "sum of 3 and 4 is " << a << endl;
      19.  
      20. b = multiplyints(2,5);
      21. cout << "2 times 5 is " << b << endl;
      22.  
      23. c = sumup(6,2);
      24. cout << "sum of 6 and 2 is " << c << endl;
      25.  
      26. return 0;
      27. }
      To copy to clipboard, switch view to plain text mode 
    4. dynamic linking with public symbols
      elab.pro
      Qt Code:
      1. TEMPLATE = lib
      2. CONFIG += dll
      3. TARGET = elab
      4. QT -= core
      5. HEADERS = mat.h \
      6. elab_global.h
      7. SOURCES = mat.cpp
      8. DEFINES += ELAB
      To copy to clipboard, switch view to plain text mode 
      mat.h
      Qt Code:
      1. #ifndef MAT
      2. #define MAT
      3.  
      4. #include "elab_global.h";
      5.  
      6. class ELAB_SPEC elabclass {
      7. public:
      8. elabclass();
      9. int sumints(int x, int y);
      10. };
      11.  
      12. extern "C" {
      13. ELAB_SPEC int multiplyints(int x, int y);
      14. }
      15.  
      16. #endif
      To copy to clipboard, switch view to plain text mode 
      mat.cpp
      Qt Code:
      1. #include "mat.h"
      2.  
      3. extern "C" __declspec(dllexport) int sumints(int x, int y) {
      4. return (x + y);
      5. }
      6.  
      7. extern "C" __declspec(dllexport) int multiplyints (int x, int y) {
      8. return(x * y);
      9. }
      To copy to clipboard, switch view to plain text mode 
      program.pro
      Qt Code:
      1. TEMPLATE = app
      2. TARGET = program
      3. SOURCES += main.cpp
      To copy to clipboard, switch view to plain text mode 
      main.cpp
      Qt Code:
      1. #include <iostream>
      2. #include <QLibrary>
      3. #include <QtDebug>
      4.  
      5. int sumup(int a, int b) {
      6. return (a + b);
      7. }
      8.  
      9. int main() {
      10. using namespace std;
      11. int a = 0;
      12.  
      13. QLibrary library("elab.dll");
      14. if (!library.load()) {
      15. cout << "failed to load elab.dll" << endl;
      16. } else {
      17. cout << "elab.dll loaded ok" << endl;
      18.  
      19. typedef int (*sumintsprototype)(int,int);
      20. sumintsprototype sumcalc = (sumintsprototype) library.resolve("sumints");
      21. if (sumcalc)
      22. a = sumcalc(3,4);
      23.  
      24. if (sumcalc == 0 || a == 0){
      25. qDebug() << library.errorString();
      26. qDebug() << "Cant run the sumints in elab.dll";
      27. } else {
      28. cout << "sum of 3 and 4 is " << a << endl;
      29. }
      30.  
      31. typedef int (*multipLyintsprototype)(int,int);
      32. multipLyintsprototype multycalc = (multipLyintsprototype) library.resolve("multiplyints");
      33. if (multycalc)
      34. a = multycalc(2,5);
      35.  
      36. if (multycalc == 0 || a == 0){
      37. qDebug() << library.errorString() << endl;
      38. qDebug() << "Cant run the multiplyints in elab.dll" <<endl ;
      39. } else {
      40. cout << "2 times 5 is " << a << endl;
      41. }
      42.  
      43. library.unload();
      44.  
      45. }
      46.  
      47. int c = sumup(6,2);
      48. cout << "sum of 6 and 2 is " << c << endl;
      49.  
      50. return 0;
      51. }
      To copy to clipboard, switch view to plain text mode 
    Last edited by arcull; 17th October 2013 at 08:21.

Similar Threads

  1. Qt Creator Source code export from Qt Creator
    By Gera777 in forum Qt Tools
    Replies: 2
    Last Post: 17th May 2013, 15:21
  2. Source code to *.ui
    By Zergi in forum Qt Tools
    Replies: 3
    Last Post: 28th September 2011, 19:12
  3. no source code
    By banlinhtienphong in forum General Programming
    Replies: 1
    Last Post: 25th July 2011, 18: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, 09:39
  5. Where is the source code?
    By Fletcher in forum Newbie
    Replies: 1
    Last Post: 10th December 2009, 21: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.