Thread: Linker Issues

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    9

    Linker Issues

    Hi Guys and Girls,

    I am receiving the LNK2019 and LNK2001 errors on my DLL project within Visual C++.

    The compilation is fine, so for now I have assumed the code is "ok". The usual thing to do would be to check the Linker, Input dependencies within the Project Configuration, however I have included the relevant Library here.

    This DLL project includs a header file "xyz.h", the relevant xyz.dll is present in the directory, and the "xyz.lib" file has been added to the Project Configuration.

    There is one method getSomething, that returns an auto_ptr, its prototype is within the "xyz.h" file and has been implemented in the DLL somewhere. Some other methods that are implemented in the DLL with a header file prototype causes no errors when building, it is only this one which is problematic.

    #ifdef BUILD_FOO
    # define FOOAPI __declspec(dllexport)
    #else
    # define FOOAPI __declspec(dllimport)
    #endif

    A macro like the one above is used for the getSomething method and a few others (which do not causes a problem).

    If I create a normal win32 application (native c++) I can use ALL the functions etc from the DLL fine. Only when creating a DLL do I have problems. I want to create a DLL that I can PInvoke from a .NET language.

    I do not have access to the code at the moment, but will provide some code snippets which will make clearer what I am trying to say. Hope someone can shed some light on this matter (although I know its probably difficult without code or the compete Linker error...Thanks!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Those errors generally means something has been declared but not defined. For example, a variable declared extern needs a separate definition, as does a static member of a class. Otherwise all the usages of those variables resolve to something that the linker can't find (hence the complaints from the linker).

    You will probably need to instantiate the auto_ptr<> template for whatever type you're putting in it too.

    The only reason things work when you create a "normal win32 application" is probably because the source of that application is defining things appropriately but you've let those definitions out when compiling the library.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    What do you mean by compiling the library...

    The initial "xyz.lib" was provided to me, along with the corresponding header and dll files. I have not re-created or done anything to these, apart from use them in a win32 project, without any issues/problems.

    I was assuming the same as you, whereby it must be missing the actual implementation, but I was confused as to why it was working with the win32 project, using the same header, dll and lib files.

    Could you expand a little on:

    You will probably need to instantiate the auto_ptr<> template for whatever type you're putting in it too.
    Last edited by The Rainmaker; 11-19-2010 at 04:32 PM.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    My guess is that, when you built the win32 application, you used the source code for the library, not the DLL. So the compiler, when compiling your application code, had direct visibility of all the functions from the DLL - and your executable does not use the DLL.

    You need to read the documentation for the library. You're probably trying to call a function that is not exported (i.e. available for calling by programs that use the DLL).

    Templates are a compile-time mechanism. So when you use auto_ptr, it will be with a type like auto_ptr<Something> or auto_ptr<SomethingElse>, and the compiler needs to do the work of defining both types (as an auto_ptr<Something> is usually a completely different type from auto_ptr<SomethingElse>). One of the names of the process for doing that is template instantiation Care is needed to ensure that the auto_ptr types returned from DLL functions are known to the calling code, since the calling program and the DLL are compiled independently - the templates need to be instantiated both in the DLL and in the calling program. You'll need to read your compiler documentation to work out how to do that across DLL boundaries.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    Hi, Thanks for your time so far.

    The win32 project has no idea about the source code afaik, even I do not know where the source code for that dll is. I was just provided the header, dll and lib, no source. The header interface suggests that the function may be used and it has been, within the win32 project.

    The declaration of the function is as follows (based on memory, no access right now):

    Code:
    #ifdef APISomething 
    # define APISomething __declspec(dllexport)
    #else
    # define APISomething __declspec(dllimport)
    #endif
    
    APISomething auto_ptr<SomeObjectA> randomMethod (auto_ptr<someObjectB>, int numb);

    One thing I may add is that, would it be worth me using the win32 project and setting it up to output a .dll instead of an .exe? Obviously making changes to the code to suite this...and seeing if I can produce a dll this way...The win32 project was set-up by someone else, and I have been working off that. Although i have made sure the Linker dependencies etc are in place, I may be missing some minute detail?

    I will look into template instantiation now, to understand this better.

    Thanks again.

  6. #6
    Registered User
    Join Date
    Nov 2010
    Posts
    9
    Forgot to ask, can you use an OOD when creating a DLL? Or should it just be in one big file?

    EDIT: I feel slightly stupid for asking this, but will leave it...I'm almost certain that it should be allowed.
    Thanks
    Last edited by The Rainmaker; 11-20-2010 at 04:23 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linker question
    By ryanfx in forum C Programming
    Replies: 2
    Last Post: 03-22-2010, 04:20 PM
  2. linker 2019 issues
    By werdy666 in forum C++ Programming
    Replies: 3
    Last Post: 02-25-2009, 04:12 AM
  3. linker
    By George2 in forum C++ Programming
    Replies: 6
    Last Post: 02-23-2008, 01:25 AM
  4. Linker issues....
    By verbity in forum C++ Programming
    Replies: 4
    Last Post: 08-19-2007, 02:08 AM
  5. Linker errors in VC++ 2005
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 05-18-2007, 07:42 AM