mono / CppSharp

Tools and libraries to glue C/C++ APIs to high-level languages
MIT License
3.12k stars 514 forks source link

Question: how can I compile and deploy dll/so compiled from generated cpp files #1576

Open qingfengxia opened 3 years ago

qingfengxia commented 3 years ago

Thank you for bring up such a powerful tool.

I had tried to follow the SDL example to generate bindings for one lib: MOAB OS: Linux Ubuntu 20.04, dotnet core 3.1

I also have difficulty sort out premake5.lua to run SDL example on Ubuntu. so I use dotnet cli to generate bindings (sucessfully, CppSharp is really a powerful tool) and compiling a dummy binding for a simple header when on cpp file are generated.

For more realistic project, CppShart generated some cpp file, but I did not know how to compile into so/dll.

For example, I had generated 3 files but did not know how to compile: MOAB.cs, Std.cs ==> MOAB.dll but failed Std-symbols.cpp ==> I probably know how to compile it into a so file, but how can MOAB.cs use it?

  1. what is the target so/dll name it should be for the generated Std-symbols.cpp, so later C# assembly can find it?
    CppSharp has already got a "libStd-symbols.so", in the folder of CppSharp.Runtime.dll

  2. how to compile Std-symbols.cpp if it is not auto compiled, CppSharp has some cpp header CppSharp.h to include and some so to link with

  3. It seems if I did not compile native cpp code, I will not be able to compile the generated binding cs file?

Is there any tutorial/doc for this kind of situation? I found an issue, mentioned std-symbols. https://github.com/mono/CppSharp/issues/1348

  1. to deploy my bindings: should I just put CppSharp.dll, CppSharp.Runtime.dll, as well as my generated MOAB.dll on assembly loadable path

libCppSharp.CppParser.so , libStd-symbols.so + libMOAB.so (original lib to bind) on LD_LIBRARY_PATH? according to name, libCppSharp.CppParser.so may only needed to generate binding, not needed to run.

CLI binding dll seems depend only on CppSharp.dll and CppSharp.Runtime.dll

Used headers
Used settings

All default passes, no user pass added yet.

Target: gcc

Other settings

Stack trace or incompilable generated code
public static string[] ErrorCodeStr
{
        get
        {
                var __ptr = (__IntPtr*)global::MOAB.__Symbols.MOAB_so._ZN4moab12ErrorCodeStrE;
                return string.__CreateInstance(new __IntPtr(__ptr));
        }
}

error CS0117: 'string' does not contain a definition for '__CreateInstance'

tritao commented 3 years ago
  1. The name should be libStd-symbols.so, but you should compile your own, since the one provided by CppSharp for its own usage may not have all the symbols your binding needs.

  2. CppSharp.h is unrelated, its to be used with C++/CLI generated code. As how to compile, you can use Premake, CMake, Makefile or even a manual GCC command.

  3. The generated native and managed code are separate entities, so you can compile each independently.

  4. You only need CppSharp.Runtime.dll for using the generated bindings and if you prefer you can also embed the runtime code in the bindings DLL if you only have one binding.

It seems your difficulty is mostly in compiling the generated code. You can try to use options.CompileCode = true which will compile your managed code. For native code, I suggest you learn a cross-platform build tool like CMake.

You also posted some error, please create a separate issue and provide more details about the error, including an example C++ header that reproduces the issue.

qingfengxia commented 3 years ago

Great, thank you for prompt reply.

Your reply has clarified a lot, those may go to official doc, which may already there, yet found by me. I may later post a blog on how I did this.

I had compiled my libStd-symbols.so, that is straight-ward, I will remove the libStd-symbols.so ffrom CppSharp parser, from LD_LIBRARY_PATH, and put my newly built libStd-symbols.so to LD_LIBRARY_PATH.

ddobrev commented 3 years ago

I've been working on a feature which uses Clang to automatically compile the generated C++ for you. Its only incomplete piece is MinGW. I'd hoped an upgrade of our used version of it would solve the problem but we cannot do it because of this bug the LLD maintainers have so far refused to do anything about. I hope they can fix the bug soon so that I can finish and merge this feature.

qingfengxia commented 3 years ago

Looking forwards to such a splendid feature, that will surely simply the workflow!

regarding, my compiling error. It seems CppShart try to inject a new method string.__CreateInstance(new __IntPtr(__ptr)); into System.string type, but I have not got it loaded/discovered.

I will file another issue later,

Thank you.

tritao commented 3 years ago

It seems CppShart try to inject a new method string.__CreateInstance(new __IntPtr(__ptr)); into System.string type, but I have not got it loaded/discovered.

We do not try to inject any methods into string, it seems like a generation bug.

kumja1 commented 2 months ago

I've been working on a feature which uses Clang to automatically compile the generated C++ for you. Its only incomplete piece is MinGW. I'd hoped an upgrade of our used version of it would solve the problem but we cannot do it because of this bug the LLD maintainers have so far refused to do anything about. I hope they can fix the bug soon so that I can finish and merge this feature.

@tritao Was this ever implemented?

tritao commented 2 months ago

Yes, its implemented in https://github.com/mono/CppSharp/blob/main/src/Generator/Passes/GenerateSymbolsPass.cs#L52.

CppSharp includes its own Clang compiler and LLD linker in its library, which is used to both compile and link the generated symbols code.

kumja1 commented 2 months ago

Yes, its implemented in https://github.com/mono/CppSharp/blob/main/src/Generator/Passes/GenerateSymbolsPass.cs#L52.

CppSharp includes its own Clang compiler and LLD linker in its library, which is used to both compile and link the generated symbols code.

Good to know