LoginSignup
7
6

More than 5 years have passed since last update.

CppSharp コンパイル (Mac,2017/07/05)

Last updated at Posted at 2015-10-07

monoが開発しているC++のクラスをC#で使用できるようにするツールです。

基本的には以下のとおりです。
https://github.com/mono/CppSharp/blob/master/docs/GettingStarted.md

Mac上でコンパイルしてみます。

CppSharpを取得します。

git clone https://github.com/mono/CppSharp.git

LLVMを取得します。

cd CppSharp/deps
git clone http://llvm.org/git/llvm.git
cd llvm
git checkout cccdd2eff6e04737dfc4a2caf33ea1a6ee5fb80e

Clangを取得します。

cd tools
git clone http://llvm.org/git/clang.git
cd clang
git checkout 6a795889c33eb039866a867b41c30fdf000e345d

buildディレクトリを作成してcmakeでmakefileを作成します。
そしてコンパイルします。

コンパイル済みのLLVM、Clangが公式で配布されているので、配布されている環境を使用している方はそちらを使用したほうが楽です。

cd ../../
mkdir build
cd build

cmake -G "Unix Makefiles" -DLLVM_ENABLE_LIBCXX=true -DLLVM_BUILD_32_BITS=true -DCMAKE_BUILD_TYPE=RelWithDebInfo ..

make

premakeでCppSharpのプロジェクトを作成し、コンパイルします。

cd ../../../build/
./premake5-osx gmake
config=release_x86 make -C gmake

そうすると、gmake/lib/にDLLが生成されます。

生成されたバイナリの

CppSharp.AST.dll
CppSharp.dll
CppSharp.Generator.dll
CppSharp.Parser.dll
CppSharp.Parser.CSharp.dll
CppSharp.Runtime.dll
CSharp.CSharp.dll
libCppSharp.CppParser.dylib
libStd-symbols.dylib

をコードを生成するアプリケーションから参照します。

dylib以外を参照に追加して、以下のコードを記述します。

namespace App
{
    class SampleLibrary : CppSharp.ILibrary
    {
        public void Setup(CppSharp.Driver driver)
        {
            var options = driver.Options;
            options.GeneratorKind = CppSharp.Generators.GeneratorKind.CSharp;
            options.LibraryName = "SampleLib";
            options.Headers.Add(@"絶対パス/Sample.h");
            options.Libraries.Add("Sample.lib");
        }
        public void SetupPasses(CppSharp.Driver driver)
        {
        }

        public void Preprocess(CppSharp.Driver driver, CppSharp.AST.ASTContext ctx)
        {

        }

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

    class Program
    {
        static void Main(string[] args)
        {
            CppSharp.ConsoleDriver.Run(new SampleLibrary());
        }
    }
}

これを実行すると、Sample.hをC#で使用できるようにするコードが生成されます。

ただ、何故かQtSharpは上手く行かなかったので今後調査していきます。

7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6