Creating dll which links another dll - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating dll which links another dll

sideshoresideshore Posts: 2Member
Hi, I'm trying to create a dll which uses a class defined in another dll.

I'm using dev-cpp 4.9.9.2 with the default compiler.
I can use the dll when I'm building an exe file but i'm unable to link this dll when building an dll file.

I've added the library in the linker window in the project options, and included the header file. But in the compiler log I can't find any reference to the library.

log
[code]
Compiler: Default compiler
Building Makefile: "C:Documents and SettingsSideshoreDesktopprogrammerenc++New BoardDesigndll-generalfunctionsMakefile.win"
Executing make clean
rm -f Functions.o GNRFunctions.dll
g++.exe -c Functions.cpp -o Functions.o -I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include" -I"C:/Dev-Cpp/include/c++/3.4.2/backward" -I"C:/Dev-Cpp/include/c++/3.4.2/mingw32" -I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -DBUILDING_DLL=1
Functions.cpp: In function `double Interpolated2d(double**, int, double, int, int, bool)':
Functions.cpp:70: warning: passing `double' for converting 1 of `int abs(int)'
Functions.cpp: In function `int Round(double)':
Functions.cpp:133: warning: converting to `int' from `double'
Functions.cpp:135: warning: converting to `int' from `double'
Functions.cpp: In function `double** GetFrame(double**, int, double**, int, double**, int, double**, int, double**, int, double**, int, double**, int, double**, int, double, int)':
Functions.cpp:295: error: `CBSpline' undeclared (first use this function)
Functions.cpp:295: error: (Each undeclared identifier is reported only once for each function it appears in.)
Functions.cpp:295: error: expected `;' before "Curve"
Functions.cpp:296: error: `Curve' undeclared (first use this function)
Functions.cpp: In function `double** GetFrameSimple(double**, int, double**, int, double**, int, double**, int, double**, int, double**, int, double, int)':
Functions.cpp:537: error: `CBSpline' undeclared (first use this function)
Functions.cpp:537: error: expected `;' before "Curve"
Functions.cpp:538: error: `Curve' undeclared (first use this function)
make.exe: *** [Functions.o] Error 1
Execution terminated
[/code]

I also tried to use Loadlibrary but that doesn't work neither
[code]
HMODULE hMod = LoadLibrary("Curves.dll");
[/code]

Comments

  • LundinLundin Posts: 3,711Member
    I'm no gcc expert, but at least I can tell you that LoadLibrary should work. What do you mean with "doesn't work", do you get an incorrect return value or...? Further, it returns HINSTANCE and not HMODULE, but I suspect they are pretty much the same, so it should work even though a C++ compiler should give you typecast warnings.

    Btw those long function prototypes are really ugly. If you wrote the DLL source you should replace them with a pointer to a struct. That will not only make the code readable but also increase the performance of the program since you don't have to slaughter the stack at every function call.
  • sideshoresideshore Posts: 2Member
    Thank you for your reply!

    Well loadlibrary does load the dll but I wasn't able to use the class from the dll. Yesterday I found this page: http://www.codeproject.com/dll/classesexportedusingLL.asp

    It explains how to use classes from a dll with loadlibrary. But it also warns about it's use because it sort of a hack.

    I much rather link the library implicitly it's much cleaner (in the code) and easier (if it works :-) ).
    But as said earlier I think dev-cpp doesn't add link the dll in the commandline for the compiler. I think it has something to do with the DBUILDING_DLL=1 option.


    I'm not very familiar with structs until now I just made my programs with classes and functions. Just started to use dll's. Can you give an example of a pointer to a struct? (or maybe a link)
  • LundinLundin Posts: 3,711Member
    : Thank you for your reply!
    :
    : Well loadlibrary does load the dll but I wasn't able to use the class from the dll. Yesterday I found this page: http://www.codeproject.com/dll/classesexportedusingLL.asp
    :
    : It explains how to use classes from a dll with loadlibrary. But it also warns about it's use because it sort of a hack.
    :
    : I much rather link the library implicitly it's much cleaner (in the code) and easier (if it works :-) ).
    : But as said earlier I think dev-cpp doesn't add link the dll in the commandline for the compiler. I think it has something to do with the DBUILDING_DLL=1 option.
    :
    :
    : I'm not very familiar with structs until now I just made my programs with classes and functions. Just started to use dll's. Can you give an example of a pointer to a struct? (or maybe a link)
    :


    I don't know how to link it implicitly in gcc but if you wrote the code yourself you could perhaps link it as a .lib instead.

    Classes inside dll:s are probably painful since dll:s were invented before C++. I don't have any experience about that myself, but somekind of wrapper function to access the class sounds safer and cleaner than hackish, inline asm. Also note that the example from Codeproject is using non-standard inline asm. Correct syntax in C++ is

    asm ("NOP");


    ---

    Not sure what you are after when it comes to structs, but here is an example of structs and pointers:

    [code]
    typedef struct
    {
    int x;
    double y;
    } MyStruct;

    ...

    MyStruct ms;
    MyStruct* ptr = &ms;

    ptr->x = something; // access the element pointed at by the struct

    [/code]

    The main difference is that a pointer-to-struct uses the -> operator rather than the . operator. In C++, structs are allowed to use constructors, operator overloading etc making them similar to public classes, though not backwards-compatible with C.
Sign In or Register to comment.