PDA

View Full Version : How to create a Windows dynaminc library looking like C++ libs



Momergil
21st August 2012, 20:35
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 (http://www.youtube.com/watch?v=9JTooLxhmC0&feature=relmfu)), 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

amleto
21st August 2012, 22:23
pick 'c++ library' when making new project in Qt creator.

I think you are a bit confused about namespaces, though.


#include <iostream>

void foo()
{
std::cout << "foo";
}

using namespace std;

void bar()
{
cout << "bar;"
}


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

ChrisW67
22nd August 2012, 02:23
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:


// project.pro
TEMPLATE = lib
QT -= core gui # no Qt involvement
TARGET = yourlib
SOURCES = yourlib.cpp

but that's the end of Qt's involvement.

Momergil
22nd August 2012, 21:48
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!



//What I have to do know (given VoidRealms instructional video):
#include "mydll.h"
#include "mydll_global.h"

//Include in the .pro file
LIBPATH += mydll.dll

//Using
MyDll myobject;
int a = myobject.myFunction();
//...

//--------
//What I want:

#include "..."
//...

//.pro:
//...

//source code:
int a = myFunction();
//...


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.

d_stranz
22nd August 2012, 23:53
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:



namespace NS
{
int foo;
}

int foo;


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:



namespace NS
{
int foo;
}

int foo;

using namespace NS;

foo = 42;


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

ChrisW67
23rd August 2012, 07:20
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.

SAMSON
23rd August 2012, 08:07
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.

amleto
23rd August 2012, 10:21
kindly ask your question in your own thread in stead of hijacking an unrelated one.