Menu

How to export a C++ constructor from a DLL?

2004-10-31
2012-09-26
  • Nobody/Anonymous

    Hi everyone,
    I'm trying to develop a library that enables people to quickly develop games. (Library focusses mainly on sound)
    I need to know how I export a constructor from a DLL. I've tried the following:
    __declspec(dllexport) Sound() { MySound = 0; }
    // Sound is constructor, MySound is private data

    Here is the compile log (rather long):
    Compiler: Default compiler
    Building Makefile: "C:\CPP\TDLSoftLib\DLL\Makefile.win"
    Executing make clean
    rm -f Dll.o Sound.o NumLib.o DLL.dll

    g++.exe -c Dll.cpp -o Dll.o -I"C:/DEV-CPP/include/c++/3.3.1" -I"C:/DEV-CPP/include/c++/3.3.1/mingw32" -I"C:/DEV-CPP/include/c++/3.3.1/backward" -I"C:/DEV-CPP/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/DEV-CPP/include" -I"d:/fmod/api/inc" -fsave-memoized -fexpensive-optimizations -O3 -fmessage-length=0

    g++.exe -c Sound.cpp -o Sound.o -I"C:/DEV-CPP/include/c++/3.3.1" -I"C:/DEV-CPP/include/c++/3.3.1/mingw32" -I"C:/DEV-CPP/include/c++/3.3.1/backward" -I"C:/DEV-CPP/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/DEV-CPP/include" -I"d:/fmod/api/inc" -fsave-memoized -fexpensive-optimizations -O3 -fmessage-length=0

    Sound.cpp:10: warning: `dllexport' attribute ignored

    Sound.cpp:10: error: declaration does not declare anything
    Sound.cpp:10: error: syntax error before `)' token

    Sound.cpp:11: error: ISO C++ forbids defining types within return type
    Sound.cpp:11: error: semicolon missing after declaration of class TDLSoftLib::Sound' Sound.cpp: In functionint TDLSoftLib::Sound(char)':
    Sound.cpp:11: error: MySound' undeclared (first use this function) Sound.cpp:11: error: (Each undeclared identifier is reported only once for each function it appears in.) Sound.cpp:12: error:Length' undeclared (first use this function)
    Sound.cpp: At global scope:
    Sound.cpp:14: error: destructors must be member functions
    Sound.cpp:20: error: non-member function int TDLSoftLib::GetLength()' cannot haveconst' method qualifier
    Sound.cpp:22: error: non-member function int TDLSoftLib::GetChannel()' cannot haveconst' method qualifier
    Sound.cpp: In function int TDLSoftLib::GetChannel()': Sound.cpp:22: error:Channel' undeclared (first use this function)
    Sound.cpp: At global scope:
    Sound.cpp:25: error: non-member function int TDLSoftLib::Pan()' cannot haveconst' method qualifier
    Sound.cpp:27: error: non-member function int TDLSoftLib::Volume()' cannot haveconst' method qualifier
    Sound.cpp:29: error: syntax error before ::' token Sound.cpp:33: error:MySound' was not declared in this scope
    Sound.cpp:33: error: ISO C++ forbids declaration of FSOUND_Stream_SetMode' with no type Sound.cpp:33: error:int FSOUND_Stream_SetMode' redeclared as different kind of symbol
    d:/fmod/api/inc/fmod.h:1014: error: previous declaration of `signed char FSOUND_Stream_SetMode(FSOUND_STREAM
    , unsigned int)'
    Sound.cpp:33: error: initializer list being treated as compound expression

    Sound.cpp:34: error: `Channel' was not declared in this scope

    Sound.cpp:34: error: Pan' was not declared in this scope Sound.cpp:34: error: ISO C++ forbids declaration ofFSOUND_SetPan' with no type
    Sound.cpp:34: error: int FSOUND_SetPan' redeclared as different kind of symbol d:/fmod/api/inc/fmod.h:905: error: previous declaration ofsigned char FSOUND_SetPan(int, int)'

    Sound.cpp:34: error: initializer list being treated as compound expression
    Sound.cpp:35: error: Channel' was not declared in this scope Sound.cpp:35: error:Volume' was not declared in this scope
    Sound.cpp:35: error: ISO C++ forbids declaration of FSOUND_SetVolume' with no type Sound.cpp:35: error:int FSOUND_SetVolume' redeclared as different kind of symbol
    d:/fmod/api/inc/fmod.h:903: error: previous declaration of signed char FSOUND_SetVolume(int, int)' Sound.cpp:35: error: initializer list being treated as compound expression Sound.cpp:36: error:Channel' was not declared in this scope
    Sound.cpp:36: error: ISO C++ forbids declaration of FSOUND_SetPaused' with no type Sound.cpp:36: error:int FSOUND_SetPaused' redeclared as different kind of symbol
    d:/fmod/api/inc/fmod.h:910: error: previous declaration of signed char FSOUND_SetPaused(int, signed char)' Sound.cpp:36: error: initializer list being treated as compound expression Sound.cpp:37: error: syntax error beforereturn'
    Sound.cpp:39: error: ISO C++ forbids declaration of Channel' with no type Sound.cpp:39: error:MySound' was not declared in this scope
    Sound.cpp:40: error: syntax error before `return'

    g++.exe -c NumLib.cpp -o NumLib.o -I"C:/DEV-CPP/include/c++/3.3.1" -I"C:/DEV-CPP/include/c++/3.3.1/mingw32" -I"C:/DEV-CPP/include/c++/3.3.1/backward" -I"C:/DEV-CPP/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/DEV-CPP/include" -I"d:/fmod/api/inc" -fsave-memoized -fexpensive-optimizations -O3 -fmessage-length=0

    dllwrap.exe --output-def libDLL.def --driver-name c++ --implib libDLL.a Dll.o Sound.o NumLib.o -L"C:/DEV-CPP/lib" -L"d:/fmod/api/lib" d:/fmod/api/lib/libfmod.a -fmessage-length=0 -s -o DLL.dll

    C:\DEV-CPP\BIN\dlltool: Unable to open object file: Sound.o

    C:\DEV-CPP\BIN\DLLWRAP.EXE: no export definition file provided.
    Creating one, but that may not be what you want
    C:\DEV-CPP\BIN\DLLWRAP.EXE: C:\DEV-CPP\BIN\dlltool exited with status 1

    Execution terminated

    I'm using DEV-CPP Version 4.9.9.0, using the supplied compiler.
    How can I export the constructor?
    Thanks in advance,
    Peter.
    TDLGames Programmer.
    Visit our website at
    http://www.tdlgames.com

     
    • Nobody/Anonymous

      Hi everyone,
      Since some of the errors in the compile log indicate that some member functions are not member functions, I post the Sound class in its intirity:
      class __declspec(dllexport) Sound {
      FSOUND_STREAM MySound;
      int Length;
      int Channel;
      public:
      Sound() { MySound = 0; }
      Sound(char
      Name) { MySound = FSOUND_Stream_Open(Name, 0, 0, 0);
      Length = FSOUND_Stream_GetLengthMs(MySound);
      }
      ~Sound() { FSOUND_Stream_Close(MySound); }
      void __declspec(dllexport) Init(char Name[]) {
      if (!MySound == 0)
      FSOUND_Stream_Close(MySound);
      MySound = FSOUND_Stream_Open(Name, 0, 0, 0); Length = FSOUND_Stream_GetLengthMs(MySound);
      }
      int __declspec(dllexport) GetLength() const { return Length; }
      int __declspec(dllexport) Play(bool Looping = false, int Pan = 128, int Volume = 128);
      int __declspec(dllexport) GetChannel() const { return Channel; }
      void __declspec(dllexport) Stop() { FSOUND_Stream_Stop(MySound); }
      bool __declspec(dllexport) Pan(int Position) { FSOUND_SetPan(Channel, Position); }
      int __declspec(dllexport) Pan() const { return FSOUND_GetPan(Channel); }
      bool __declspec(dllexport) Volume(int Volume) { FSOUND_SetVolume(Channel, Volume); }
      int __declspec(dllexport) Volume() const { return FSOUND_GetVolume(Channel); }
      };
      // The play function plays the sound. Is not included here.
      Thank you,
      Peter.
      TDLGames Programmer.

       
    • Nobody/Anonymous

      __attribute((dllexport))

       
    • Nobody/Anonymous

      Hmm... It doesn't seem to work - I get the same errors as before... Thanks anyway!
      Peter.

       
    • aditsu

      aditsu - 2004-11-06

      I'm not sure, but is it possible to export the entire class directly instead of exporting every function separately?

      Adrian

       
    • Kip

      Kip - 2004-11-02

      Greetings Peter! Long time no see...er...read. What has transpired? How is your lady friend?

      Kip

       
    • Nobody/Anonymous

      Kip, Lady's fine. ;-)
      Adrian,
      Yes, I found out a way to do it.
      You see, the problem was not with my code, it was with my header file.
      In my header file, I had this only:
      class __declspec(dllimport) Sound;
      I counted on that every piece of code that I wrote in the DLL would get exported, including the constructor. This didn't work. Yet, if I put my constructor in my header file, everything is fine (I've also learned about the use of private functions).
      So, I did get my problem solved. Thanks to everyone for reading/answering.
      Yours sincerely,
      Peter.

       

Log in to post a comment.

MongoDB Logo MongoDB