Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use CppSharp

Tags:

c#

cppsharp

I want to use CppSharp to create a C# wrapper for some classes from a C++ library which I intend to use from C# (on a 64 Bit Windows 10 system).

Unfortunately I am absolutely uncertain how to proceed. I cloned CppSharp into my filesystem and built it as described in the 'Getting started' section

https://github.com/mono/CppSharp/blob/main/docs/GettingStarted.md

I also ran all the test successfully.

From what I read in the documentation I was under the impression that to use CppSharp one has to create a C# application (for simplicity a console application), implement an abstract class from CppSharp and execute some commands from CppSharp. From

https://github.com/mono/CppSharp/issues/82

EDIT: Now I use the following code

using CppSharp;
using CppSharp.AST;
using CppSharp.Generators;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CppSharpTransformer
{

    class DllDemoGenerator : ILibrary
    {
        public static void Main(string[] args)
        {
            ConsoleDriver.Run(new DllDemoGenerator());
        }

        void Setup(Driver driver)
        {

            var options = driver.Options;
            options.GeneratorKind = GeneratorKind.CSharp;
            var module = options.AddModule("Sample");
            module.IncludeDirs.Add(@"C:\Users\Boehm.J\Documents\CppSharpSamples\Sample\include");
            module.Headers.Add("Sample.h");
            module.LibraryDirs.Add(@"C:\Users\Boehm.J\Documents\CppSharpSamples\Sample\lib");
            module.Libraries.Add("Sample.lib");

        }

        public void SetupPasses(Driver driver) { }

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

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

        void ILibrary.Setup(Driver driver)
        {
        }
    }
}

which is in Program.cs of my Console application I called CppSharpTransformer.

I could make it compile ok (Visual Studio 2022, Console Project with NET 6.0) by adding the following DLLs to my project:

CppSharp.AST.dll

CppSharp.CLI.dll

CppSharp.CppParser.dll

CppSharp.dll

CppSharp.Generator.dll

CppSharp.Parser.Bootstrap.dll

CppSharp.Parser.CLI.dll

CppSharp.Parser.CSharp.dll

CppSharp.Parser.dll

CppSharp.Parser.Gen.dll

CppSharp.Runtime.dll

I added them under "Dependencies (Abhängigkeiten) > Assemblys" and also as files as members of my project.

But I can not execute the application currently, as I get the runtime error:

Could not load file or assembly 'CppSharp.Parser.CLI, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null'. Das System kann die angegebene Datei nicht finden.

(The german text says "The system can not find the specified file").

But the dll of this name is, it seems, in place. It occurs in

C:\Users\Boehm.J\Documents\work\CppSharpTransformer\CppSharpTransformer

and in

C:\Users\Boehm.J\Documents\work\CppSharpTransformer\CppSharpTransformer\bin\x64\Release\net6.0

like image 387
Jürgen Böhm Avatar asked Nov 28 '25 12:11

Jürgen Böhm


2 Answers

I've just run into the same issue. Make sure you copy the Ijwhost.dll into your application's output path.
For example like this:

<Target Name="CopyNative" AfterTargets="Build">
    <Copy SourceFiles="..\CppSharp\bin\Release_x64\Ijwhost.dll" DestinationFolder="$(OutputPath)" />
</Target>
like image 98
B Ludewig Avatar answered Nov 30 '25 01:11

B Ludewig


This post is quite old, but in case somebody needs it. To make author's code work, it is needed to :

  1. Remove void ILibrary.Setup(Driver driver) { }
  2. Make void Setup(Driver driver) public

As a result it should be:

using CppSharp;
using CppSharp.AST;
using CppSharp.Generators;

namespace CppSharpTransformer
{
class DllDemoGenerator : ILibrary
{
    public static void Main(string[] args)
    {
        ConsoleDriver.Run(new DllDemoGenerator());
    }

    public void Setup(Driver driver)
    {
        var options = driver.Options;
        options.GeneratorKind = GeneratorKind.CSharp;
        var module = options.AddModule("Sample");
        module.IncludeDirs.Add(@"path\to\headers");
        module.Headers.Add("Sample.h");
        module.LibraryDirs.Add(@"path\to\libs");
        module.Libraries.Add("Sample.lib");

    }

    public void SetupPasses(Driver driver) { }

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

    public void Postprocess(Driver driver, ASTContext ctx) { }
}
}
like image 43
Ilya Avatar answered Nov 30 '25 03:11

Ilya