gnu-gcc-banner

GCC: Build Application with Docker in VS Code

During this article we will show to you how you can build a simple C/C++ application and compile into a docker container using GNU GCC compiler.

Step 1 — Get GNU GCC Image Form Docker Hub

First of all you have to go to the docker pub site and you type in the search bar and enter the word GCC an choose the first search that matches the official GCC image:

docker hub gcc image

Then copy the command: docker pull gcc

docker hub image pull gcc

After that go to the VSCode terminal and enter the command to retrieve the docker image on your machine:

docker pull gcc
run docker pull image on the shell terminal

Once the installation is complete we start the gcc container by executing the commands bellow:

>> docker run -d gcc
>> docker start f5a062036007
start the gcc container

Step 2 — Install The “Remote Containers” Plugin In VSCODE

Go to the VSCODE extension tab and enter “Remote containers”: 

add the remote container extension to the VSCode IDE

Then we will attach VSCODE on the gcc container already launched 

use the remote code extension to run into th docker container

Step 3 — Write And Build A Simple C Program Using GNU GCC Compiler

We write a small program in c that calculate the sum of two numbers passed in parameter of the program:

#include <stdio.h>
#include <stdlib.h>
 
 
void main(int argc, char** argv)
{
   int number1 = atoi(argv[1]);
   int number2 = atoi(argv[2]);
 
   printf("The Sum of %s and %s equal to %d\n", argv[1], argv[2], 
number1 + number2);
}

after that, we compile the program on the terminal using the GNU GCC compiler and then we launch the binary output program file with some arguments values : ./mainApp 23 12

root@a6f7efc21d28:/home/Src# ls -al
total 12
drwxr-xr-x 2 root root 4096 Jun  5 19:44 .
drwxr-xr-x 1 root root 4096 Jun  5 19:28 ..
-rw-r--r-- 1 root root  231 Jun  5 19:44 main.c
root@a6f7efc21d28:/home/Src# gcc -o mainApp main.c
root@a6f7efc21d28:/home/Src# ./mainApp 23 12
The Sum of 23 and 12 equal to 35
build a c program on docker container

Conclusion

I Hope that this tutorial gives you some help to initiate and build your C project using the powerful docker tool. 

Please don’t hesitate to share the link of this article and see you for a next post! 🙂

Default image
@freecoder

With 15+ years in low-level development, I'm passionate about crafting clean, maintainable code.
I believe in readable coding, rigorous testing, and concise solutions.

Articles: 22