CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    [RESOLVED] using a dll without the .lib file

    I have a dll that I want to use in Visual C++ but I don't have the library file for it (.lib). When I try to compile I get "Error 2 error LNK1120: 1 unresolved externals"
    Is there any way to use dllimport for this, like I can with C#? Or some other workaround? I have the header with the function prototypes.

    TIA

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: using a dll without the .lib file

    Did you try using GetProcAddress?
    Victor Nijegorodov

  3. #3
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: using a dll without the .lib file

    Just generate the .lib file from the .dll. See https://stackoverflow.com/questions/...ile-from-a-dll See the .bat file(s) that automate this process.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  4. #4
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: using a dll without the .lib file

    Quote Originally Posted by 2kaud View Post
    Just generate the .lib file from the .dll. See https://stackoverflow.com/questions/...ile-from-a-dll See the .bat file(s) that automate this process.
    Thanks for that. So I used the batch file scripts from there and created a .lib file. I added that to my project under the linker's additional dependencies. But when I try to build the project I get unresolved external symbol errors for the DLL functions that I'm calling. Doesn't the lib file tell the compiler to use the DLL? Or do I need to do something else?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: using a dll without the .lib file

    Well, did you try LoadLibrary/GetProcAddress?
    And there is another interesting resource: https://www.xspdf.com/resolution/2263630.html
    Victor Nijegorodov

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: using a dll without the .lib file

    Are you compiling as C or C++? If as C++, are the function declarations for the dll functions specified as extern "C" (capital C) to avoid name 'mangling'? See https://stackoverflow.com/questions/...-extern-c-in-c

    Are you linking dynamically or statically?
    Last edited by 2kaud; February 12th, 2021 at 04:33 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  7. #7
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: using a dll without the .lib file

    Quote Originally Posted by VictorN View Post
    Well, did you try LoadLibrary/GetProcAddress?
    And there is another interesting resource: https://www.xspdf.com/resolution/2263630.html
    I haven't tried that yet. If I can't get the .lib to work, I'll go that route. Thanks for the reference.

  8. #8
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: using a dll without the .lib file

    Quote Originally Posted by 2kaud View Post
    Are you compiling as C or C++? If as C++, are the function declarations for the dll functions specified as extern "C" (capital C) to avoid name 'mangling'? See https://stackoverflow.com/questions/...-extern-c-in-c

    Are you linking dynamically or statically?
    Dynamic linking. The DLL is in the same directory as the .exe (debug). The header file for the dll definitions has the extern "C" -

    #ifdef __cplusplus
    extern "C" {
    #endif

    A typical function prototype looks like this:

    void __cdecl C_2p_nominal(double z[], double theta[], double cc[],
    int32_t C_fpp, int32_t C_out_hex[], double C_out[], int32_t n_samples,
    int32_t n_cc);

    [Are there CODE tags in this forum?]

    Thanks for the help

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: using a dll without the .lib file

    Quote Originally Posted by Dave C View Post
    [Are there CODE tags in this forum?]
    Yes, of course! See https://forums.codeguru.com/misc.php?do=bbcode#code
    Victor Nijegorodov

  10. #10
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: using a dll without the .lib file

    I get unresolved external symbol errors for the DLL functions that I'm calling.
    What's an example of the external symbol that can't be found - and what is called in the .dll?
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  11. #11
    Join Date
    Jun 2001
    Location
    USA
    Posts
    298

    Re: using a dll without the .lib file

    Quote Originally Posted by VictorN View Post
    Well, did you try LoadLibrary/GetProcAddress?
    And there is another interesting resource: https://www.xspdf.com/resolution/2263630.html
    OK, I did that and it worked. Thank you.

  12. #12
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: using a dll without the .lib file

    Quote Originally Posted by Dave C View Post
    OK, I did that and it worked. Thank you.
    You are welcome!
    Victor Nijegorodov

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured