Results 1 to 8 of 8

Thread: How to create a Windows dynaminc library looking like C++ libs

  1. #1
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default How to create a Windows dynaminc library looking like C++ libs

    Hello!

    I want to create a dynamic library for one of my softwares in Windows, so I guess that means I'll have to create a .dll.

    The problem is that as far as I know, in order to use a .dll's function inside a Qt App you'll need to create an object of that lib class and than use the methods using that object (e.g.: http://www.youtube.com/watch?v=9JTooLxhmC0), and in fact I would like to create a lib whose functions could be used as the Standart C++ libs are used.

    Just to remember, considering iostream.h, what you need to do is only to include the header file inside you C++ project and than use its functions only needing to declare the lib first:

    #include <iostream>

    //...

    iostream::cout << ...
    and if one wants to not write the "iostream::" all times, he just needs to write:

    using namespace iostream
    (if I'm not mistaken! quite a lot of time since that last time I wrote a pure C++ app!)

    So now all he needs to do is to write the functions normally in his app:

    //some code

    cout << "hahaha";

    //some code

    cin >> var;

    //some code
    So, how can I create a dll in Qt that can do this?


    Thanks,


    Momergil

  2. #2
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create a Windows dynaminc library looking like C++ libs

    pick 'c++ library' when making new project in Qt creator.

    I think you are a bit confused about namespaces, though.
    Qt Code:
    1. #include <iostream>
    2.  
    3. void foo()
    4. {
    5. std::cout << "foo";
    6. }
    7.  
    8. using namespace std;
    9.  
    10. void bar()
    11. {
    12. cout << "bar;"
    13. }
    To copy to clipboard, switch view to plain text mode 

    'using namespace std' is a purely namespace-ey thing. It is not at all related to where cout is in a different library or not.
    Last edited by amleto; 21st August 2012 at 21:27.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

  3. #3
    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 create a Windows dynaminc library looking like C++ libs

    So, how can I create a dll in Qt that can do this?
    Well, strictly speaking, you don't. Qt is a set of libraries not a C++ compiler. What you want is between you, your compiler, and linker.

    You can use Qt's qmake to create a Makefile for you:
    Qt Code:
    1. // project.pro
    2. TEMPLATE = lib
    3. QT -= core gui # no Qt involvement
    4. TARGET = yourlib
    5. SOURCES = yourlib.cpp
    To copy to clipboard, switch view to plain text mode 
    but that's the end of Qt's involvement.

  4. #4
    Join Date
    Jun 2011
    Location
    Porto Alegre, Brazil
    Posts
    482
    Thanks
    165
    Thanked 2 Times in 2 Posts
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Re: How to create a Windows dynaminc library looking like C++ libs

    I think you are a bit confused about namespaces, though.
    Well, not exactly; just brain's memory malfunction ^^
    Anyway, your (amleto) code example is just what I was talking about.

    'using namespace std' is a purely namespace-ey thing. It is not at all related to where cout is in a different library or not.
    The ideia is that by puting "using namespace std', I'm able to use iostream functions without any reference to the library thoose functions came from, but rather just calling them, and I want to do the same thing with my .dll; I don't want to have to create an object from that dll's class in order to use its functions, but just use them!

    Qt Code:
    1. //What I have to do know (given VoidRealms instructional video):
    2. #include "mydll.h"
    3. #include "mydll_global.h"
    4.  
    5. //Include in the .pro file
    6. LIBPATH += mydll.dll
    7.  
    8. //Using
    9. MyDll myobject;
    10. int a = myobject.myFunction();
    11. //...
    12.  
    13. //--------
    14. //What I want:
    15.  
    16. #include "..."
    17. //...
    18.  
    19. //.pro:
    20. //...
    21.  
    22. //source code:
    23. int a = myFunction();
    24. //...
    To copy to clipboard, switch view to plain text mode 

    ChristW67, thanks, but I do want to use Qt's classes to create my .dll (of course that if I want to create a C++ library for generic C++ apps, not necessarely using Qt themselfs, all return types would have to be C++ standart types, not some sort of QString etc., but this is detail). My only doubt here is about not having to create an object from my dll in order to use its functions.

  5. #5
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: How to create a Windows dynaminc library looking like C++ libs

    The ideia is that by puting "using namespace std', I'm able to use iostream functions without any reference to the library thoose functions came from, but rather just calling them, and I want to do the same thing with my .dll;
    No, you are still completely confused. A namespace is not a library. A namespace is a way to put a qualification on something in C++ to distinguish it from something not in the namespace. Some writers of libraries, (like the C++ Standard Library) choose to put the classes and other C++ entities in the library inside a namespace but that is completely optional. Qt is a library (actually, a whole collection of libraries), and by default it does not have a namespace. That's why you can declare a QWidget and don't have to declare a Qt::QWidget.

    For example:

    Qt Code:
    1. namespace NS
    2. {
    3. int foo;
    4. }
    5.  
    6. int foo;
    To copy to clipboard, switch view to plain text mode 

    There are two variable declarations in this code, each of them is unique. One of them is in the namespace "NS" and must be referred to as "NS::foo" and the other one is in the global namespace and is referred to as "::foo" (or, because C++ allows you to omit the namespace qualifier for things in the global namespace, as simply "foo").

    But if you wrote code like this:

    Qt Code:
    1. namespace NS
    2. {
    3. int foo;
    4. }
    5.  
    6. int foo;
    7.  
    8. using namespace NS;
    9.  
    10. foo = 42;
    To copy to clipboard, switch view to plain text mode 

    The compiler would complain because there are two instances of variable "foo" and it can't tell which one you want to assign 42 to.

    I don't want to have to create an object from that dll's class in order to use its functions, but just use them! ... My only doubt here is about not having to create an object from my dll in order to use its functions.
    DLLs are not classes. They are libraries. Libraries might contain definitions of C++ classes, they might not. Most of them do, and you can't use a C++ class defined in a library without creating an instance of it in your own code, unless the library defines an instance of it as a global variable. (For example, the C++ Standard Library defines the global variables std::cout, std::cin, and std::cerr and you can simply use these in your own code).
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  6. #6
    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 create a Windows dynaminc library looking like C++ libs

    Your program is linked to the C++ standard library by default: this has nothing to do with the "using namespace" construct, which is a way to save a bit of typing but does not change the linkage or function of the program.

    Ultimately, if your library contains C++ classes then you have to have an instance of the class to use the class functions. The exception is in the limited case of using class static functions. If your class consists entirely of static functions then you might as well have defined them as free functions in the first place. Your library can create a public instance of your library class for use outside the library, like the standard library does for the standard IO streams, but this is more-or-less limited to singleton type classes.

    You will not be able to use any Qt GUI function on behalf of your parent application without providing a QApplication and servicing its Qt event loop somehow.

  7. #7
    Join Date
    Aug 2012
    Posts
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create a Windows dynaminc library looking like C++ libs

    hello Qt Flocks , i build one application using Qt4.7 i want make it cloud base for testing , kindly suggest me cloud based linux server is best or cloud based windows hosting is best. thanks for your time to guide me on this.

  8. #8
    Join Date
    Sep 2011
    Posts
    1,241
    Thanks
    3
    Thanked 127 Times in 126 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: How to create a Windows dynaminc library looking like C++ libs

    kindly ask your question in your own thread in stead of hijacking an unrelated one.
    If you have a problem, CUT and PASTE your code. Do not retype or simplify it. Give a COMPLETE and COMPILABLE example of your problem. Otherwise we are all guessing the problem from a fabrication where relevant details are often missing.

Similar Threads

  1. Replies: 2
    Last Post: 19th February 2011, 11:26
  2. Replies: 2
    Last Post: 27th May 2010, 15:12
  3. jom / Windows SDK libs linking problem
    By nooky59 in forum Qt Tools
    Replies: 3
    Last Post: 12th November 2009, 14:58
  4. LIBS+= behaviour on Windows
    By Benne Gesserit in forum Newbie
    Replies: 6
    Last Post: 4th August 2008, 21:51
  5. Including libs on windows
    By ucomesdag in forum Qt Programming
    Replies: 2
    Last Post: 17th August 2007, 08:31

Tags for this Thread

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.