Understanding Make files

Find Tips and tricks on how to better use the Zeus IDE. Feel free to post your own tips but please do not post bug reports, feature requests or questions here.
Post Reply
Guest

Understanding Make files

Post by Guest »

Here are some introductions into understanding make files: Here are some other links to manuals/tutorials on how to create make files: Here are lots of resources for the GNU Make tool: https://www.gnu.org/software/make/manual/

Naturally, it very easy to incorporate these make files into the Zeus IDE's project configuration model.
jussij
Site Admin
Posts: 2650
Joined: Fri Aug 13, 2004 5:10 pm

Post by jussij »

Simple Make File Tutorial

To help demystify the make process here is an example to illustrate just how easy it is to manage a project with using a make file.

Consider a Windows project call MyProject that is made up of a main.cpp file as follows:

Code: Select all

//------------------------------------------------------------------------
//--  Simple Hello World Windows Application
//------------------------------------------------------------------------

#define STRICT
#include <windows.h>

// found in window.cpp
extern "C" long APIENTRY WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
                     LPSTR lpszCmdParam, int nCmdShow)
{
   static char szClassName[] = "Hello Word";
   HWND        hwnd;
   MSG         msg;
   WNDCLASS    wndclass;

   if (!hPrevInstance)
   {
      wndclass.style          = CS_HREDRAW|CS_VREDRAW;
      wndclass.lpfnWndProc    = WndProc;
      wndclass.cbCls_Extra     = 0;  // ***** remove the underscore!!!
      wndclass.cbWndExtra     = 0;
      wndclass.hInstance      = hInstance;
      wndclass.hIcon          = LoadIcon (NULL, IDI_APPLICATION);
      wndclass.hCursor        = LoadCursor (NULL, IDC_ARROW);
      wndclass.hbrBackground  = (HBRUSH)GetStockObject (WHITE_BRUSH);
      wndclass.lpszMenuName   = 0;
      wndclass.lpszClassName  = szClassName;

      RegisterClass (&wndclass);
   }

   hwnd = CreateWindowEx(WS_EX_OVERLAPPEDWINDOW,
                         szClassName,
                         "Hello Word",
                         WS_OVERLAPPEDWINDOW,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         CW_USEDEFAULT,
                         NULL,
                         NULL,
                         hInstance,
                         NULL);

   ShowWindow (hwnd, nCmdShow);
   UpdateWindow (hwnd);

   while (GetMessage (&msg, NULL, 0, 0))
   {
      TranslateMessage (&msg);
      DispatchMessage (&msg);
   }

   return msg.wParam;
}
and a window.cpp file as follows:

Code: Select all

//------------------------------------------------------------------------
//--  Client Window Procedure
//------------------------------------------------------------------------

#define STRICT
#include <windows.h>

extern "C" long APIENTRY WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);

long APIENTRY WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
   HDC         hdc;
   RECT        rect;
   PAINTSTRUCT ps;

   switch (message)
   {
      case WM_CREATE:
         return 0;

      case WM_PAINT:
         hdc = BeginPaint (hwnd, &ps);
         GetClientRect (hwnd, &rect);
         SetBkMode(hdc, TRANSPARENT);
         DrawText (hdc, "Hello Windows!", -1, &rect,
         DT_SINGLELINE|DT_CENTER|DT_VCENTER);
         SetTextColor(hdc, RGB(255, 0, 0));
         TextOut(hdc, rect.right/2, 200, "Hello World", 11);
         EndPaint (hwnd, &ps);
         return 0;

      case WM_DESTROY:
         PostQuitMessage (0);
         return 0;
    }

    return DefWindowProc (hwnd, message, wParam, lParam);
}
To build this project we need to compile the two source files and link the resulting object files to produce and executable.

Assuming this is a MSVC development environment the compiler will cl.exe, the linker will be link.exe and the maker will be nmake.exe.

NOTE: Depending on how these tools have been installed you may or may not need to run the vcvars32.bat to setup the environment.

So the example.mak make file for this project will be as follows where lines that start with # are just comments:

Code: Select all

##########################################################################
# An Example of a Simple Make File
##########################################################################

##########################################################################
#   How to Use Inside of Zeus
##########################################################################
#
# In the Zeus Build Workspace Options use this command line:
#
#   nmake -f Example.mak
#
# In the Zeus Rebuild Workspace Options use this command line:
#
#   nmake -f Example.mak REBUILD
#
# In the Zeus Clean Workspace Options use this command line:
#
#   nmake -f Example.mak CLEAN
#
##########################################################################

##########################################################################
# Step #1: Here are the project details
##########################################################################

# Give the project a name
PROJECT = MyProject

# Define the source files that make up the project
SOURCE_FILES = main.cpp window.cpp

# Define the object files that make up the project
OBJECT_FILES = main.obj window.obj

##########################################################################
# Step #2: Define the compiler details needed to compile the source code
##########################################################################
# Define the C++ compiler executable
CC = cl.exe

# Define the C-Compiler command line arguments (excluding file details)
CFLAGS = /W4

##########################################################################
# Step #3: Define the linker details needed to do the build project
##########################################################################
# Define the Linker executable
LINK = link.exe

# Define the linker options
LFLAGS = /DEBUG

# Define the library files to be linked in
LIBS = GDI32.LIB USER32.LIB

##########################################################################
# The details should hardly ever be change. They might need to be teaked
# or changed if you are not using the MSVC development tools.
##########################################################################

# This is the name of the executable to be produced
EXECUTABLE = $(PROJECT).exe

##########################################################################
# Define how C++ files are compiled to produce object files
##########################################################################
.cpp.obj :
	$(CC) $(CFLAGS) /c $<

##########################################################################
# Define how to create the project executable file from the object files
##########################################################################
$(EXECUTABLE) : $(OBJECT_FILES)
	link $(LFLAGS) $(OBJECT_FILES) $(LIBS) /OUT:$(EXECUTABLE)

##########################################################################
# Define how to rebuild the project
##########################################################################
REBUILD : CLEAN $(EXECUTABLE)
    del $(EXECUTABLE)
    del $(OBJECT_FILES)

##########################################################################
# Define the make clean the project (i.e delete all output files)
##########################################################################
CLEAN :
    del $(EXECUTABLE)
    del $(OBJECT_FILES)
To test the make file do the following:
  1. Create a project folder
  2. Add the main.cpp file to this folder
  3. Add the window.cpp file to this folder
  4. Add the example.mak file to this folder
  5. Open a DOS command line window
  6. Run the "c:\Program Files\Microsoft Visual Studio\VC98\Bin\vcvars32.bat" if required
  7. Run the these commands

    Code: Select all

    nmake -f Example.mak
    nmake -f Example.mak REBUILD
    nmake -f Example.mak CLEAN
If you are using another make utility, for example maybe the make.exe that comes with the Digital Mars C/C++ or DMD packages, where ever you see nmake.exe replace it with make.exe. I tested the make file with the Digital Mars maker and it worked just find ;)

NOTE: This make file will probably work with the GNU Make, provided the leading white space characters are first converted to a physical tab character. For some reason some makers insist that the first character of a command line must be a tab character.

Naturally, rather than using a DOS command line window you can also configure Zeus to run the make and have it capture the output ;)

Cheers Jussi
Post Reply