Tuesday, 26 April 2016

GNU C/C++14 Installation & Codeblocks 16.01 from Source (Command Line)

In yesterdays post I explained a C++14 user was having instant issues with the vanilla install of gcc/g++ on Ubuntu 14.04 LTS.

Getting a C++14 Compiler
So, here today are my command-line steps to update the GNU Toolchain v5.x.

sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install gcc-5 g++-5

If you already have compiler alternatives you may need these lines.

sudo update-alternatives
sudo update-alternatives --remove-all gcc
sudo update-alternatives --remove-all g++

But, everyone will need to swap the default gcc and g++ command-lines to the new paths to make them the defaults.

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 20
sudo update-alternatives --config gcc
sudo update-alternatives --config g++

These last two --config commands are only needed if you have multiple alterantives, so don't worry if it tells you it's failed as you only have the one.

Now, if you perform g++ --version, you should see it's a version 5.x series compiler.

Codeblocks 16.01
Older versions of Codeblocks may start to error on the code completion with some of the C++14 specific commands.  So, we need install some prerequisites, and then build the latest 16.01 version from source.

sudo apt-get instal gtk+-2.0 automake libtool libwxgtk2.8-dev libwxbase2.8-dev

wget http://sourceforge.net/projects/codeblocks/files/Sources/16.01/codeblocks_16.01.tar.gz

tar -xvf codeblocks_16.01.tar.gz

cd code*

The next step is interesting, we need to iteratively check:

./bootstrap

Running this will show you anything wrong with your machine environment, any missing dependences etc... However, once bootstrap runs cleanly, you can continue below.

./configure
make
sudo make install

The make step takes quite a time, the more cores & RAM you have the better, on an 4 core (8 thread) machine with 8 GB of ram, I've found it takes about 10 minutes.  On a single core machine as the poor VM I had was assigned, it took a lllooooottttt longer.

Once complete we needed to use the text editor of our choice, in sudo mode....

sudo nano /etc/ld.so.conf

And to this we need to add the line:

include /usr/local/lib

Save the file, exit the editor and run:

sudo ldconfig

Once this was complete, we can run up Codeblocks, and see it's version 16.01.



And we can further see the nice new C++14 options in the build options:



Even the code highlighting recognises the make_shared operation:



Voila, if this helped, do check out my other posts, and code, leave a tip with my new tip jar!  And I'll be making a video of this one soon, as it's so useful.

Monday, 25 April 2016

Embrace C++14 Please Mr Developer

Today I've had to spend sometime setting up a Xenserver, to host some virtual machines, the moment I was done, of course the first request I had was for a developer to have a Linux machine running a compiler.

I was quite excited, in a C# & embedded C strong company to hear "C++" as the reply to my query "what language are you going to use?"... However, my excitement quickly evaporated when I asked, "What version of C++ do you need?"... And he replied "There are different versions?"

After I had explained, yes, yes indeed there are... I took a look at his code for him, and pointed out naked pointers:

char *something = new char[28];
memset (something, 0, 28);
delete something;

I quickly explained, as kindly as I could, that this code is not only wrong, but very very old hat, and I introduced him to the standard library:

#include <string>

std::string something;
something.resize(28);
memset(&something[0], 0, something.size());

And his eyes opened a little... He asked what version of C++ is this in???... C++98... and his crest fell again, realising it was very old tech, which he had no idea about.

So I pointed him to C++14 and explained smart pointers as something for him to try out:

#include <memory>
#include <iostream>

namespace Xelous
{
class Test;
using TestPtr = std::shared_ptr<Test>;
class Test
{
public:
void Hello()
{
std::cout << "Hello";
}
};
}

int main (int p_argv, char** p_argc)
{
Xelous::TestPtr instance = std::make_shared<Test>();
instance->Hello();
std::cout << " World";
}

So, once this was done, I left him reading a copy a Tour of C++ by Bjarne, and told him to read all of Scott Meyers books.

This is a sad state of affairs for a programming & technology environment, especially when I know the chap earns more money than me, and as polite as I was I did want to just ask him to get his coat, and I'd slip into his salary grade & comfy company car (a perk I don't get).

Not least because I think the chap whom sent me off to speak to this fellow treats me a little more like a trained monkey, and they've themselves no idea about virtualisation, servers or development, beyond say using Turbo C++ from the command line in DOS 6.22... And unfortunately, things have moved on a lot since them...

Anyway, this leads me to tomorrows post, which I'm drafting, I set this C++ developer on the path to C++14, and installed him a Ubuntu 14.04 virtual machine on my little server... He was amazed, until he wondered over to me and pointed out that he had to manually add -std=c_++14 to the build options, and that sometimes the code-completion crapped out on him... Seems older Codeblocks instances fall on their face, and the default version of gcc/g++ on Ubuntu is 4.8.x and we need 5.x for C++14.  The next post will cover going through setting this up from the command-line.

Thursday, 21 April 2016

C++: Boost Libraries, Code Documentation & Examples

I have a loving relationship with the Boost C++ libraries, but I have a real hate of their documentation, not just it's style, but the mistakes and fragmentation they build into it.

When I search for say "socket", I want it to take me to boost::asio::tcp::ip::socket.  But instead it takes me on a run around, and when I finally get to boost::asio::tcp::ip::socket the examples and namespace being used is incomplete, it assumes a bunch of things, and you get told to look in tcp::ip::socket.

This is fine, if your reader knows to assume boost::asio as well, but if you're new to the whole boost project, you don't know sockets live in asio, you might be looking for boost::net, or boost::networking, or boost::network or just boost::tcp.  And you're stuck, lost, you have to leave the boost documentation search and go to google.

To fundamentally have to leave the documentation of an actual project website, and use a third party in this way is fundamentally telling you your documentation is flawed.

Then, there are mistakes in the documentation, this is annoying, but when the mistakes are in the examples given it's unforgivable, you're basically giving your users the finger, because not only are you teasing them with an example, but when it doesn't work they are again cast out into the wilds of the internet.

Case in point, again the socket, it's example code shows it as "soocket".  A typo yes, but it tells me two things, first of all, the code was not proof read, and second; and perhaps most importantly; the examples are not from working code, they've never been run, because it's not soocket, and the compiler would instantly tell anyone trying to run that code soocket is invalid.  Examples SHOULD ALWAYS BE LIFTED FROM A WORKING EXAMPLE!

Wednesday, 20 April 2016

C++11 Examples: Random Unsigned Integer Sequence

This is just a quick note, of using the C++11 random functions, to generate a positive list of random integers.

#include <iostream>
#include <string>
#include <random>
#include <cmath>

int main()
{
    // Seed with a real random value, if available
    std::random_device r;

    // Choose a random mean between 1 and 6
    std::default_random_engine e1(r());
    std::uniform_int_distribution<int> uniform_dist(1, 6);
    int mean = uniform_dist(e1);
    std::cout << "Randomly-chosen mean: " << mean << '\n';

    // Generate a normal distribution around that mean
    std::seed_seq seed2{r(), r(), r(), r(), r(), r(), r(), r()};
    std::mt19937 e2(seed2);
    std::normal_distribution<> normal_dist(20, 1000);
     

    // Loop edited by Xelous
    for (int n = 0; n < 10000; ++n)
    {
        unsigned int l_x = std::abs(normal_dist(e2));       
        std::cout << l_x << std::endl;
    }   
}


If you alter the uniform_dist spread to a higher yield, e.g. 1, 1000.  Then you increase the randomness of the mean used to generate the sequential seed.

This is taken straight from: http://en.cppreference.com/w/cpp/numeric/random

But, has been run on Coliru.

Friday, 15 April 2016

Windoze Security Loop Hole

This is an example of why I hate Windows...

In a curious case of a security loop hole, in the office, we have a supposedly locked down security situation, none of us are local administrators on our machines, and neither do we have access to any of the very useful parts of our machines.

This is a real pain, and one whereby we often have to call up on the IT Administrators to come and physically, or remotely in a remote desktop session, enter their password for us.

I personally disagree that educated users such as myself have to put up with this situation, I agree totally with data privacy and integrity, however, I wholly disagree with locking people out of things on their machines, such as defragging, or emptying the temporary folders... Or in the case of a programmer, not being able to empty Prefetch or write an ISO to an SD Card.

Anyway, today, I had to write an ISO to an sdcard, the result... I called IT and asked them to run the program for me....


So, just to be clear, I'm logged in as myself:


I am unable to access parts of the system, like the Administrators desktop folders...


I get IT to log the ISO image writer as their elevated user, and the loop-hole begins, you see the program has a standard windows open dialog.  And this will work with any standard windows open or save-as dialog, in any program... The program is running as Administrator at this point.

When I select to browse to the file to open, the default folder is the administrators folder by name...


However, because these dialogs all use explorer under the hood, and it's all integrated, they do far more than select a file for you, they let you create folders, browse things and even launch programs...

Yes you can launch a program from a save-sa, or open-sa, browsing dialog!


Lets try to run a command prompt...


Oh, look, it's running as Administrator...


And now I can see the Administrator account directories, which were hidden from be in my own logged on Explorer window..


And I can clear the prefetch folder in windows...


This is clearly wrong, but it's all caused by windows, so what's going on?...

Well, instead of asking the current session (logged in as regular old me) to start the new application instance of Command Prompt, it's asking the application owning the browsing dialog, so command prompt is started under and inherits the user credential level of that program, not my whole session.

What should have happened, well, I believe windows, starting a new program from an elevated user like this should have re-prompted for the user's password again.  And indeed, trying to start certain files from the launched command prompt it does go back to the session level to ask for the credentials to start the application with.  But not asking and just starting the new application is a problem.

Solutions I can think of include, setting the administrator level account to timeout its password every minute, so one reduces the amount of time a regular user has an unaccredited ability to launch programs.  And within a minute the administrator could have started anything the user wanted and left.

A better solution however, might have been to have a user elevation level which could give access only to what the regular user wanted, permissions to use peripherals perhaps, rather than start applications.  And the Administrator should not have just started the application as themselves, but should have started the application under themselves as the Hardware only user.

There are other solutions, and I'm sure many I'm not even going to think about, because I don't use Windows systems.  If I want security, I simply use Linux and set things up correctly.

Thursday, 14 April 2016

Project - Socket 771 to Socket 775 - Xeon Conversion

I've been conducting an experimental project, one I've seen all over the net by others, but which had lots of different information... This is the conversion of a socket 775 motherboard to a socket 771.

First of all, why?... Why would we do this?... Well, the socket 775 is a commercial socket, supposedly sold to us mere mortal customers who buy one machine and one CPU at a time, and the motherboards and processors in the class were/are quite expensive.

We're talking about the Core 2 era, Celeron D, Core 2 Duo, Core 2 Quad.  I remember the Core 2 Quad machine I put together was really rather expensive at the time.  So, between six and eight years on, we're retiring those machines; yet they cost us a lot of money, and despite depreciation rates we users can still make use of these machines.  They can be useful as render farm nodes for 3D or movie work, they can be used as servers, to host basic information, or upload/download points, even as firewalls.  All roles they can fall into easily.

I personally am going to be using the machine I've got as a quiet webserver, retiring a venerably serving Pentium 4 Prescott for this old Dell machine.

So, what is the base machine?

Well, it's a franenstein, the in-laws have had me build them a new machine, so they had an Intel D31PR motherboard holding a Celeron D 450, and one gigabyte of RAM, a totally unhelpfully slow machine.  Even they noticed it was extremely slow.

From my spares they had enough parts to basically rebuild their machine, which I did, and it left me with their old motherboard.  I wanted to upgrade the processor, expand the RAM and add a RAID array controller card, but my budget is extremely low, we're talking £20.

Well on Amazon, I can get a RAID controller card for £13.. This left me £7... Hmm.. Luckily, the IT Department at work were able to donate to me some old DDR2 RAM, so I had the maximum 4GB the board can handle.

£7... Upgrade the processor?.... A challenge... Ebay... Core 2 Duo's and Quads, going for over £25 a pop, most of the Quads were going for £30+.  Way out of the budget.

But there were dual core Xeons for around £4... And I saw this hack out there on the wires, so I set about working.

The first step is to strip everything down, clean it perfectly, and get a scalpel.  The first part of the modification is to remove the tabs from the processor, these tell the user which way to orientate the CPU for insertion, they do nothing else... A consumer CPU is orientated horizontally, so there are tabs top and bottom to stop you inserting the wrong CPU.

And the Xeon has gaps left and right, meaning it'll bounce off these tabs on the socket 775.


Taking the scalpel, I started to cut the tabs, now I DID THIS WRONG!  A much better approach is to leave the current CPU in there, with the tabs engaged into the socket 775 CPU, and then cut between the CPU and the edge of the socket.  So the CPU acts as a guide and the delicate socket pins are protected out of sight below the CPU.



And clean the cuts you make up.



Remove any debris...



Then you need to go back to ebay, and buy a Xeon Socket Modification sticker, this is a little sticker, which will cover two rows of connections on the bottom of the CPU, it will allow most of them to pass through the plastic, but two pins are headed with a little connector, and behind the sticker these connectors actually swap the two pins over.

So, two pins, and the orientation, that's all that's different about a Socket 771 and a Socket 775 CPU.


The stickers are bar shaped, so they indicate which pin to swap, but lay the CPU down with the notches to the top, and from the bottom count 10 connectors from the bottom right, moving left... Voila, stick it down carefully.

Insert into the Motherboard socket now, so the notches are to the "top" of the socket, add the heat-sink assembly, and build it back up on your work bench.


Now, some videos and advice says you need to go to sites, and download patches for your motherboard.  During my project here I've found most Intel brand motherboards do not need any patching, only third party boards.  It seems Intel include all the microcode for all their processors (this is only a guess, I have no proof other than using five different intel boards, and two none-intel boards and always having to patch the non-intel branded ones, whilst the intel ones just work).

Then powering on...


It worked, I've gone from a Celeron D 430 to a Xeon 5130.  They're very similar processors, but the Xeon has dual cores and a much faster FSB.



My YouTube play list, for my crappy videos covering this project can be found here:

Wednesday, 13 April 2016

RIP Dude - First Anniversary

I can't believe it's been a whole year since my beautiful best boy had to leave us...


I miss you everyday my big yellow beauty.