Contents

C development environment on Windows

How to create a more unix-like development environment on Windows.

Install MSYS2

  • Download MSYS2.
  • Check checksum: certutil -hashfile msys2-x86_64-20210725.exe SHA256.
  • Install.
  • Run MSYS2.
  • Update package database: pacman -Syu.
  • Run “MSYS2 MSYS App” from Start menu.
  • Update the rest of the base packages with pacman -Su.
  • Close “MSYS2 MSYS” window.
  • To pin to taskbar, open “MSYS2 MinGW 64-bit” from start menu, right click on icon, “pin to taskbar”, right click on terminal icon and set properties/target to C:\msys64\msys2_shell.cmd -mingw64.

Install Emacs

  • Open MSYS MinGW 64-bit.

  • Install Emacs: pacman -S mingw-w64-x86_64-emacs.

  • Start Emacs: emacs.

  • In Emacs install MELPA: Create ~/.emacs.d/init.el via Ctrl-x Ctrl-f ~/.emacs.d/init.el:

    (require ‘package) (add-to-list ‘package-archives ‘(“melpa-stable” . “https://stable.melpa.org/packages/") t) (package-initialize)

  • Save file via Ctrl-x Ctrl-s.

  • Evaluate init.el via M-x eval-buffer.

  • Update packages via M-x package-refresh-contents.

  • If you get Failed to verify signature archive-contents.sig: during M-x package-refresh-contents, download latest package gnu-elpa-keyring-update from here and install via M-x package-install-file and select the downloaded .tar. After this, M-x package-refresh-contents should run fine.

Install Solarized theme

  • Install Solarized theme via M-x package-install solarized-theme.
  • Activate theme via M-x customize-themes (answer prompts with y).
  • Click on Save Theme Settings.

Further settings

  • Open ~/.emacs.d/init.el via Ctrl-x Ctrl-f ~/.emacs.d/init.el.
  • Add the following settings:
    • Deactivate startup splash screen via (setq inhibit-startup-message t).
    • Deactivate menu bar via (menu-bar-mode -1).
    • Deactivate tool bar via (tool-bar-mode -1).
    • Deactive scroll bars via (scroll-bar-mode -1).
    • Deacticate cursor blinking via (blink-cursor-mode -1).
    • Activate column number mode via (column-number-mode 1).
  • Activate settings via M-x eval-buffer.
  • Full ~/.emacs.d/init.el so far:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
(require 'package)
(add-to-list 'package-archives
  '("melpa-stable" . "https://stable.melpa.org/packages/") t)
(package-initialize)
(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-enabled-themes '(solarized-dark))
 '(custom-safe-themes
   '("0598c6a29e13e7112cfbc2f523e31927ab7dce56ebb2016b567e1eff6dc1fd4f" default))
 '(package-selected-packages '(solarized-theme)))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )
(setq inhibit-startup-message t)
(menu-bar-mode -1)
(tool-bar-mode -1)
(scroll-bar-mode -1)
(blink-cursor-mode -1)
(column-number-mode 1)

Install Clang

  • Install Clang via pacman -S mingw-w64-x86_64-clang. (You can do this from Emacs via eshell now: M-x eshell.)
  • Create sample application via Ctrl-x Ctrl-f hello.c:
1
int main(void) { printf("Hello world!\n"); }
  • Compile with clang -include stdio.h hello.c -o hello.
  • Run with hello, hello.exe or ./hello.exe.

Install GDB

  • Install GDB via pacman -S mingw-w64-x86_64-gdb.
  • Create sample application:
1
2
3
4
5
6
#include <stdio.h>
  
int main(void) {
  int x = 10;
  printf("x = %d\n", x);
}
  • Save as 10.c.
  • Compile via clang -g 10.c -o 10.
  • In Emacs start gdb via M-x gdb.
  • In gdb load symbols via file 10.
  • Set breakpoint in line 4 via break 10.c:4.
  • Start program via r.
  • When breakpoint is hit, print x via print x.
  • x will be 0.
  • Move to next instruction via n.
  • print x will show x is 10.
  • Quit debugging with q or continue till end of execution via c.

Install GNU Make

  • Install GNU Make via pacman -S make.
  • Create sample application:
1
2
3
4
5
6
// main.c
#include "library.h"

int main() {
  SomeFunction();
}
1
2
// library.h
void SomeFunction();
1
2
3
4
5
6
// library.c
#include <stdio.h>

void SomeFunction() {
  printf("SomeFunction()\n");
}
  • Create file Makefile:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
CC=clang

CFLAGS= -Wall -Werror -g -O0

INCLUDES =

LFLAGS =

LIBS =

SRCS = library.c main.c

OBJS = $(SRCS:.c=.o)

MAIN = main.exe

all:$(MAIN)
	@echo  main.exe has been compiled

$(MAIN): $(OBJS) 
	$(CC) $(CFLAGS) $(INCLUDES) -o $(MAIN) $(OBJS) $(LFLAGS) $(LIBS)

.c.o:
	$(CC) $(CFLAGS) $(INCLUDES) -c $<  -o $@

clean:
	$(RM) *.o *~ $(MAIN)

run:
	./main.exe
  • Load main.c or library.c in Emacs and build the program via M-x compile RET make -k RET.
  • (If you get something like Makefile ... missing separator, you didn’t use tabs in your Makefile.)
  • Run program via M-x compile RET make run RET.
  • To clean up and recompile all files perform M-x compile RET make clean RET.

Install git

  • Install git: pacman -S git.
  • Set up git user credentials via:
    • git config --global user.email "you@example.com"
    • git config --global user.name "Your Name"

Install Magit

  • In Emacs install Magit via M-x package-install RET magit RET.
  • Create repository: M-x magit-init.
  • Add files via M-x magit-stage or open Magit buffer via M-x magit and stage files via s.
  • Commit changes via M-x magit-commit RET c or open Magit buffer with M-x magit and commit via c, enter a commit message and Ctrl-C Ctrl-C to commit (if you don’t enter a commit message, commit will fail).
  • View changes via M-x magit-diff RET d RET.
  • Revert changes of opened file via M-x magit-file-checkout.
  • Use M-x magit while file of repository opened to open an overview of a repository.

Install Doxygen

  • Install Doxygen via pacman -S mingw-w64-x86_64-doxygen.
  • Create Doxygen configuration file with doxygen -g.
  • Sample code with Doxygen documentation (\file has to be at beginning of file):
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
/// \file file.c
/// Description of file.

/// Description of function.
/// \param   parameter Some parameter.
/// \returns Some return code.
int SomeFunction(int parameter) {
    int result = DoSomething();
    return result;
}
  • Create documentation via doxygen.
  • Documentation is created in directories html and latex.