Table Of ContentC++ Sof tware
Interoperability
for Windows
Programmers
Connecting to C#, R, and Python Clients
—
Adam Gladstone
C++ Software
Interoperability for
Windows Programmers
Connecting to C#, R, and
Python Clients
Adam Gladstone
C++ Software Interoperability for Windows Programmers: Connecting to C#, R, and
Python Clients
Adam Gladstone
Madrid, Spain
ISBN-13 (pbk): 978-1-4842-7965-6 ISBN-13 (electronic): 978-1-4842-7966-3
https://doi.org/10.1007/978-1-4842-7966-3
Copyright © 2022 by Adam Gladstone
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the
material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation,
broadcasting, reproduction on microfilms or in any other physical way, and transmission or information
storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now
known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with
every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an
editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the
trademark.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not
identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to
proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication,
neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or
omissions that may be made. The publisher makes no warranty, express or implied, with respect to the
material contained herein.
Managing Director, Apress Media LLC: Welmoed Spahr
Acquisitions Editor: Jonathan Gennick
Development Editor: Laura Berendson
Coordinating Editor: Jill Balzano
Cover designed by eStudioCalamar
Cover image designed by Freepik (www.freepik.com)
Distributed to the book trade worldwide by Springer Science+Business Media New York, 1 New York Plaza,
Suite 4600, New York, NY 10004-1562, USA. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail
orders-ny@springer-sbm.com, or visit www.springeronline.com. Apress Media, LLC is a California LLC and
the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM
Finance Inc is a Delaware corporation.
For information on translations, please e-mail booktranslations@springernature.com; for reprint,
paperback, or audio rights, please e-mail bookpermissions@springernature.com.
Apress titles may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Print and eBook Bulk Sales
web page at http://www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in this book is available to
readers on GitHub via the book's product page, located at www.apress.com/978- 1- 4842- 7965- 6. For more
detailed information, please visit http://www.apress.com/source- code.
Printed on acid-free paper
Table of Contents
About the Author �����������������������������������������������������������������������������������������������������ix
About the Technical Reviewer ���������������������������������������������������������������������������������xi
Acknowledgments �������������������������������������������������������������������������������������������������xiii
Introduction �������������������������������������������������������������������������������������������������������������xv
Part I: Foundations �����������������������������������������������������������������������������������������1
Chapter 1: Preliminaries �������������������������������������������������������������������������������������������3
Introduction �����������������������������������������������������������������������������������������������������������������������������������3
Prerequisites ���������������������������������������������������������������������������������������������������������������������������������3
How to Use This Book �������������������������������������������������������������������������������������������������������������������4
The Software Interoperability Project �������������������������������������������������������������������������������������������5
The Projects �����������������������������������������������������������������������������������������������������������������������������7
Terminology �����������������������������������������������������������������������������������������������������������������������������8
How the Projects Fit Together ��������������������������������������������������������������������������������������������������9
Summary�������������������������������������������������������������������������������������������������������������������������������������10
Chapter 2: C++ Components and C++ Clients ��������������������������������������������������������11
Introduction ���������������������������������������������������������������������������������������������������������������������������������11
A Tour of the Source Code �����������������������������������������������������������������������������������������������������������11
Descriptive Statistics �������������������������������������������������������������������������������������������������������������12
Linear Regression ������������������������������������������������������������������������������������������������������������������17
The Data Manager �����������������������������������������������������������������������������������������������������������������18
Statistical Tests ���������������������������������������������������������������������������������������������������������������������18
Functions, Classes, and Type Conversion ������������������������������������������������������������������������������23
iii
Table of ConTenTs
C++ Components ������������������������������������������������������������������������������������������������������������������������23
StatsLib ���������������������������������������������������������������������������������������������������������������������������������23
StatsDll ����������������������������������������������������������������������������������������������������������������������������������28
C++ Clients ���������������������������������������������������������������������������������������������������������������������������������30
Introduction ���������������������������������������������������������������������������������������������������������������������������30
StatsConsole ��������������������������������������������������������������������������������������������������������������������������31
StatsViewer ���������������������������������������������������������������������������������������������������������������������������31
Summary�������������������������������������������������������������������������������������������������������������������������������������34
Additional Resources �������������������������������������������������������������������������������������������������������������35
Exercises �������������������������������������������������������������������������������������������������������������������������������������35
Part II: C++/CLI and �NET ������������������������������������������������������������������������������39
Chapter 3: Building a C++/CLI Wrapper �����������������������������������������������������������������41
Introduction ���������������������������������������������������������������������������������������������������������������������������������41
Prerequisites �������������������������������������������������������������������������������������������������������������������������������42
C++/CLI support ��������������������������������������������������������������������������������������������������������������������42
StatsCLR��������������������������������������������������������������������������������������������������������������������������������������43
Project Settings ���������������������������������������������������������������������������������������������������������������������43
Code Organization �����������������������������������������������������������������������������������������������������������������45
The Statistics Class ���������������������������������������������������������������������������������������������������������������46
Type Conversion ��������������������������������������������������������������������������������������������������������������������49
Exception Handling ����������������������������������������������������������������������������������������������������������������53
Testing the Code �������������������������������������������������������������������������������������������������������������������������54
StatsCLR�UnitTests ����������������������������������������������������������������������������������������������������������������54
Managed Wrapper Classes ���������������������������������������������������������������������������������������������������������55
Introduction ���������������������������������������������������������������������������������������������������������������������������55
The DataManager ������������������������������������������������������������������������������������������������������������������56
The TTest Class ����������������������������������������������������������������������������������������������������������������������59
Summary�������������������������������������������������������������������������������������������������������������������������������������62
Additional Resources �������������������������������������������������������������������������������������������������������������63
Exercises �������������������������������������������������������������������������������������������������������������������������������������64
iv
Table of ConTenTs
Chapter 4: C# Clients: Consuming the Managed Wrapper ��������������������������������������67
Introduction ���������������������������������������������������������������������������������������������������������������������������������67
StatsClient �����������������������������������������������������������������������������������������������������������������������������������68
Project Settings ���������������������������������������������������������������������������������������������������������������������68
Installing Accord�NET �������������������������������������������������������������������������������������������������������������68
Code Organization �����������������������������������������������������������������������������������������������������������������68
Using Reflection ��������������������������������������������������������������������������������������������������������������������73
StatsExcel �����������������������������������������������������������������������������������������������������������������������������������78
Installing Excel-DNA ��������������������������������������������������������������������������������������������������������������78
Project Settings ���������������������������������������������������������������������������������������������������������������������79
Exposing Functions to Excel ��������������������������������������������������������������������������������������������������79
Type Conversion ��������������������������������������������������������������������������������������������������������������������81
Build and Run ������������������������������������������������������������������������������������������������������������������������82
Exception Handling ����������������������������������������������������������������������������������������������������������������83
Debugging �����������������������������������������������������������������������������������������������������������������������������85
Summary�������������������������������������������������������������������������������������������������������������������������������������85
Additional Resources �������������������������������������������������������������������������������������������������������������85
Exercises �������������������������������������������������������������������������������������������������������������������������������������86
Part III: R and Rcpp ���������������������������������������������������������������������������������������89
Chapter 5: Building an R Package ��������������������������������������������������������������������������91
Introduction ���������������������������������������������������������������������������������������������������������������������������������91
Prerequisites �������������������������������������������������������������������������������������������������������������������������������92
Rtools ������������������������������������������������������������������������������������������������������������������������������������92
Installing CodeBlocks ������������������������������������������������������������������������������������������������������������92
CodeBlocks ���������������������������������������������������������������������������������������������������������������������������������93
Toolchain Setup ���������������������������������������������������������������������������������������������������������������������93
Project Setup �������������������������������������������������������������������������������������������������������������������������96
R/RStudio Packages ��������������������������������������������������������������������������������������������������������������������99
Background ���������������������������������������������������������������������������������������������������������������������������99
Building a Package with Rcpp ���������������������������������������������������������������������������������������������101
v
Table of ConTenTs
Summary�����������������������������������������������������������������������������������������������������������������������������������110
Additional Resources �����������������������������������������������������������������������������������������������������������111
Exercises �����������������������������������������������������������������������������������������������������������������������������������111
Chapter 6: Exposing Functions Using Rcpp ����������������������������������������������������������113
Introduction �������������������������������������������������������������������������������������������������������������������������������113
The Conversion Layer ����������������������������������������������������������������������������������������������������������������113
Code Organization ���������������������������������������������������������������������������������������������������������������������114
Descriptive Statistics �����������������������������������������������������������������������������������������������������������114
Linear Regression ����������������������������������������������������������������������������������������������������������������120
Statistical Tests �������������������������������������������������������������������������������������������������������������������125
Testing���������������������������������������������������������������������������������������������������������������������������������������129
Measuring Performance �����������������������������������������������������������������������������������������������������������131
Debugging ���������������������������������������������������������������������������������������������������������������������������������134
Distribution Explorer �����������������������������������������������������������������������������������������������������������������135
Summary�����������������������������������������������������������������������������������������������������������������������������������137
Additional Resources �����������������������������������������������������������������������������������������������������������138
Exercises �����������������������������������������������������������������������������������������������������������������������������������138
Part IV: Python ��������������������������������������������������������������������������������������������145
Chapter 7: Building a Python Extension Module ���������������������������������������������������147
Introduction �������������������������������������������������������������������������������������������������������������������������������147
Prerequisites �����������������������������������������������������������������������������������������������������������������������������148
Using Visual Studio Community Edition 2019 ����������������������������������������������������������������������148
StatsPythonRaw ������������������������������������������������������������������������������������������������������������������������149
Project Settings �������������������������������������������������������������������������������������������������������������������149
Code Organization ���������������������������������������������������������������������������������������������������������������150
Functions �����������������������������������������������������������������������������������������������������������������������������150
The Conversion Layer ����������������������������������������������������������������������������������������������������������156
Error Handling ���������������������������������������������������������������������������������������������������������������������160
The Module Definition����������������������������������������������������������������������������������������������������������162
vi
Table of ConTenTs
Python Client �����������������������������������������������������������������������������������������������������������������������������164
Debugging ���������������������������������������������������������������������������������������������������������������������������166
Summary�����������������������������������������������������������������������������������������������������������������������������������167
Additional Resources �����������������������������������������������������������������������������������������������������������167
Exercises �����������������������������������������������������������������������������������������������������������������������������������168
Chapter 8: Module Development with Boost�Python and PyBind �������������������������173
Introduction �������������������������������������������������������������������������������������������������������������������������������173
Boost�Python �����������������������������������������������������������������������������������������������������������������������������173
Prerequisites �����������������������������������������������������������������������������������������������������������������������174
Project Settings �������������������������������������������������������������������������������������������������������������������174
Code Organization ���������������������������������������������������������������������������������������������������������������176
The Conversion Layer ����������������������������������������������������������������������������������������������������������179
The Module Definition����������������������������������������������������������������������������������������������������������180
Exception Handling ��������������������������������������������������������������������������������������������������������������184
PyBind ���������������������������������������������������������������������������������������������������������������������������������������186
Prerequisites �����������������������������������������������������������������������������������������������������������������������186
Project Settings �������������������������������������������������������������������������������������������������������������������186
Code Organization: module�cpp �������������������������������������������������������������������������������������������187
Exception Handling ��������������������������������������������������������������������������������������������������������������194
The Python “Client” �������������������������������������������������������������������������������������������������������������������194
Performance ������������������������������������������������������������������������������������������������������������������������195
The Statistics Service ����������������������������������������������������������������������������������������������������������199
Summary�����������������������������������������������������������������������������������������������������������������������������������202
Additional Resources �����������������������������������������������������������������������������������������������������������202
Exercises �����������������������������������������������������������������������������������������������������������������������������������203
Chapter 9: Conclusion �������������������������������������������������������������������������������������������211
Appendix A: Boost Libraries ���������������������������������������������������������������������������������213
I ntroduction �������������������������������������������������������������������������������������������������������������������������������213
Installation ��������������������������������������������������������������������������������������������������������������������������������213
vii
Table of ConTenTs
Building �������������������������������������������������������������������������������������������������������������������������������������214
References ��������������������������������������������������������������������������������������������������������������������������������214
Appendix B: CMAKE ����������������������������������������������������������������������������������������������215
I ntroduction �������������������������������������������������������������������������������������������������������������������������������215
Project Setup ����������������������������������������������������������������������������������������������������������������������������215
Building the Outputs �����������������������������������������������������������������������������������������������������������������217
References ������������������������������������������������������������������������������������������������������������219
Index ���������������������������������������������������������������������������������������������������������������������221
viii
About the Author
Adam Gladstone is a software developer. He has over
20 years of experience in investment banking, building
software mostly in C++ and C#. For the last few years, he has
been developing data science and machine learning skills,
particularly in Python and R after completing a degree in
maths and statistics. He currently works at Virtu Financial
Inc. in Madrid as an Analyst Programmer. In his free time, he
develops tools for natural language processing.
ix