Skip to content

Instantly share code, notes, and snippets.

@Acry
Last active December 5, 2019 18:45
Show Gist options
  • Save Acry/3fcc8fcd86b92cc4dbca22f5f90ce0f0 to your computer and use it in GitHub Desktop.
Save Acry/3fcc8fcd86b92cc4dbca22f5f90ce0f0 to your computer and use it in GitHub Desktop.
First Steps using make with C

First Steps Using Make With C

This is a very simple C-source-code which just returns success in a cross-platform way.

#include <stdlib.h>

int main(void)
{
return EXIT_SUCCESS;
}

Nowadays many Linux-Distributions are coming without the bare minimum of developer tools.

To make this program executable you will need a C-compiler. A popular compiler for Linux based OS's is gcc. 'GCC' doesn't stand for GNU-C-Compiler anymore, it stands for GNU-Compiler-Collection. As a side-note I need to mention the popular Clang compiler.

Make is a build-tool, it can be used to build a program from source-code. You may want to install it too.

At the top of the C-Code you see an include. The # (hash-sign) announces a pre-processor directive to include a file at the top/head of the source-code. I included a standard header - stdlib.h is the header of the general purpose standard library. It defines EXIT_SUCCESS. The headers are part of the LibC implementation. In this case GLibC. I am not sure if Kali brings the header with it.

On many Linux Distributions you need so called dev packages e.g. libc6.1-dev to get the header files. Those are not necessary to run a program, but to build one.

To install essential developer tools on Kali-Linux you want to use: apt install build-essential

And on Arch-Linux this group is called base-devel pacman -S base-devel

If you are all set you can enter the directory where the C-source-code lies and call: make dummy Make looks for a file called dummy.c and invokes gcc to compile it. To be more precise it calls cc, a symbolic link to your default C-Compiler, hence cc.

To execute the program type ./dummy.

If you want to check the return value you can type: echo $? It should say 0 on your system, so everything went well. Not every OS uses 0 as a success-code.

Now change EXIT_SUCCESS to EXIT_FAILURE

#include <stdlib.h>

int main(void)
{
return EXIT_FAILURE;
}

Compile the program again and run it. make failure ./failure Check the return-code: echo $?

It should state 1.

You learned to write a stiny C-program, how to turn a source file into a program, how to execute it from within a shell, what a return-status is and how to give a return-status back to your shell.

As a bonus this is a hello world which actually writes something to stdout (the shell in our case).

#include <stdlib.h>
#include <stdio.h>
int main(void)
{
printf("std out\n");
return EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment