can not use static library (.lib) which is created by .net?

mc 4,006 Reputation points
2024-06-20T05:49:21.9566667+00:00

I created static library using .net with publishAOT

dotnet publish -r -win-x64 -release /p:NativeLib=Static

then I create dynamic library in c++ (.dll) and want to use this library

but it seems can not .

#pragma comment(lib,"my_lib.lib");

extern __declspec(dllimport) const char* __stdcall get(const char*, const char*);

but I can not use get and there will be error message unresovled link.

User's image

C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,609 questions
0 comments No comments
{count} votes

Accepted answer
  1. Minxin Yu 10,781 Reputation points Microsoft Vendor
    2024-06-20T08:40:37.1+00:00

    Hi, @mc

    Use extern "C".
    __declspec(dllimport) is not needed when loading static lib.

    Currently , there are still a series of problems when using static libraries, so it is better to use dynamic libraries.
    https://github.com/dotnet/runtime/issues/96298

    Improve static library publishing

    Also check the doc:
    https://github.com/dotnet/samples/blob/main/core/nativeaot/NativeLibrary/README.md

    Warning

    It's preferred to build shared libraries than static libraries:

    All code in the loadable module must be compiled with C/C++ compiler version and options that are compatible with native AOT static libraries.

    It's also not possible to mix multiple native AOT compiled static libraries within the same loadable module.

    These problems don't exist when you build a shared library.

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful