Conditional compilation with macros using CMake

As I have described in my previous post, I am working through the 2nd edition of Discovering Modern C++ by out advantage of being able to write the output to file for example… besides sometimes I find it more convenient.

On the other hand, printing “i = 8 in function f” is not something you usually want your program to do, even in debug mode. Deleting or commenting out these statements and then adding them again, was too annoying to me. Besides, this time I have decided to learn CMake, as it seems to be the standard build system at the moment. I also found it really helpful tool for managing files associated with the book as single project and not have dozens of projects for every toy program I write while reading the book.

So, I used CMake to automate the process.

Logging function calls and variable values, is actually easy – it just requires a bit of conditional compilation (similar to Debug/Release mode when using asserts…).

How is it done?

Before every message we want to print, you do something similar to this

#ifdef PRINT_STATEMENTS_ON
// print statements
#endif

When compiling, pass -DPRINT_STATEMENTS_ON flag to the compiler. So far easy. But what happens if we want to make this option available in few projects?

We add the following lines to CMakelists.txt file.

option(WITH_PRINT_STATEMENTS "Print local variables values and invoked function names" OFF)
if(WITH_PRINT_STATEMENTS)
    add_definitions(-DPRINT_STATEMENTS_ON)
endif()

WITH_PRINT_STATEMENTS default value is OFF because printing local values is not the default behavior we want.

To set it on, when invoking add -DWITH_PRINT_FUCNTION_MESSAGES=ON flag when building (or to your configuration, if you use IDE, for example).

In order not add the folders that do not need this functionality, put

if(NOT WITH_PRINT_STATEMENTS)
# Add subdirectories of projects that do not need this functionality
# Or add executables without this functionality
endif()

Perhaps, there might be better ways to do this (feel free to write in comments), however this one was simple to implement and understand.

Hope it helps someone.

Avalonia 11 is out!

A week ago, Avalonia 11.0 has been released. These are exciting news, as stable, cross-platform, first-class GUI dream for .NET has finally become a reality… or at least getting close to reality.

My plan is to go over available XAML framework tutorials/guides and try to convert them to Avalonia, and see how it works. I hope to include materials from Computer Graphics: Principles and Practice, since the book used WPF before, which made it mostly usable on Windows.

We’ll see how it goes. I plan to document my findings here.

Until next time.

Working through 2nd edition of Discovering Modern C++

It has been a while since I’ve wrote C++ code or any serious code, as a matter of a fact, being busy finishing my mathematics studies. As my graduate studies are almost over, after some reflection on what I want do when they are over, I have decided to get back to programming.

I had some experience with several languages C, C++, C#, F#, Java, Kotlin and Python, and played a little with OCaml and Haskell during my flirting attempts with programming, however I have decided that I should stop chasing languages, pick one (okay may be two or three, but not more than that for now) and focus on other things. I am a bit grown up and have some programming experience, easy to learn/cleanly designed/fun to use were not my main concerns for focusing on the language – as I my goal was to use my programming in future work and/or research, I needed to learn the languages commonly used in the domains that interest me, which are mathematical software, high performance computing, machine learning and statistics… and I still have hopes to make games one day (I have few great ideas… at least I think they are great). There are few options available, but C and C++ is still the language that makes most of these work behind the scenes (Rust is cool, but it is not ready in my opinion yet and so is Julia) and I had some experience with these two. So I decided to pick with C++.

I needed a refresher book (I like books) which would also be somehow updated to the recent standards – C++17/20. However it had to be short and to the point on one hand and cover enough material in (mostly) correct on the other. After some thinking I have decided that Discovering Modern C++, 2nd Edition by Peter Gottschling was right for me.

I am reading through middle of it, and I will write detailed review and summary after I am done reading it.

How to share code on your blog using github.

Suppose that you have a piece of code that you want to share on your blog (or other source). If you have a github account, than you can easily share it.

Go to gist.gitbub.com and type the piece of code you want to share. Then, just copy and paste the address. The end result is something like this.

#include <iostream>
int main()
{
std::cout << "Hello world!\n";
return 0;
}
view raw helloworld.cpp hosted with ❤ by GitHub

That’s all. A full explanation for wordpress sites can be found here https://wordpress.com/support/gist/.

Happy coding!

I really like F#,.. but I will focus on C++ for now – an update.

While I really like F#, for now it seems I will focus on C++ in the next few months. Here are few reasons, why I think it is the right thing to do.

F# is a great language, however, if you want to build “cool stuff” (high performance things that drive the world these days). with it, you need both good knowledge of F# and the knowledge of the “cool stuff”. While F# is not that hard to learn, in my opinion, at the moment I hit the wall, because my knowledge of the “cool stuff” (OpenGL, Vulkan, DirectX, CUDA, systems programming, native number crunching) is lacking at the moment… The languages I need for it is C++ and C. The major languages for data science are Python and R (with C++ backend)… So at the moment, trying to do these things in F# will just increase my learning time.

As for the language itself – while it is popular to bash C++ these days, it is not as bad language as it is portrayed. Yeah, it is complex, and error-prone if you do not know what you are doing, but so is advanced programming in general. On the other hand, modern C++ is fast, expressive and has many features that make programming easier. Not to mention great tooling it has (sorry F#).

So, next months it is going to be C++ mostly. And then we’ll see how we continue.

That’s all for now.

Algorithms in C# and F#

I hope it is not a big secret to ones reading, but in order to be more effective as F# programmer, one needs better knowledge of .net platform itself. However, at the moment of writing, most of focus in .net as aimed at C# and not F#. So basically, understanding .net requires good grasp of C#. To add more, most of libraries written for .net, including the “cool” ones are mostly implemented in C#. So, to use them more effectively (and perhaps contribute in future), C# knowledge helps as well. To sum up – to be better F# programmer one needs to know C# as well.

So in order to stay sharp, and not forget C#, I am working on going through Algorithms by Wayne and Sedgewick and attempt to reimplement the examples and do the exercises in C# and F#. This will allow me to compare F# and C# (performance, easy to write) and also ot understand better the capabilities and limitations of F# when used as imperative language (while it is functional first, it is “multi-paradigm” after all).

That’s all for now. Stay tuned!

Investigating F# as Python replacement. An experiment

Partly inspired by the talk “F# As Better Python” by Philippe Carter, I have decided to check how far one can go with F# using it in the scenarios and domains in which Python is popular – scripting, automation, data science, machine learning, web programming, prototyping and teaching programming, and see how it goes.

The work with something concrete, the first two projects will be going through “Automate the boring stuff with python” and “Learning scientific programming with python” and see how well F# does. If things go well, this project will continue and I will try to go over “Hands on machine-learning” and see far I can go with F#.

Wish me luck and stay tuned!