Re: Re: A good and short "make" tutorial

Mark Space <[email protected]> Tue, 15 Apr 2003 14:48:44 -0700
Newsgroups gmane.comp.video.gimp.windows.devel
Message-ID <[email protected]>
Jigar Suthar wrote:

>Yeah, I'm interested. Right now I'm trying to get my GTK SDK to work 
>with MinGW. MinGW probably works, but I'm trying to make a basic 
>window that they have in their tutorial.
>  
>
Sure, your first two tasks are to install Msys and gvim. Msys I assume 
you know about, install that first.  gvim is over at gvim.org, grab the 
latest version from their download page and install.

Make sure you have paths set appropriately for both msys and gvim. I 
think I only had to set msys's path.

Next you have to edit the gvim config file just a bit.  Don't worry this 
is easy.  gvim normally installs a file in c:\vim\_vimrc where you add 
config commands.  The defautl files are pretty bad.  Go into the vim61 
(or which ever version number) and copy the files 
c:\vim\vim61\gvimrc_example.vim to c:\vim\_gvimrc , and 
c:\vim\vim61\vimrc_example.vim to c:\vim\_vimrc. Next all you need to do 
is add these lines to the end of _vimrc:

set autowrite
set ignorecase
set ts=4 sw=4
set sh=sh
set shellpipe=>%s\ 2>&1

Save the file _vimrc, exit gvim, and then run gvim again by editing the 
same file.  The :shell command should now use the bash shell you 
downloaded with msys, if you have the path set properly to contain 
c:\msys\1.0\bin (or where ever you installed msys). Test it with 'ls' 
command, then 'exit'. You should also have syntax highlighting at this 
point.

I should mention right now that gvim (and vi and vim) are kinda hard to 
get used to if all you know are GUI IDEs.  But gvim works exactly the 
same way on unix as it does under windows, which makes editing a breeze 
for me because I only have to know one editor.  If you don't know vi and 
related programs at all, you start in command mode. To edit, type the 
'i' key to enter instert mode. Press ESC to get back to command mode.  
You can only use commands like :shell (type that exactly, including the 
':') in command mode.

Let's test just a bit more before starting out gtk.

I make new projects with explorer (the file browser).  I have a \dev 
subdirectory on my desktop, so it's easy for me to start new projects.  
I just double click that icon, then make a new subdirectory with the 
file->new->folder command.  However you want, make a folder called test, 
then under that folder another one called hw.  In hw, make two new text 
files (this can be done with exploreer too, with the file->new->text 
file command). Rename the first text file Makefile and the second hw.c. 
(You will need to show file name extensions in order to not have 
filenames like Makfile.txt and hw.c.txt. Do this now if you don't have 
it set already.)

Now edit Makefile by right clicking on it and selecting "Edit with vim". 
Enter the following lines in Makefile:

hw.exe: hw.o
    gcc -o hw.exe hw.o

The space in front of the gcc command is important. (Makefile "scripts" 
are sensitive to indentation.)  Now go to exploreer again and right 
click on the hw.c file, choose "Edit with existing vim - Makefile"  It's 
important to use the same gvim for each project, or it gets complicated 
trying to juggle many seperate open files. gvim will juggle for you if 
you use the same gvim instance for all your files.  Type the following 
lines into hw.c:

#include <stdio.h>

main()
{
    printf( "Hello World!\n" );
}

Try now using the Buffer menu to switch back and forth between Makefile 
and hw.c. Notice they are both still loaded.  Now compile your program 
by typing :make in command mode (press ESC first). gvim will 
automatically save all modified buffers to disk, and run make in a bash 
shell.  If it finds any errors, gvim will display the first one and move 
your cursor to the offending line.  You can go to the next error with 
the :cn command, and you can go back to the previous error with the :cp 
command.  If you want to see all of your errors in a window, use :cw  
This is best done with a full screen mode gvim (just full screen the 
window) and maybe the Window->split command so you can see error and a 
couple different files at the same time.  Make sure you have both hw.o 
and hw.exe in your subdirectory, run hw.exe by double clicking on it.  
If you don't have hw.exe, you may have got linker errors. These gvim 
can't help you fix, so you have to use :cw to see them and figure out 
what went wrong.  If everything goes with out a hitch, introduce some 
syntax errors into hw.c so you can play around with finding and fixing 
errors in gvim.  Use the WIndows->split command and practice resizing 
the buffers (just grab the black bar with the mouse and move it up or 
down), viewing your Makefile and hw.c at the same time, and building in 
this mode too (building = type :make).

Now that you are just about a gvim expert you are ready for some gtk. 
Quit this gvim completely before going on to gtk.

Back to the \dev directory, I made a \dev\gtk directory, then put the 
tutorial files into gtk\test\hw and gtk\test\hw2. Make the hw subdir 
first and put two new text files therein, just like before, named 
Makefile and hw.c.  Right click on the new Makefile and choose "Edit 
with vim" to start fresh.  The makefile from the first gtk example 
almost works but needs a bit of changing for win32.  Here's what I used:

hw.exe: hw.c
    gcc hw.c -o hw.exe -mms-bitfields `pkg-config --cflags --libs 
gtk+-1.3-win32-production`

The only changes are the flag -mms-bitfields and the different config 
package.  Next right click on the new hw.c file and choose "Edit with 
existing vim - Makefile" as before.  Use the first example from the gtk 
verbatium:

#include <gtk/gtk.h>

int main( int   argc,
          char *argv[] )
{
    GtkWidget *window;
   
    gtk_init (&argc, &argv);
   
    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
    gtk_widget_show  (window);
   
    gtk_main ();
   
    return(0);
}

This should now compile without a hitch.  If you get any errors, use :cw 
in command mode to list them as before, and debug as normal.

That's all for now! My finders are tired.  Your best source for info on 
gvim is the givm.org home page, check out their "tips" section.  The gnu 
programs are well documented on gnu.org.  Don't forget that if gvim gets 
too confusing you can also try to build the exact same files you have 
created by making a shell and comipling from there - just click on the 
Msys icon, cd "subdir" to go to the correct folder, and type "make" in 
the correct subdir to build a project which has a Makefile.

TTFN!