Skip to content

Instantly share code, notes, and snippets.

@Redchards
Redchards / Makefile
Created September 1, 2015 02:05
Simple makefile aimed to rapid prototyping and testing of C or/and C++ code
# TODO: Add options for library generation.
# Even though this makefile is only intended as testing purpose, I'm not entirely happy with it.
# Indeed, I got multiple evals, and a more or less serious problem :
# the makefile has no "memory", so it does not remember the previous build config.
# The problem is, if we build in a debug mod, and then in a release mod, nothing will be rebuilt.
# To build with another mod, we need to clean the whole obj files, and then build with the desired config.
# One possible fix would be, instead of using target dependent variables for "debug", "release" and "analysis"
# rules, using the $@ automatic variable (name of the target) along with secondary expansion (.SECONDARYEXPANSION:)
# to build the dependency list. This way, to object files would be separated. We would have obj/debug for debug objs,
# and obj/release for release objs.
@Redchards
Redchards / UglyMakefile
Created September 2, 2015 00:46
What an ugly makefile ! But it was damn fun to write :)
# Damn is this makefile ugly ! And buggy tooo !
# But it was sure fun to write :P
# The shell that the make will use to execute shell commands.
# It can be set to any decent shell without any problem.
SHELL:=/bin/bash
# We only define the C++ compiler, and it will take care of compiling both C and C++ files.
# We also set the linker (LD) to the same value, as most of the compilers do it through the same command.
CXX= clang++
@Redchards
Redchards / Makefile
Last active September 3, 2015 06:12
Upgraded and fixed version of the previous makefiles
# This makefile has basically the same goal as the previous ones, but do a greater job.
# Bugs the others had are now fixed ! The compilation for a configuration will not require
# cleaning objs to produce its result.
# PROS : -Easy to use, drop in makefile
# -Quite configurable on its own
# -Manages release and debug mod automatically
# -Supports mix of C and C++ files without further configuration
# CONS : -Bad extensibility
# -Using a sub makefile, so less compact than the previous ones
# -Target dependent variables were not the great idea I thought it would be
@Redchards
Redchards / Makefile
Last active September 4, 2015 18:13
A nice general purpose makefile ideal for testing and rapid prototyping of C and/or C++ language, without having to bother about compilation options too much. And even if one needs to add some options, it could be done easily. Of course, it's not a good idea to use it for bigger projects. Please report any bug you might find !
# This makefile is geared towards begginers and people who want fast prototyping and/or testing,
# and even if not willing to take the time to write a makefile, wants to have a configurable and yet
# solid solution.
# So yes, this makefile is general purpose, but really only useful for small to average projects.
# Indeed, bigger projects will require build mecanism far more advanced than such a simple makefile
# (or beware of everlasting compilation time ...).
# So, even if this makefile should be more than enough in general, there's few caveheats you should
# be aware of.
# First of all, to save typing and hassle, this makefile do a bunch of things "automagically". This, of course,
# includes some overhead (but usually not that much). So if you want really fast iteration time, this may be not
@Redchards
Redchards / File.cxx
Created September 8, 2015 09:33
Simple C++ wrapper around C functions. Sometimes easier to use that C++standard file streaming function.
#include <File.hxx>
File::File(const std::string& pathName, const char* openMode) : fileInfos_(initPath(pathName)),
filePtr_(nullptr),
openMode_(openMode)
{
init();
}
File::File(std::string&& pathName, const char* openMode) : fileInfos_(initPath(std::move(pathName))),
@Redchards
Redchards / isPowerOfTwo
Created September 8, 2015 09:39
I have a bad tendency to not write things. So I tend to forget some nice tricks. I will use this gist to list some of them as I remember/learn them
We will assume for all the following examples that x > 0 (IMPORTANT !)
This little formula will determine if the number is power of two :
(x & -x) == x
How does it works ?
First, let's try to formulate what it means in english :
The only bits that x and -x have set to 1 in common, are the bits
set to 1 in x.
In fact, it's based on a simple observation. Let examine the number 4 :
4 = 00000100b
-4 = ~00000100b + 1b
@Redchards
Redchards / bitExtract.cxx
Created September 8, 2015 09:41
To very small utility functions to extract bits from a number (or similar data type).
#include <iostream>
#include <cstdint>
int64_t number = 0;
int main()
{
while(true)
{
@Redchards
Redchards / AsyncFileReader.cxx
Created September 23, 2015 14:53
Very simple asynchronous file reader and writer, along with a utility file class.
#include <AsyncFileReader.hxx>
AsyncFileReader::~AsyncFileReader()
{
if(th_.joinable())
{
th_.join();
}
}
@Redchards
Redchards / Log.cxx
Last active November 7, 2015 11:04
Extremly simple logging system, using expression templates (inspired by a lightning talk of Marc Eaddy)
#pragma once
#include <map>
#include <iostream>
#include <string>
#include <utility>
// A very simple logging system, shamelessly inspired by :
// https://github.com/CppCon/CppCon2014/blob/master/Lightning%20Talks/Cheap,%20Simple,%20and%20Safe%20Logging%20Using%20Expression%20Templates/Cheap,%20Simple,%20and%20Safe%20Logging%20Using%20Expression%20Templates%20-%20Marc%20Eaddy.pdf
@Redchards
Redchards / this_is_madness.c
Last active October 3, 2015 09:58
One day, a C programmer told me that taking a pointer to int and storing in a pointer to float was just ok. So, this is a demonstration of the desastrous effects of undefined behaviour in the particular case of strict-aliasing rule breaking.
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
// DISCLAIMER : Before telling me that the results are wrong, read the text below !
// This code is a "demonstration" of the desastrous effects of undefined behaviour in the particular case of strict-aliasing rule breaking
// (even if undefined behaviours are always bad of course).
// The results were produced using gcc 5.2.0, with and without optimizations (precised when it differs). So, the results migh change depending