cmake-swig icon indicating copy to clipboard operation
cmake-swig copied to clipboard

Modern CMake C++ SWIG Sample

OS C++ Python Java .NET
Linux Status Status Status Status
macOS Status Status Status Status
Windows Status Status Status Status

Build Status

Introduction

This is a complete example of how to create a Modern CMake C++ Project with the SWIG code generator to generate wrapper and package for Python, .Net and Java.

This project should run on GNU/Linux, MacOS and Windows.

You can find detailed documentation for C++, Swig, Python 3, .Net Core and Java.

note: You should read C++ and Swig first since since other languages are just swig generated wrappers from the C++.

Requirement

You'll need:

  • "CMake >= 3.18".
  • "Python >= 3.6" and python module 'pip' (ed "setuptools" and "wheel" will be auto installed on demand).

Codemap

The project layout is as follow:

  • CMakeLists.txt Top-level for CMake based build.

  • cmake Subsidiary CMake files.

    • python.cmake All internall Python CMake stuff.
    • dotnet.cmake All internall .Net CMake stuff.
    • java.cmake All internall Java CMake stuff.
  • ci Root directory for continuous integration.

  • Foo Root directory for Foo library.

    • CMakeLists.txt for Foo.
    • include public folder.
      • foo
        • Foo.hpp
    • src private folder.
      • src/Foo.cpp
    • python
      • CMakeLists.txt for Foo Python.
      • foo.i SWIG Python wrapper.
    • dotnet
      • CMakeLists.txt for Foo .Net.
      • foo.i SWIG .Net wrapper.
    • java
      • CMakeLists.txt for Foo Java.
      • java/foo.i SWIG Java wrapper.
  • Bar Root directory for Bar library.

    • CMakeLists.txt for Bar.
    • include public folder.
      • bar
        • Bar.hpp
    • src private folder.
      • src/Bar.cpp
    • python
      • CMakeLists.txt for Bar Python.
      • bar.i SWIG Python wrapper.
    • dotnet
      • CMakeLists.txt for Bar .Net.
      • bar.i SWIG .Net wrapper.
    • java
      • CMakeLists.txt for Bar Java.
      • java/bar.i SWIG Java wrapper.
  • FooBar Root directory for FooBar library.

    • CMakeLists.txt for FooBar.
    • include public folder.
      • foobar
        • FooBar.hpp
    • src private folder.
      • src/FooBar.cpp
    • python
      • CMakeLists.txt for FooBar Python.
      • foobar.i SWIG Python wrapper.
    • dotnet
      • CMakeLists.txt for FooBar .Net.
      • foobar.i SWIG .Net wrapper.
    • java
      • CMakeLists.txt for FooBar Java.
      • java/foobar.i SWIG Java wrapper.
  • FooBarApp Root directory for FooBarApp executable.

    • CMakeLists.txt for FooBarApp.
    • src private folder.
      • src/main.cpp
  • python Root directory for Python template files

    • setup.py.in setup.py template for the Python native package.
  • dotnet Root directory for .Net template files

  • java Root directory for Java template files

Dependencies

To complexify a little, the CMake project is composed of three libraries (Foo, Bar and FooBar) with the following dependencies:

Foo:
Bar:
FooBar: PUBLIC Foo PRIVATE Bar

Build

To build the C++ project, as usual:

cmake -S. -Bbuild
cmake --build build

Appendices

Few links on the subject...

Resources

Project layout:

  • The Pitchfork Layout Revision 1 (cxx-pflR1)

CMake:

  • https://llvm.org/docs/CMakePrimer.html
  • https://cliutils.gitlab.io/modern-cmake/
  • https://cgold.readthedocs.io/en/latest/

Python:

.Net:

Issues

Some issue related to this process

Misc

Image has been generated using plantuml:

plantuml -Tsvg docs/{file}.dot

So you can find the dot source files in docs.

License

Apache 2. See the LICENSE file for details.

Disclaimer

This is not an official Google product, it is just code that happens to be owned by Google.