The method of using C/C++ to write the third-party library DLL used by unity

When writing a C++ function library for unity, macros are generally used:

#if defined(BUILD_AS_DLL)
#define API		__declspec(dllexport)
#else
#define API		extern
#endif

This allows a macro BUILD_AS_DLLto toggle whether the interface is exported or not.
When developing, do not add the BUILD_AS_DLL macro, project properties - general - configuration type is set to run the program exe
and then make a bat, add macros to generate dll:

cl /LD /Zi /Od main.c -DBUILD_AS_DLL  /WX -o MAIN.dll 

Among them:
/LD means to generate dll
/Zi means to generate debugging information
/Od means to disable optimization
When compiling a release dll, generally use:

cl /LD /O2 main.c -DBUILD_AS_DLL  /WX -o MAIN.dll 

Put the generated dll in the Assets\Plugins\x86_64 directory of the unity project to use it.

[DllImport("LuaCallCPP")]
public static extern int add(int a, int b);

The generated dll of the debug version can be debugged using vs studio. Attach to the process and find unity.

Guess you like

Origin blog.csdn.net/fztfztfzt/article/details/126811188