#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    24
    Rep Power
    0

    Question A linking error on using member functions in dll . Can anyone with kindness help me?


    I am using Dev-C++ 4.9.8.0 and lambda expression (boost 1.34.1).
    I created a dll containing a class. And want other programs to use the memberfunctions in the class.
    the dll.h is:
    ...........................
    #if BUILDING_DLL
    # define DLLIMPORT __declspec (dllexport)
    #else /* Not BUILDING_DLL */
    # define DLLIMPORT __declspec (dllimport)
    #endif /* Not BUILDING_DLL */
    .............
    class DLLIMPORT classA{
    private:
    ............
    public:
    int function1(const int& );
    ................
    };
    the dll.cpp is:
    # include "dll.h"
    ........................
    int classA::function1(const int& testnumber){
    ......................
    }
    .........................
    In the classA::function1 in dll.cpp I used the lambda expression and the above dll is created OK.
    and the main executable using the dll is:
    # include "dll.h"
    .................................
    int WINAPI WinMain (HINSTANCE hThisInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpszArgument,
    int nFunsterStil)
    ........................
    classA instanceA;
    instanceA.function1(3);
    ......................
    the complier told me no errors but the linker told me that :
    [Linker error] undefined reference to `classA::Function1(int const&)'
    And I do not know how to solve it.

    Can anyone with kindness help me?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    892
    So you have a linker error and you showed us a bunch of source code, but you failed to include any information regarding how you invoked the linker. Nothing obvious seems to be wrong with your sources. I suspect you simply aren't linking to your dll.lib import library. Perhaps you haven't even built one? Have you compiled and linked the DLL yet?
    I no longer wish to be associated with this site.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    24
    Rep Power
    0
    Originally Posted by jwdonahue
    So you have a linker error and you showed us a bunch of source code, but you failed to include any information regarding how you invoked the linker. Nothing obvious seems to be wrong with your sources. I suspect you simply aren't linking to your dll.lib import library. Perhaps you haven't even built one? Have you compiled and linked the DLL yet?
    I used the dll.h and dll.cpp to form a project to produce the dll.
    And the dll.dll is created OK. I really do not know the reason of the linker error and am puzzled about that.
    Can anyone with kindness help me?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    892
    When you built the DLL did you also create an import library? That would something along the lints yourDLLname.lib. That's what you link your program to, not the DLL. The DLL will be loaded for you when your program runs.
    I no longer wish to be associated with this site.
  8. #5
  9. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1807
    Post the entire content of the "Compile Log" tab text in Dev-C++. That will give jwdonahue the information he requested and which you ignored.
  10. #6
  11. No Profile Picture
    Principal Goatkeeper
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2004
    Location
    Galway, Ireland
    Posts
    296
    Rep Power
    183
    I would suggest you research the creation of .lib's etc, and the rest of the process of using DLL's, however:

    Code:
    int function1(const int& );
    
    //....
    
    int classA::function1(const int& testnumber){
     
    //......
    
    classA instanceA;
    instanceA.function1(3);//your linker is prob looking for 
    //void classA::function1(const int&  )
    //from this line(above)
    

    Comments on this post

    • clifford agrees : Well spotted. A reference to a literal constant is not valid.
  12. #7
  13. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1807
    If the following change gets rid of the error:
    Code:
    classA instanceA;
    int x = 3 ;
    instanceA.function1( x );
    the what nogginthenog says applies.

    I can however see no point in passing a const int&. For basic types, simply passing an int will gave the same effect, with one fewer level of indirection.


    Clifford
  14. #8
  15. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2009
    Posts
    24
    Rep Power
    0
    I found that I really did not creat the lib file. and the "Compile Log" tab text in Dev-C++ is
    Compiler: Default compiler
    Building Makefile: "F:\CB2009\Dev-Cpp\myprojects\Makefile.win"
    Executing make...
    make.exe -f "F:\CB2009\Dev-Cpp\myprojects\Makefile.win" all
    g++.exe -c printdll.cpp -o printdll.o -I"F:/CB2009/Dev-Cpp/include/c++" -I"F:/CB2009/Dev-Cpp/include/c++/mingw32" -I"F:/CB2009/Dev-Cpp/include/c++/backward" -I"F:/CB2009/Dev-Cpp/include" -I"F:/CB2009/boost_1_34_1/boost_1_34_1" -I"/F:/CB2009/CodeGear/RAD Studio/6.0/include" -I"/F:/CB2009/CodeGear/RAD Studio/6.0/include/vcl" -I"/F:/CB2009/CodeGear/RAD Studio/6.0/include/dinkumware" -I"F:/CB2009/Dev-Cpp/myprojects"

    dllwrap.exe --output-def libprintdll.def --driver-name c++ --implib libprintdll.a printdll.o -L"F:/CB2009/Dev-Cpp/lib" --no-export-all-symbols --add-stdcall-alias -o printdll.dll

    dllwrap.exe: no export definition file provided.
    Creating one, but that may not be what you want

    Execution terminated
    Compilation successful

    But I do not know how to overcome it now.
    Can you help me? Thanks a lot.
  16. #9
  17. Contributing User

    Join Date
    Aug 2003
    Location
    UK
    Posts
    5,119
    Rep Power
    1807
    Originally Posted by lserjt
    I found that I really did not creat the lib file.
    What do you mean? That log looks perfectly normal to me. In most cases the automatically generated .def file that dllwrap is warning about is fine.

    It will have created a .lib file in the DLL project folder that you then need to link to your executable code.

    However as already pointed out, that is possibly not your problem - you are a couple of posts behind in your responses!

    Also you posted the build log for the DLL build. I wonder why you did not post the log for the code that is actually faiing to build!? Which is rather what I expected/intended, and would be far more helpful!

    Clifford

IMN logo majestic logo threadwatch logo seochat tools logo