- Home /
Include C/C++ source files as plugins in IL2CPP
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);
Answer by JoshPeterson · Dec 03, 2019 at 11:50 AM
See the documentation here: https://docs.unity3d.com/2019.3/Documentation/Manual/WindowsPlayerCPlusPlusSourceCodePluginsForIL2CPP.html
Right. It does work just as with normal pre-compiled DLL plugins. He just forgot the extern "C"
(either as block or local for the method) to ensure C name encoding and not C++ name mangling.
Also keep in $$anonymous$$d that different platforms might have different calling conventions. Though I guess if the file is just included in the compilation process it would use the correct one.
However I guess that testing plugin code inside the editor would not work for such "plugins" since the code is not compiled so it can not be tested inside the editor. The IL2CPP compiler only runs when you create a build. Testing inside the editor is always done through mono.
Finally I can understand that keeping the source files of plugins has some advantages, though it might come with several downsides. I don't know how the IL2CPP backend actually compiles the final native executable, but we know that the C++ compiler is rather slow if you have a lot of native plugin code. That means it could ramp up your building time which actually makes testing much slower.
Extern "C" is for C++ not C, but anyway even with that I still get that error.
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/$$anonymous$$apFileParser/$$anonymous$$apFileParser.exe" --directory="C:/Users/HP/Documents/Unity Projects/C11/Temp/StagingArea/Data/$$anonymous$$anaged" --generatedcppdir="C:/Users/HP/Documents/Unity Projects/C11/Temp/StagingArea/Data/il2cppOutput"
stdout: Building GameAssembly.dll with $$anonymous$$svcDesktopToolChain $$anonymous$$svc Install Version: 15.0 $$anonymous$$svc Install SD$$anonymous$$ Directory: C:\Program Files (x86)\Windows $$anonymous$$its\10 $$anonymous$$svc Linker Path: C:\Program Files (x86)\$$anonymous$$icrosoft Visual Studio\2019\Community\VC\Tools\$$anonymous$$SVC\14.23.28105\bin\HostX64\x86\link.exe $$anonymous$$svc Compiler Path: C:\Program Files (x86)\$$anonymous$$icrosoft Visual Studio\2019\Community\VC\Tools\$$anonymous$$SVC\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'
Your answer
![](https://koobas.hobune.stream/wayback/20220612215534im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Multiple plugins with the same name 0 Answers
How to read a file from OneDrive folder in HoloLens app 0 Answers
How to read uploaded WebGL binary files? 0 Answers