Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LNK2038 mismatch detected in NativeAOT #85517

Closed
AhmedZero opened this issue Apr 28, 2023 · 9 comments
Closed

LNK2038 mismatch detected in NativeAOT #85517

AhmedZero opened this issue Apr 28, 2023 · 9 comments

Comments

@AhmedZero
Copy link

Description

I build the C# ObjectArx library using NativeAOT for Cad programs like C++, but the library needs two export APIs "ACRXEntryPoint" and "acrxGetApiVersion".

Reproduction Steps

in c++ uses

#if !defined(_WIN64) && !defined (_AC64)
#pragma comment(linker, "/export:_acrxGetApiVersion,PRIVATE")
#else
#pragma comment(linker, "/export:acrxGetApiVersion,PRIVATE")
#endif

I want to make it like C++ in C#

  [DllImport("rxapi", EntryPoint = "acrxGetApiVersionImpl", CallingConvention = CallingConvention.Cdecl)]
        private static extern ulong acrxGetApiVersionImpl();
        [UnmanagedCallersOnly(EntryPoint = "acrxGetApiVersion")]
        public static ulong acrxGetApiVersion()
        {
            return acrxGetApiVersionImpl();
        }

Expected behavior

it should be build successfully.

Actual behavior

rxapi.lib(libinit.obj) : warning LNK4099: PDB '' was not found with 'rxapi.lib(libinit.obj)' or at ''; linking object as if no debug info [N:\My_Projects\asdkmgPoly\asdkmgPoly\asdkmgPoly.csproj]
msvcprt.lib(nothrow.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in Runtime.WorkstationGC.lib(thread.cpp.obj) [N:\My_Projects\asdkmg
Poly\asdkmgPoly\asdkmgPoly.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

i use .NET 7.
Windows 11 X64.

Other information

No response

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Apr 28, 2023
@ghost
Copy link

ghost commented Apr 28, 2023

Tagging subscribers to this area: @agocke, @MichalStrehovsky, @jkotas
See info in area-owners.md if you want to be subscribed.

Issue Details

Description

I build the C# ObjectArx library using NativeAOT for Cad programs like C++, but the library needs two export APIs "ACRXEntryPoint" and "acrxGetApiVersion".

Reproduction Steps

in c++ uses

#if !defined(_WIN64) && !defined (_AC64)
#pragma comment(linker, "/export:_acrxGetApiVersion,PRIVATE")
#else
#pragma comment(linker, "/export:acrxGetApiVersion,PRIVATE")
#endif

I want to make it like C++ in C#

  [DllImport("rxapi", EntryPoint = "acrxGetApiVersionImpl", CallingConvention = CallingConvention.Cdecl)]
        private static extern ulong acrxGetApiVersionImpl();
        [UnmanagedCallersOnly(EntryPoint = "acrxGetApiVersion")]
        public static ulong acrxGetApiVersion()
        {
            return acrxGetApiVersionImpl();
        }

Expected behavior

it should be build successfully.

Actual behavior

rxapi.lib(libinit.obj) : warning LNK4099: PDB '' was not found with 'rxapi.lib(libinit.obj)' or at ''; linking object as if no debug info [N:\My_Projects\asdkmgPoly\asdkmgPoly\asdkmgPoly.csproj]
msvcprt.lib(nothrow.obj) : error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease' in Runtime.WorkstationGC.lib(thread.cpp.obj) [N:\My_Projects\asdkmg
Poly\asdkmgPoly\asdkmgPoly.csproj]

Regression?

No response

Known Workarounds

No response

Configuration

i use .NET 7.
Windows 11 X64.

Other information

No response

Author: AhmedZero
Assignees: -
Labels:

area-NativeAOT-coreclr

Milestone: -

@jkotas
Copy link
Member

jkotas commented Apr 28, 2023

error LNK2038: mismatch detected for 'RuntimeLibrary': value 'MD_DynamicRelease' doesn't match value 'MT_StaticRelease'

The pre-built .NET runtime libraries that are included in native AOT package are built as "static link" (https://learn.microsoft.com/en-us/cpp/c-runtime-library/crt-library-features?view=msvc-170#c-standard-library-stl-lib-files).

The error is caused by ObjectArx library built as "dynamic link" that leads to the mismatch.

Are you able to rebuild the ObjectArx library as "static link"? If you are using Visual Studio, set Properties / C/C++ / CodeGeneration / RuntimeLibrary to "Multi-threaded (/MT)" in your project file properties.

@AhmedZero
Copy link
Author

there is no source code for ObjectArx.

@jkotas
Copy link
Member

jkotas commented Apr 28, 2023

We do not have a great solution for this today. The potential workarounds that I can think of are:

  • Link ObjectArx into a separate .dll library that will get dynamically loaded using standard DllImport by your native AOT compiled .dll.
  • or, build this repo to create your own version of AOT package with matching C++ compiler options (the relevant C++ options are set here
    set(CMAKE_MSVC_RUNTIME_LIBRARY MultiThreaded$<$<AND:$<OR:$<CONFIG:Debug>,$<CONFIG:Checked>>,$<NOT:$<BOOL:$<TARGET_PROPERTY:DAC_COMPONENT>>>>:Debug>)
    ).

#83611 tracks adding an option for compiling the unmanaged runtime with custom C++ compiler options that would help here.

@AhmedZero
Copy link
Author

AhmedZero commented Apr 28, 2023

  • Link ObjectArx into a separate .dll library that will get dynamically loaded using standard DllImport by your native AOT compiled .dll.

there is no DLL for rxapi.

  • or, build this repo to create your own version of AOT package with matching C++ compiler options (the relevant C++ options are set here

That solution is only available.

I hope we get a great solution in the future.

@jkotas
Copy link
Member

jkotas commented Apr 28, 2023

there is no DLL for rxapi.

You can build it yourself.

I hope we get a great solution in the future.

+1. Resolving as duplicate of #83611

@jkotas jkotas closed this as completed Apr 28, 2023
@ghost ghost removed the untriaged New issue has not been triaged by the area owner label Apr 28, 2023
@AhmedZero
Copy link
Author

I hope can build rxapi but there is no source code for rxapi. it's just headers and static library.

@jkotas
Copy link
Member

jkotas commented Apr 28, 2023

You can build it from the static library:

  • Create an empty .dll project
  • Add .def file to the project that list the entrypoints to export from .dll (e.g. acrxGetApiVersionImpl)
  • Add the rxapi static library to the linker inputs of this project.

@AhmedZero
Copy link
Author

I understand, thanks.

@ghost ghost locked as resolved and limited conversation to collaborators May 28, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

2 participants