Create a Makefile for gcc in Visual Studio Code


  1. Install MinGW (how to set up MinGW – watch this video)
  2. Inatsll Visual Studio Code (VSC)
  3. you can immediately integrate VSC with Git so that everything is automatically synchronized. To do this, simply make a repository on Github and clone it to some folder on your computer. Open this folder in VSC and it will automatically pick up this repository, suggesting synchronization 🙂 Agree, what is already there. You can commit through the button in VSC (see the screenshot on the right) or automatically.
  4. now install the plugin there “C/C++ for Visual Studio Code”:

4. create a file in our folder main.c and write there the sacred Tetragrammaton:

#include <stdio.h>

int main(void)
{
    printf("Hello, world!");
    return 0;
}

5. try to compile it. If you have already downloaded and added the treasured gcc из MinGW – everything should work. We drive in the terminal VSC:

gcc main.c

Here’s what will happen:

File generateda.exe , because we have not given an output name to the compiler. You can already run this executable using the command:

./a

Well, you can from cmd. By the way, it is convenient to switch between different terminals directly in VSC. To do this, first enter cmd into the terminal, and then you can run it there a like that:

$ cmd
Microsoft Windows [Version 10.0.19044.1706]
(c) Microsoft Corporation. All rights reserved.

D:\GitHub\C>a
Hello, world!

To exit the terminal, type the command exit 😉

6. Now let’s compile more competently:

gcc -o main.exe main.c

You can start, as you probably already understood, using:

./main

7. Well, each time to enter the gcc command and compiler options – laziness. Therefore, programmers use the so-called makefile (Makefile). Create a new file in the project and name it… Makefile 😉 VSC will immediately prompt you to download a plugin for this theme. In the Makefile we write:

all: main.exe

main.exe: main.o
	 gcc -o main.exe main.o

main.o: main.c
	 gcc -c main.c
     
clean:
	 rm main.o main.exe

This is a Nubian version of the makefile that just generates an executable and that’s it. There you can specify a cloud of useful things. Note that indentation is important in makefiles (as in Python).

8. Now that you have a makefile, you can compile the program simply with the make command:

Conveniently? Not that word! I will write further tricks and life hacks in my notes. Dare! And leave a comment 😉

p.s.
By the way, sugar for today: clear the terminal in VSC: Ctrl+L


This entry was posted in C language (en). Bookmark the permalink.

One Response to Create a Makefile for gcc in Visual Studio Code

  1. Richard Ashton says:

    It would probably e much better to stop encouraging people to use the disaster that is Visual Studio Code. It really is quite ghastly.

Leave a Reply to Richard Ashton Cancel reply

🇬🇧 Attention! Comments with URLs/email are not allowed.
🇷🇺 Комментарии со ссылками/email удаляются автоматически.