dotnet / runtimelab

This repo is for experimentation and exploring new ideas that may or may not make it into the main dotnet/runtime repo.
MIT License
1.42k stars 198 forks source link

errors with static library call #1787

Closed kkptm closed 2 years ago

kkptm commented 2 years ago

[UnmanagedCallersOnly(EntryPoint = "AOT_TEST")] public static int AOT_TEST(int a,int b) { return a + b; }

build->dotnet publish /p:NativeLib=Static /p:SelfContained=true /p:AdditionalCppCompilerFlags=/MTd -r win-x64 -c Release then in cpp:

pragma comment(lib,"advapi32.lib" )

pragma comment(lib,"bcrypt.lib" )

pragma comment(lib,"crypt32.lib" )

pragma comment(lib,"iphlpapi.lib" )

pragma comment(lib,"kernel32.lib" )

pragma comment(lib,"mswsock.lib" )

pragma comment(lib,"ncrypt.lib" )

pragma comment(lib,"normaliz.lib" )

pragma comment(lib,"ntdll.lib" )

pragma comment(lib,"ole32.lib" )

pragma comment(lib,"oleaut32.lib" )

pragma comment(lib,"secur32.lib" )

pragma comment(lib,"user32.lib" )

pragma comment(lib,"version.lib" )

pragma comment(lib,"ws2_32.lib" )

pragma comment(lib,"System.IO.Compression.Native-Static.lib")

pragma comment(lib,"bootstrapperdll.lib" )

pragma comment(lib,"Runtime.ServerGC.lib" )

pragma comment(lib,"NativeLib.lib")

extern "C" int AOT_TEST(int a, int b); int main() { while (true) { cout << AOT_TEST(1,1) << endl; } return 0; } //and copy the libs to cpp.libdir then build whith /MD x64 -> run throw error in AOT_TEST-> Exception raised at 0x00007FF7B48DC2D0 (in cppconsole.exe) : 0xC0000005: Access conflict occurred while reading position 0x0000000000000000.

kkptm commented 2 years ago

is there something wrong with what I did, or can't do it now?

jkotas commented 2 years ago

You also need equivalent of this line: https://github.com/dotnet/runtime/blob/b2dc37ba181a7fa4427e717eab819ba3543d0ae4/src/coreclr/nativeaot/BuildIntegration/Microsoft.NETCore.Native.Windows.props#L83

Explicitly reference CoreRT_StaticInitialization or call the initialization in some other way.

kkptm commented 2 years ago

I tried this and it didn't seem to solve the problem.. I tried this: <Project Sdk="Microsoft.NET.Sdk".>

net5.0 true $(SolutionDir)bin x64 TRACE;TARGET_WINDOWS