Showing posts with label cmake. Show all posts
Showing posts with label cmake. Show all posts

This Week I Learned - 2017 Week 02

Last week post or the whole series.

#1 Continue with CMake again. Not sure why, I'm having lots of fun picking up C++ and enjoying the learning process, although it's quite frustrating when you're stuck with a problem. The weird thing is, is totally different experience with doing web development compare to system programming. Nevertheless, some knowledge gained from using CMake.

As I mentioned before, if you're picking up C++, start with Modern C++, or at least C11 standard. To enable this explicitly in CMake, put this line in your CMakeLists.txt file.
set(CMAKE_CXX_STANDARD 11)

To verify that the setting have been set to compiler.
$ make VERBOSE=1 | grep std
/usr/bin/c++     -std=gnu++11 -o CMakeFiles/stree.dir/stree.cc.o -c /home/ang/src/stree/stree.cc

If you move all your source files into a `src` folder, how do you configure CMake to include these files during compilation? Using GLOB directive.
file(GLOB SOURCES src/*.cpp)
add_executable(progname ${SOURCES})

#2 The C++ 'const' Declaration: Why & How. Definitely a good guide and explanation on understanding and using `const` keyword in C++. The gist of it is `const` prevents alternation but it sometimes caused confusion depends on the context of usages in variables, methods, return values, or function arguments.

#3 It just dawned on me that the sole reason Design Patterns (the book) was written is to collect all the workarounds to the limitation of C++ language itself. For example C++ does not support introspection or dynamic instantiation, hence, a list of creation patterns exists to handle the object creation. What this means is that C++ can't create objects during run-time (like from a string name), everything needs to be defined during compile-time. Or should I use Registry Pattern instead? While we're on the Design Patterns book, much have changed since the book was published. Most of the code example is still using the old C++ way, Modern C++ should make these code cleaner. Furthermore, Iterator was added to Standard Template Library (STL) and there is no need for anyone to implement it manually. I'm slowly beginning to understand why so many programming languages (Java, Rust, Go, and others) were created. Coding in C++ is not, what I would describe, a convenient experiences. I'm still in the early phase of learning, struggling is inevitable, especially you'll need to unlearn quite a number of things from dynamic languages.

#4 ISO standards for programming languages. Stumbled upon this while looking into different standards for software development. Was reading these intensively for the past two months. Seriously, after working for quite some times in development field, didn't realize that these standards exists in the first place. Notable standards are ISO/IEC 14882 (C++), ISO/IEC 9899 (C), ISO/IEC 23270 (C#), and ISO/IEC 30170 (Ruby). Surprise that one particular programming language is notable missing from the list, Java. Did I miss out anything? Nevertheless, standard compliance ensures portability in different operating systems.

Meanwhile, MIL-STD-498, a military standard for system development life-cycle. Superseded by IEEE STD 12207 or ISO/IEC 12007. Yet I'm still enduring the 498 while newer standards already long established. Why? Simply because the documentation is freely available and most academic world still following these syllabus. Standard feel rigid, especially when there are lots of paper works and the development style still following water fall model, very sequentially.

#5 The same thing every single damn year. Generic advice on solving, yet another first word problem, which is just sugarcoating for seeking fortune or fame. Working with younger people have some profound effect on me. How do I regain and reignite my interests on those old hobbies of mine again? You do learn a lot from them but at the same you also see a lot of repeated mistakes. Unfortunately, life moves forward. There are things you must give up as the time or stamina does not permits it anymore. Or it it?

#6 What do you want to learn in 2017 and this is how/now I plan to do it. Yes, another same damn question asked every year, especially the end of the previous year. So many things to try out, so little time. Develop a system and incorporated it into your daily life. Just start? But if you're not finishing, then you're just busy but not productive. Hence, to finish!

This Week I Learned - 2017 Week 01

Last week post or the whole series.

First week of the new year 2017. A busy and slow week. Busy because wrapping up most of the pending to-do list. Slow due to the progress of clearing up the list. When you're overwhelmed by numerous tasks at hand, the best way is still sleep over it. You can't do much with a tired mind, especially at my age now. You can accomplish lots of thing with a good sleep and fresh mind in the wee hours in the morning, roughly around 4:30 a.m.


Did something happened in July? Darker green means more steps and lighter green, the opposite. To the best of my recollection, I've misplaced my pedometer and resolved to use Google's Fit step counter, which is not entirely accurate. Typically offset by half against my pedometer.

#1 Remember about last week post on adding extra options so we compile the sample D source code? The extra options can be added to the `dmd.conf` configuration file. Following these steps to add them to `dmd.conf` file so that you won't need to type them again and again.

Copy the config file to your home directory
$ cp /etc/dmd.conf $HOME

Append the options, `-fPIC -defaultlib=libphobos2.so` to `DFLAGS` variable.
[Environment32]
DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import
-L-L/usr/lib/i386-linux-gnu -L--export-dynamic -fPIC -defaultlib=libphobos2.so

[Environment64]
DFLAGS=-I/usr/include/dmd/phobos -I/usr/include/dmd/druntime/import-L-L/usr/lib/x86_64-linux-gnu
-L--export-dynamic -fPIC -defaultlib=libphobos2.so

#2 Some new concepts in Modern C++. What is this Resource acquisition is initialization (RAII) I kept reading about? It's a programming idiom is about safe resource management and in C++, through an object's lifetime. Resources are initialized and acquired in object's constructor and released through object's destructor. In other words, resources are cleaned up when the scope of code have exit. See the diagram below for further understanding. Other programming languages implemented this idiom through a try/finally block instead through an object's destructor.


Second, is the concept of template. As C++ is a statically typed programming language (type checking is done at compile-time rather than run-time, see this good example), when you declare or initialize any variables, you need to specific a data type. What if you need to pass in a variable to a function which can accept different kinds of numeric data, an integer or and a floating-point number? This is when you will use template to specific a generic type.

Example of the function templates is shown below. You can pass in different kind of data types like integer, floating-number, or character.
#include <iostream>
using namespace std ;
//max returns the maximum of the two elements
template 
T max(T a, T b) {
    return a > b ? a : b ;
}

While we're at Modern C++, there is a list of resources which can get you started as well as the C++11/14/17 feature list. For learning C++ today, the minimum standard you should follow is C++11.

#3 Instead of listening to music while coding, why not listen to development related podcasts like Software Engineering Radio, The Changelog, The Bike Shed, or FLOSS weekly? All this better than some annoying pig squeal and snort noises.

#4 Accidentally messed up my commit dates while testing Git. Need to updates some old commits. However, you have to force push to override the history.
$ git filter-branch --env-filter \
'if [ $GIT_COMMIT = f1d2d2f924e986ac86fdf7b36c94bcdf32beec15 ]
then
    export GIT_AUTHOR_DATE="Sun Jan 7 01:02:03 2017 +0800"
    export GIT_COMMITTER_DATE="Sun Jan 7 01:02:03 2017 +0800"
fi'

$ git push -f origin master

And to remove all the uncommitted files from your project, you can use this Git command. However, be careful as it also remove my Vim's swap files (.swp). Did I forget to set a global path for all Vim's swap files?
$ git clean -d -f -x

#5 As I'm starting to build something in C++, creating and maintaining a Makefile is quite a hassle. Luckily we have CMake which helps let us bootstrap the Makefile fast. One thing I've learned when using CMake is compile and build your program in a specific folder. This will prevent all the temporary files generated by CMake from cluttering your root folder.
$ mkdir build && cd build
$ cmake ../
$ tree -L 1 .
.
├── CMakeCache.txt
├── CMakeFiles
├── cmake_install.cmake
└── Makefile