-2

I've been trying to write simple functions in C and use IL2CPP to include them in my plugin directory directly without building DLLs because I find it more straightforward as well as less costly P/Invoke wise as well as maybe not needing to restart Unity each time I make a change.

But when I target IL2CPP scripting back-end w/ standalone target I always get this error in the editor in play mode: EntryPointNotFoundException And when I build I get /il2cpp.exe did not run properly!

I have VS2019 w/ C++ workload and Windows 10 SDK.

Anyone knows how to use native source files and IL2CPP in Unity?

Sample code:

#include <math.h>
#define EXPORT __declspec(dllexport)

/* Power function*/
EXPORT int power1(int n, int p)
{
    int r = pow(n, p);
    return r;
}

C#

[DllImport("__Internal", EntryPoint ="power1")] static extern int power1(int n, int p);

EDIT: Error I get:

Failed running C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp/build/deploy/net471/il2cpp.exe --convert-to-cpp --emit-null-checks --enable-array-bounds-check --dotnetprofile="unityaot" --compile-cpp --libil2cpp-static --platform="WindowsDesktop" --architecture="x86" --configuration="Release" --outputpath="C:\Users\HP\Documents\Unity Projects\C11\Temp/StagingArea/Data\Native\GameAssembly.dll" --cachedirectory="C:\Users\HP\Documents\Unity Projects\C11\Assets\..\Library/il2cpp_cache" --incremental-g-c-time-slice=3 --profiler-report --map-file-parser="C:/Program Files/Unity 2020.1.0a12/Editor/Data/Tools/MapFileParser/MapFileParser.exe" --directory="C:/Users/HP/Documents/Unity Projects/C11/Temp/StagingArea/Data/Managed" --generatedcppdir="C:/Users/HP/Documents/Unity Projects/C11/Temp/StagingArea/Data/il2cppOutput" 

stdout:
Building GameAssembly.dll with MsvcDesktopToolChain
    Msvc Install Version: 15.0
    Msvc Install SDK Directory: C:\Program Files (x86)\Windows Kits\10
    Msvc Linker Path: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x86\link.exe
    Msvc Compiler Path: C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x86\cl.exe

    Output directory: C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\Native
    Cache directory: C:\Users\HP\Documents\Unity Projects\C11\Library\il2cpp_cache
il2cpp.exe didn't catch exception: Unity.IL2CPP.Building.BuilderFailedException: plug.c
C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput\plug.c(4): error C2059: syntax error: 'string'

Invocation was: Executable: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x86\cl.exe"
Arguments: "C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput\plug.c" /nologo /c /bigobj /W3 /Zi /EHs /GR- /Gy /utf-8 /wd4102 /wd4800 /wd4056 /wd4190 /wd4723 /wd4467 /wd4503 /wd4996 /wd4200 /Ox /Oi /Oy- /GS- /Gw /GF /Zo /MT /DNET_4_0 /DUNITY_AOT /DIL2CPP_MONO_DEBUGGER_DISABLED /DGC_NOT_DLL /DRUNTIME_IL2CPP /DIL2CPP_ENABLE_WRITE_BARRIERS=1 /DIL2CPP_INCREMENTAL_TIME_SLICE=3 /DBASELIB_INLINE_NAMESPACE=il2cpp_baselib /D_WIN32 /DWIN32 /DWIN32_THREADS /D_WINDOWS /DWINDOWS /D_UNICODE /DUNICODE /D_CRT_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_WARNINGS /D_WINSOCK_DEPRECATED_NO_WARNINGS /DNOMINMAX /D_NDEBUG /DNDEBUG /DWINDOWS_SDK_BUILD_VERSION=17763 /DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\libil2cpp" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\libil2cpp" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\bdwgc\include" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\xxHash" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\baselib\Include" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\baselib\Platforms\Windows\Include" /I"C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput" /I"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\winrt" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt" /Fo"C:\Users\HP\Documents\Unity Projects\C11\Library\il2cpp_cache\42917C786FF6C7700329B5FBFAD9962E.obj" /Fd"C:\Users\HP\Documents\Unity Projects\C11\Library\il2cpp_cache\42917C786FF6C7700329B5FBFAD9962E.pdb"
EnvArg key: PATH value: C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x86;C:\Program Files (x86)\Windows Kits\10\bin\x86;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x64

   at Unity.IL2CPP.Building.CppProgramBuilder.BuildAllCppFiles(IEnumerable`1 sourceFilesToCompile, IBuildStatisticsCollector statisticsCollector)
   at Unity.IL2CPP.Building.CppProgramBuilder.Build(IBuildStatistics& statistics)
   at il2cpp.Program.DoRun(String[] args, List`1 foundAssemblies)
   at il2cpp.Program.Run(String[] args, Boolean setInvariantCulture)
   at il2cpp.Program.Main(String[] args)
stderr:

Unhandled Exception: Unity.IL2CPP.Building.BuilderFailedException: plug.c
C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput\plug.c(4): error C2059: syntax error: 'string'

Invocation was: Executable: "C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x86\cl.exe"
Arguments: "C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput\plug.c" /nologo /c /bigobj /W3 /Zi /EHs /GR- /Gy /utf-8 /wd4102 /wd4800 /wd4056 /wd4190 /wd4723 /wd4467 /wd4503 /wd4996 /wd4200 /Ox /Oi /Oy- /GS- /Gw /GF /Zo /MT /DNET_4_0 /DUNITY_AOT /DIL2CPP_MONO_DEBUGGER_DISABLED /DGC_NOT_DLL /DRUNTIME_IL2CPP /DIL2CPP_ENABLE_WRITE_BARRIERS=1 /DIL2CPP_INCREMENTAL_TIME_SLICE=3 /DBASELIB_INLINE_NAMESPACE=il2cpp_baselib /D_WIN32 /DWIN32 /DWIN32_THREADS /D_WINDOWS /DWINDOWS /D_UNICODE /DUNICODE /D_CRT_SECURE_NO_WARNINGS /D_SCL_SECURE_NO_WARNINGS /D_WINSOCK_DEPRECATED_NO_WARNINGS /DNOMINMAX /D_NDEBUG /DNDEBUG /DWINDOWS_SDK_BUILD_VERSION=17763 /DWINAPI_FAMILY=WINAPI_FAMILY_DESKTOP_APP /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\libil2cpp" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\libil2cpp" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\bdwgc\include" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\xxHash" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\baselib\Include" /I"C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp\external\baselib\Platforms\Windows\Include" /I"C:\Users\HP\Documents\Unity Projects\C11\Temp\StagingArea\Data\il2cppOutput" /I"C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\include" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\shared" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\um" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\winrt" /I"C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt" /Fo"C:\Users\HP\Documents\Unity Projects\C11\Library\il2cpp_cache\42917C786FF6C7700329B5FBFAD9962E.obj" /Fd"C:\Users\HP\Documents\Unity Projects\C11\Library\il2cpp_cache\42917C786FF6C7700329B5FBFAD9962E.pdb"
EnvArg key: PATH value: C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Windows Kits\10\bin\10.0.17763.0\x86;C:\Program Files (x86)\Windows Kits\10\bin\x86;C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.23.28105\bin\HostX64\x64

   at Unity.IL2CPP.Building.CppProgramBuilder.BuildAllCppFiles(IEnumerable`1 sourceFilesToCompile, IBuildStatisticsCollector statisticsCollector)
   at Unity.IL2CPP.Building.CppProgramBuilder.Build(IBuildStatistics& statistics)
   at il2cpp.Program.DoRun(String[] args, List`1 foundAssemblies)
   at il2cpp.Program.Run(String[] args, Boolean setInvariantCulture)
   at il2cpp.Program.Main(String[] args)

UnityEngine.Debug:LogError(Object)
UnityEditorInternal.Runner:RunProgram(Program, String, String, String, CompilerOutputParserBase)
UnityEditorInternal.Runner:RunManagedProgram(String, String, String, CompilerOutputParserBase, Action`1)
UnityEditorInternal.IL2CPPBuilder:RunIl2CppWithArguments(List`1, Action`1, String)
UnityEditorInternal.IL2CPPBuilder:ConvertPlayerDlltoCpp(Il2CppBuildPipelineData, String, String, Boolean)
UnityEditorInternal.IL2CPPBuilder:Run()
UnityEditorInternal.IL2CPPUtils:RunIl2Cpp(String, IIl2CppPlatformProvider, Action`1, RuntimeClassRegistry)
DesktopStandalonePostProcessor:SetupStagingArea(BuildPostProcessArgs, HashSet`1)
DesktopStandalonePostProcessor:PostProcess(BuildPostProcessArgs)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)

Error 2

Exception: C:\Program Files\Unity 2020.1.0a12\Editor\Data\il2cpp/build/deploy/net471/il2cpp.exe did not run properly!
UnityEditorInternal.Runner.RunProgram (UnityEditor.Utils.Program p, System.String exe, System.String args, System.String workingDirectory, UnityEditor.Scripting.Compilers.CompilerOutputParserBase parser) (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEditorInternal.Runner.RunManagedProgram (System.String exe, System.String args, System.String workingDirectory, UnityEditor.Scripting.Compilers.CompilerOutputParserBase parser, System.Action`1[T] setupStartInfo) (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEditorInternal.IL2CPPBuilder.RunIl2CppWithArguments (System.Collections.Generic.List`1[T] arguments, System.Action`1[T] setupStartInfo, System.String workingDirectory) (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEditorInternal.IL2CPPBuilder.ConvertPlayerDlltoCpp (UnityEditor.Il2Cpp.Il2CppBuildPipelineData data, System.String outputDirectory, System.String workingDirectory, System.Boolean platformSupportsManagedDebugging) (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEditorInternal.IL2CPPBuilder.Run () (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEditorInternal.IL2CPPUtils.RunIl2Cpp (System.String stagingAreaData, UnityEditorInternal.IIl2CppPlatformProvider platformProvider, System.Action`1[T] modifyOutputBeforeCompile, UnityEditor.RuntimeClassRegistry runtimeClassRegistry) (at <71e256980c424823a60822d6a2aa03cd>:0)
DesktopStandalonePostProcessor.SetupStagingArea (UnityEditor.Modules.BuildPostProcessArgs args, System.Collections.Generic.HashSet`1[T] filesToNotOverwrite) (at <71e256980c424823a60822d6a2aa03cd>:0)
DesktopStandalonePostProcessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args) (at <71e256980c424823a60822d6a2aa03cd>:0)
Rethrow as BuildFailedException: Exception of type 'UnityEditor.Build.BuildFailedException' was thrown.
DesktopStandalonePostProcessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args) (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEditor.Modules.DefaultBuildPostprocessor.PostProcess (UnityEditor.Modules.BuildPostProcessArgs args, UnityEditor.BuildProperties& outProperties) (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEditor.PostprocessBuildPlayer.Postprocess (UnityEditor.BuildTargetGroup targetGroup, UnityEditor.BuildTarget target, System.String installPath, System.String companyName, System.String productName, System.Int32 width, System.Int32 height, UnityEditor.BuildOptions options, UnityEditor.RuntimeClassRegistry usedClassRegistry, UnityEditor.Build.Reporting.BuildReport report) (at <71e256980c424823a60822d6a2aa03cd>:0)
UnityEngine.GUIUtility:ProcessEvent(Int32, IntPtr)
S.S. Anne
  • 15,171
  • 8
  • 38
  • 76
Sami Ben
  • 536
  • 7
  • 22

1 Answers1

2

You didn't post the full error that Unity gives you, so I have guess what error you're getting... In either case, this doesn't work because:

  1. C# uses __stdcall calling convention by default, while C/C++ uses __cdecl. Therefore the linker won't be able to find your function at link time and will error out. You need to fix it either on the P/Invoke declaration OR the native definition.

  2. If your source file is a .cpp file, you need to mark your function as extern "C" so that the compiler doesn't mangle its name and so it could be P/Invoked into.

As for EntryPointNotFoundException in the editor: this feature isn't supported in the Unity editor. It's only available for builds made with IL2CPP scripting backend.

Sunius
  • 2,789
  • 18
  • 30
  • Sorry I edited the question to add the errors. – Sami Ben Dec 04 '19 at 22:01
  • Thanks for the reply, yes I found out that feature isn't available in the Editor, as for "extern" I'm only using C but even in C++ with "extern" I still get it. And I tried adding `__stdcall` but it didn't help either. – Sami Ben Dec 04 '19 at 22:05
  • There's a compilation error in your .c file: plug.c(4): error C2059: syntax error: 'string' – Sunius Dec 06 '19 at 00:54
  • It's OK I resolved the issue by building into a clean folder, maybe something erroneous corrupted the folder. – Sami Ben Dec 06 '19 at 19:09