Category: Tools

Compiling and linking C

Compiling and linking C

Code
Image by fancycrave1 from Pixabay

So, if you are new to programming Compiling and linking can seem like magic. The compiler transforms C source code files into obj files. The actual format is different depending on the CPU and Operating system.  Linking is even more magic because it takes a bunch of obj files and produces a single executable.

There’s actual more magic going on than my simplistic explanation. The compiler might be doing optimisation (generally in release mode but not debug). That makes debug compiles faster which is more desirable.

Another blog called Hack the developer has published an article with a lot more detail about compiling and linking. If you want to get a better understanding then this is a petty good piece. It uses Linux, GCC and Clang as examples. It also goes into more depth on the layout of obj files.

How to install WSL 2 and Linux on Windows 10

How to install WSL 2 and Linux on Windows 10

Winver commandThis assumes that you have the version 2004 of Windows 10. Run the command Winver (open a command line then type winver) to see what version you have.

WSL is Windows Subsystem for Linux and lets you run one of several Linuxes (after installing) in Windows. For now it is terminal only but you can debug programs using Visual Studio. WSL 2 is the current version of WSL though you can run the older WSL 1.

Your computer also needs to support Hyper-V Virtualization to run WSL 2. If it doesn’t you can run WSL 1.

Steps.

  1. Open a PowerShell windows in Admin mode. My way of doing this is open the search window and type Powershell. Then right-click run as Admin.

 

When I mean Search Window, I mean the one on the Toolbar that looks like this like a magnifying glass: (highlighted in the red square)

Search Window

 

 

2. In the Powershell Windows, copy and paste this command:

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

3. Next run this command in the same Windows:

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

4. Set WSL 2 as default with this Powershell command:

wsl --set-default-version 2

Now close the Powershell Window and in the search box type Store. You should see Microsoft Store.  It’s an app on your PC. Click it to run it and type in Linux in the search box. Click Show all and you should see something like this. Pick one like Ubuntu, Debian etc.  Apart from the ones with a price against them, the rest are free. Cl;ick Get and it will install.

Linux in Microsoft Store

After it has installed, you can run it from your Start Menu. I dragged it onto the square so I have a nice clickable icon.

Windows Start MenuJust click it and your Ubuntu (or whatever) Linux will open at a terminal prompt like this.

Ubuntu Terminal

 

How I track ideas

How I track ideas

About twelve years ago I came across WOAS (= Wiki on a Stick). It was a single HTML file notebook with built in JavaScript and you opened it in a browser. It showed a simple interface and you could switch it into edit mode and start adding in text in markdown format. It was very easy to create links to pages in the document and add new pages.

And it was all saved in the document. Also very easy to find the text. It had one excellent feature which I miss, You could have a page self-encrypt using AES-256. Great for holding passwords etc.

I used WOAS to deign software and had at least one very detailed design with several hundred pages each with links to other pages. Sadly WOAS was eventually killed by the browsers themselves, preventing files writing to disk. It’s not the only example of a single-file Wiki but they all suffered the same problem. I think someone came up with a Java shim that did the file writing but it was never the same.

Now I use WikidPad, it’s written in Python and I think just runbs on Windows, (though you wouldn’t know it) which works like WOAS but internally it uses SQLite to hold the Wiki pages.  You can even use their website WikidPad but you have to register first.  That picture is a screenshot of WikidPad.

A graphics editor in C

A graphics editor in C

Screenshot of grafX2 As the picture shows, no one has ever accused me of being artistic! Hey I can’t be good at everything (sometimes it feels like anything but it passes!) and I’ve long ago accepted that I never will be an artist.

But I can get by with graphics as I hope all the pictures on this site show. Either they are screenshots or comes from sites like pixabay.com and unsplash.com. Both are excellent sources of free images.

Sometimes though I have to do a lot of editing. MonoGame requires bitmaps for fonts. So I’ll take a free monospaced font and choose a size and create a sprite sheet from it.

But sometimes the tools I use are less than perfect. I never mastered or bought PhotoShop. Instead I used software like PhotoDraw 2 (from Microsoft in the early 2000s) or before that Macromedia Fireworks. But these are quite old and instead I’ve been looking around for something a bit newer.

There are quite a few open source graphics editors and if you look closely at my er artwork you can see at the bottom that this was taken from one such package called GrafX2. One reason for including it is that it’s coded in C and it reminds me of the granddaddy of drawing packages : Dpaint. As we’re talking over 30 years ago, I don’t know how many will remember it.

Hex Editor for Raspberry-Pi

Hex Editor for Raspberry-Pi

Hex Editor Raspberry PiSometimes you just need to view or maybe a binary file and here I’ve done that on the compiled file for asteroids.

I can’t recommend doing that, but I wanted to try hexedit on the Raspberry Pi. I ran it against the asteroids compiled program and found the text.

If you look I’ve changed the d in starfield (first line of text) to an e. Then I saved it. The program, not unreasonably crashed when run with an error in the errolog.txt file: Couldn’t open images/starfiele.png

You install hexedit with

sudo apt install hexedit

and run with the file you want to view/edit.

hexedit asteroids

To view all the command press F1. It displays the man page for hexedit. Have fun!

Binary editors – Useful tools

Binary editors – Useful tools

HxD - Binary editorAt some time or other you are going to need a binary editor, to let you look in files and see what they contain.

One I can recommend is HxD which is shown here.

This not only lets you look at the contents of a binary file (in hex and decoded as text)  but you can change them.

It also includes file tools so you can split files, combine them (i.e. append) and securely wipe them. And very handy, the ability to compare two binary files, shown below.

It also lets you export binary files as data  for any of these languages (Pascal,C C#, Java, VisualBasic.net, HTML, rich text and some other formats). Here’s what the top file looks like exported into C.

/* D:\development\pivot\pivot\Debug\l.key (11/05/2020 12:03:32)
StartOffset(h): 00000000, EndOffset(h): 0000003F, Length(h): 00000040 */

unsigned char rawData[64] = {
0x18, 0x3C, 0x2B, 0x11, 0x24, 0x1E, 0x17, 0x26, 0x1C, 0x15, 0x04, 0x19,
0x23, 0x28, 0x08, 0x07, 0x35, 0x0F, 0x34, 0x37, 0x32, 0x05, 0x20, 0x27,
0x3E, 0x0A, 0x3B, 0x2E, 0x1F, 0x29, 0x21, 0x25, 0x2F, 0x14, 0x2D, 0x1A,
0x1D, 0x0C, 0x33, 0x01, 0x39, 0x2A, 0x1B, 0x00, 0x36, 0x06, 0x22, 0x31,
0x38, 0x3A, 0x3D, 0x2C, 0x16, 0x0D, 0x03, 0x00, 0x09, 0x12, 0x13, 0x02,
0x0B, 0x0E, 0x30, 0x10
};

In truth there are many binary file editors. You can find them under hex editors as well. But this is a particularly nice one; recommended.

New version of Visual Studio Code

New version of Visual Studio Code

Update to Visual Studio codeNormally I wouldn’t mention it, as there’s nothing really outstanding about the update (accessibility improvements, Timeline view, Better quick open for files etc.) You can read all about it here.

But the online documentation has improved and more importantly there’s a new tutorial using C++ on Linux. I could have done with this a few weeks back; everything there I had to figure out for myself! In our case I’ve used clang not gcc and C not C++ but those are very minor differences.

But it does have better explanations for many of the fields in the various .json files. Also the changes to tasks.json to allow multiple files. I just added them in based on gcc.

 

Install Clang and Visual Studio Code on Ubuntu

Install Clang and Visual Studio Code on Ubuntu

Visual Studio CodeClang is very easy to install. Open a terminal and issue this command:

sudo apt install clang

It takes a little bit more for Visual Studio Code (aka VS Code). Start on the download page of VS Code. Just google for download Visual Studio Code or click here. Click the Deb rectangle and pick the Open with Software install (default) an that will install it.

After that is installed, run VS Code (open a terminal and type code) and click the extensions icon (5th down on the left) and type in C++ in the search box. The first result is by Microsoft so select it and click install.

Now I created a Projects folder under my home, so click on open Folder in the File menu and select Projects. You can create individual folders for applications under that. If you look closely you can see Asteroids, AsteroidsDND and Examples with Asteroids being open and containing two .c files.

To compile, make sure the open tab is the file you wish to compile then click Terminal on the top menu and Run Build Task…  You’ll see the compiled programs on the left under the Asteroids folder (decnot and ex1).

There’s a bit more for configuring Builds and Debugging but I’ll cover that another time.

 

 

How I write my ebooks

How I write my ebooks

Screenshot of JutohMy secret: is I use a piece of software called Jutoh from a Scottish software house. It costs £35 and if you are writing a book or e-book I highly recommend it.

Note I have no connection with them except as a very satisfied customer.  

It’s all the little things, inserting pictures, tables, handling source code that make it particularly good for non-fiction books.

It also includes formatting for various programming languages and is cross-platform with versions for Linux, Mac and Windows.

The current version of the Linux e-book has 55144 words, (307407 characters). It takes about 10 seconds to compile that into a .mobi (Amazon) or .epub file with all sorts of warnings.

Originally I started writing the first e-book in a text editor then imported it to MS-Word. But Word is not exactly great software for publishing non-fiction as it’s not desktop publishing. Actually you don’t need desktop publishing per se because Kindle reflows the text, but having pictures, source code and tables makes it more complicated.

I looked about, found Jutoh and consider myself very fortunate; it makes life much easier. I’ve also had excellent support from Jutoh’s creator Dr. Julian Smart who is also well known as a creator of wxWidgets.

Writing an e-book is a multi-pass project that takes me roughly 3-4 months part-time.

  • Pass 1. Develop the software and e-book structure i.e. chapters.
  • Pass 2. Write the chapters.
  • Pass 3. Look for typos. For now this is just me, but eventually I hope to be able to afford a technical editor.
  • Pass 4. Tidy up the layout. This is probably the slowest phase. Because of the images, tables and shortish code listings, it might look neat and tidy at one size, but if you change the viewing font size, it looks er not quite so neat! I can’t see any way round that.

A friend drew the original book cover but I’ve decided to pay for the cover for the Linux version.

How to view disassembled C/C++ code in Visual Studio Code

How to view disassembled C/C++ code in Visual Studio Code

Disassembled C source codeThere are four steps to do this. That’s assuming that you have successfully compiled your C or C++ program and have the source file handy.

  1. Install Disassembly Explorer extension into Visual Studio Code.
  2. Set a configuration setting.
  3. Run a single line command in terminal.
  4. Select a command in the Visual Studio Code command palette.

Install Disassembly Explorer.

In Visual Studio Code just switch to the extension tab and search for Dissassembly Explorer and click the install button.

Disassembly Explorer

 

 

 

Next is configuration

View the installed extensions and click the cogwheel for Disassembly Explorer then click Extension Settings at the bottom of the popup menu. A Settings tab will open, click Edit in settings.json.

You should see a few settings but only this one matters: “compilerexplorer.compilationDirectory”: “${workspaceFolder}/.vscode” . That path is where the compile etc. .json files are located.

Now open a terminal

I’ve assumed that you are running on Linux or Mac. If you are on Windows, you can also use Visual Studio Community Edition which includes disassembly features and why I’m not mentioning it here.

In the folder with your source files issue this command in the terminal.

clang -g -o asteroids.S -S asteroids.c

If OTOH you are running gcc then just substitute gcc for clang.

Finally, back in Visual Studio Code select your source file as the open tab then press F1 and in the menu that appears type in enough of Disassembly Explorer: Show until you can select it and a second tab should open with the disassembly. Click on a function in the source tab and it will jump to the correct place in the disassembled code.