Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is Cppsharp capable for converting a comprehensive library? #1880

Open
mahdiye-imanpanah opened this issue Nov 13, 2024 · 0 comments
Open

Is Cppsharp capable for converting a comprehensive library? #1880

mahdiye-imanpanah opened this issue Nov 13, 2024 · 0 comments

Comments

@mahdiye-imanpanah
Copy link

mahdiye-imanpanah commented Nov 13, 2024

Hi guys
I am new to Cppsharp, I just tried to convert some simple C++ projects to be used in C# using Cppsharp.
It worked perfectly for those simple and small projects.
My goal is of course converting comprehensive C++ libraries to C#.
For testing, I tried to convert this source to be used in c# projects using Cppsharp(it is a medium size project).

I built it using cmake and visual studio and it write a test program in c++ to make sure the functionality is ok.
After trying to use the Cppsahrp to convert the code using this code:


using CppSharp;
using CppSharp.AST;
using CppSharp.Generators;
using CppSharp.Parser;
using CppSharp.Passes;

public class SampleLibrary : ILibrary
{
    public void Setup(Driver driver)
    {
        var headers = Directory.GetFiles(@"C:\Desktop\Imath\Installl\include\Imath\");

        // Specify options for the binding generation.
        var options = driver.Options;
        options.GeneratorKind = GeneratorKind.CSharp; // Set the output to C#
        options.GenerateClassTemplates = true;
        options.GeneratorKind = GeneratorKind.CSharp;
        options.GenerateClassTemplates = true;
        options.GenerateDebugOutput = true;

        // Specify the module for the sample library.
        var module = options.AddModule("IMath");

        foreach (var file in headers) 
        {
            module.Headers.Add(file);// Specify the header to bind
        }
        module.IncludeDirs.Add(@"C:\Desktop\Imath\Installl\include\Imath\"); // The include directory of the C++ headers
        module.LibraryDirs.Add(@"C:\Desktop\Imath\Installl\bin\"); 
        module.Libraries.Add("Imath-3_2_d.dll"); // If you have a compiled library
        
    }

    public void SetupPasses(Driver driver)
    {
        // Add passes to rename and convert methods if necessary
        driver.Context.TranslationUnitPasses.RenameDeclsUpperCase(RenameTargets.Any);
        driver.Context.TranslationUnitPasses.AddPass(new FunctionToInstanceMethodPass());
        
    }

    public void Preprocess(Driver driver, ASTContext ctx) { }

    public void Postprocess(Driver driver, ASTContext ctx) { }
}
class Program
{  
    static void Main(string[] args)
    {
        ConsoleDriver.Run(new SampleLibrary()); // Start generating the bindings
    }
}

There were some errors about throw in the source code. I commented them, and finally, the program ran successfully.
After running the program there was an error about DllNotFound I copied the DLL manually to the program bin.
The next problem was about BadImageFormatException I changed the platform and this problem was solved too.
After that I faced this error, System.EntryPointNotFoundException: 'Unable to find an entry point named..., based on information in the user manual I tried to add __declspec(dllexport) to the classes I wanted to use like:
template <class T> class __declspec(dllexport) IMATH_EXPORT_TEMPLATE_TYPE Matrix33.
Now the program knows the Matrix33 but there are some problems:
First, I can not see or access some methods which are available in C++.
Second, those methods that are accessible in the c# program don't run and this error appears:
System.DllNotFoundException: 'Unable to load DLL 'IMath' or one of its dependencies: The specified module could not be found. (0x8007007E)'

I have some questions:
First of all, Is Cppsharp helpful for converting comprehensive C++ libraries? or it is just helpful for simple ones???
Second, I found SWIG that is used for converting C++ codes to other languages, is Cppsharp as powerful as SWIG (just for c# use)?
Third, if the answer to the two previous questions is yes, how can I solve these errors??

OS: Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant