Menu

How to Access class in a Dll?

2004-05-19
2012-09-26
  • Nobody/Anonymous

    I've make a dll project using Dec-C++ C++ Template,Then I create a Console project to test the dll,I add the dll file created and the associated .a file ,.h file to the Console project's directory,at the Dev-C++ "Project Options" I add the param -llibDll2,then press the button "Add Library or Object",select the file libDll2.a,I rebuild all,
    but error occurs.The following is the building log:
    Compiler: Default compiler
    Building Makefile: "C:\Console1\Makefile.win"
    Executing  make clean
    rm -f main.o  Console1.exe

    g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/include/c++"  -I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  -I"C:/Dev-Cpp/winh"   -gstabs -g3 -O0

    g++.exe main.o  -o "Console1.exe" -L"C:/Dev-Cpp/lib" -L"C:/Dev-Cpp/winlib" -llibDll2 libDll2.a

    C:\Dev-Cpp\Bin\..\lib\gcc-lib\mingw32\3.2\..\..\..\..\mingw32\bin\ld.exe: cannot find -llibDll2

    make.exe: *** [Console1.exe] Error 1

    Execution terminated

    Anybody would give me some advice?

     
    • Georhan

      Georhan - 2004-05-19

      Hi :)
      You don't need to specify both -llibDll2 and "PATH\libDll2.a" to the linker options.
      just delete -llibDll2 and this should work.
      Moreover, if you choose to delete libDll2.a, then :
      - add (I don't know if it's REALLY useful) the directory where this file is in the project options -> directories -> library dierctory
      - specify -lDll2 to the linker option ('lib' mustn't be there).

      Georhan

       
    • Nobody/Anonymous

      Thanx for you reply. I did it as you told me,but new problem appears.

      Compiler: Default compiler
      Building Makefile: "C:\Console1\Makefile.win"
      Executing  make clean
      rm -f main.o  Console1.exe

      g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/include/c++"  -I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  -I"C:/Dev-Cpp/winh"   -gstabs -g3 -O0

      g++.exe main.o  -o "Console1.exe" -L"C:/Dev-Cpp/lib" -L"C:/Dev-Cpp/winlib" libDll2.a

      main.o(.text+0x2d): In function `main':
      C:/Console1/main.cpp:9: undefined reference to `DllClass::DllClass()'

      main.o(.text+0x3c):C:/Console1/main.cpp:10: undefined reference to `DllClass::HelloWorld()'
      main.o(.text+0x5b):C:/Console1/main.cpp:12: undefined reference to `DllClass::~DllClass()'
      main.o(.text+0x72):C:/Console1/main.cpp:12: undefined reference to `DllClass::~DllClass()'

      make.exe: *** [Console1.exe] Error 1

      Execution terminated

      //the define file of dll project,i'v added it into my test console project
      #ifndef _DLL_H_
      #define _DLL_H_

      #if BUILDING_DLL
      # define DLLIMPORT __declspec (dllexport)
      #else /* Not BUILDING_DLL */
      # define DLLIMPORT __declspec (dllimport)
      #endif /* Not BUILDING_DLL */

      class DLLIMPORT DllClass
      {
        public:
          DllClass();
          virtual ~DllClass(void);
          void HelloWorld(void);
        private:

      };

      #endif /* _DLL_H_ */

      //the file to access class in dll.
      #include <iostream>
      #include <stdlib.h>
      #include "dll2.h"

      using namespace std;

      int main(int argc, char *argv[])
      {
        DllClass test;
        test.HelloWorld();
        system("PAUSE");   
        return 0;
      }

       
    • Georhan

      Georhan - 2004-05-19

      Arf the old bugs reappear ! :)
      Just add -DBUILDING_DLL in the C++ compiler options (you find it in the C compiler by default)

      Georhan.

       
    • Nobody/Anonymous

      Georhan,hi,I add the option _DBUILDING_DLL in the C++ compiler options,but it still failed to compile.
      I use Dev-C++ 4.9.8.0, can you help me?

      Compiler: Default compiler
      Building Makefile: "C:\Console1\Makefile.win"
      Executing  make clean
      rm -f main.o  Console1.exe

      g++.exe -c main.cpp -o main.o -I"C:/Dev-Cpp/include/c++"  -I"C:/Dev-Cpp/include/c++/mingw32"  -I"C:/Dev-Cpp/include/c++/backward"  -I"C:/Dev-Cpp/include"  -I"C:/Dev-Cpp/winh" -DBUILDING_DLL   -gstabs -g3 -O0

      g++.exe main.o  -o "Console1.exe" -L"C:/Dev-Cpp/lib" -L"C:/Dev-Cpp/winlib" libDll2.a

      main.o(.text+0x2d): In function `main':
      C:/Console1/main.cpp:9: undefined reference to `DllClass::DllClass()'

      main.o(.text+0x3c):C:/Console1/main.cpp:10: undefined reference to `DllClass::HelloWorld()'
      main.o(.text+0x5b):C:/Console1/main.cpp:12: undefined reference to `DllClass::~DllClass()'
      main.o(.text+0x72):C:/Console1/main.cpp:12: undefined reference to `DllClass::~DllClass()'
      main.o(.data$_ZTV8DllClass+0x8):C:/Console1/main.cpp:8: undefined reference to `DllClass::~DllClass()'
      main.o(.data$_ZTV8DllClass+0xc):C:/Console1/main.cpp:8: undefined reference to `DllClass::~DllClass()'

      make.exe: *** [Console1.exe] Error 1

      Execution terminated

       
    • Liviu Nicolescu

      Liviu Nicolescu - 2004-05-20

      Georhan meant to say to add -DBUILDING_DLL=1 in the C++ compiler options when compiling the dll.

      You will find here how to create/use a simple dll:

      http://sourceforge.net/forum/message.php?msg_id=2535537

      Liviu

       
    • Nobody/Anonymous

      Liviu,Thank you. I'v got it run correctly. In fact, I did like All those of the tutor steps but one,In building the Dll project, in the "Parameters" option tab->c compiler parameter,by default,the -DBUILDING_DLL is set, I moved it into the C++ compiler parameter,rebuild all,rebuild the console,it success. that's really a good tutor for our newbies,thank you a second time.

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.