www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/2001/09/08/04:00:21

From: Leif Leonhardy <leif AT dtcs DOT de>
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Split source file
Date: Sat, 08 Sep 2001 09:56:33 +0200
Organization: delta t Computerservice
Lines: 96
Message-ID: <3B99CF31.9709AC9F@dtcs.de>
References: <3B99AC68 DOT 35DDFD8E AT hotmail DOT nospam DOT com>
NNTP-Posting-Host: pd9e0e1c2.dip0.t-ipconnect.de
Mime-Version: 1.0
X-Trace: news.online.de 999935717 26653 217.224.225.194 (8 Sep 2001 07:55:17 GMT)
X-Complaints-To: abuse AT online DOT de
NNTP-Posting-Date: 8 Sep 2001 07:55:17 GMT
X-Mailer: Mozilla 4.01 [de] (Win95; I)
X-Priority: 3 (Normal)
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

Ian Rees schrieb:
> 
> Hi,
>    I am sure this is a rather dumb question, but I can't figure it out.
It is. And it is actually a question of C (comp.lang.c), not djgpp.

> I am writing a program which uses allegro, and the source is getting
> very long.  It is inconvenient to look through all the lines of code to
> find what I am looking for, and search is a pain.
Your editor should search for you.

> I would like to make
> two separate files, like main.c and gui.c, and be able to just pull all
> the gui functions out of my current main.c and put them in a file called
> gui.c and then compile to one .exe file.  I have tried doing this and
> using the command:
> 
> gcc -Wall -o prog.exe source.c gui.c -lalleg
> 
> but no exe is created and it spits out many warnings like:
> 
> gui.c:5: warning: implicit declaration of function '....'
> and
> gui.c:18: '...' undeclared (first use in this function)
                  ^^^^^^^^^^^ this is generally an ERROR (not just a
warning)
> 
> all of the ...s above are either allegro functions or constants.
							^^^^^^^^^ they HAVE TO 
                                                          be defined
somewhere
                                                           (this is an
ERROR)

The easiest, bad way is to split the source file "linear" in two parts
and
put an #include "part2.c" at the end of part1.c, but this does not allow
separate compilation and is very bad style (only a work around if you
cannot
split your sources into modules, e.g. if you have large arrays of data,
and
your last-millenium? editor is limited to 64K of text.)

Before you use a function, you have to declare it (implicit declarations
of
functions are allowed only if their return type is int, but this is bad
style,
too). Constants and types, structs etc. HAVE TO be declared before
usage;
that's why there are the so-called header files (*.h).

So if you split your file into main.c and gui.c, you should write a file
"gui.h", with function prototypes (DECLARATIONS) and possibly type and 
constant DEFINITIONS, which is included by BOTH main.c AND gui.c,
removing 
the constant and type definitions from gui.c. Duplicate function
DECLARATIONS
(i.e., prototypes) do not matter (in case you leave some of them gui.c).

gui.c, and possibly gui.h, have to #include "allegro.h" (or whatever).
It also may be necessary to #include "allegro.h" in main.c, but if you
split your application well, you won't need that. Other standard include
files like stdio.h will typically have to be included by both files.

The big advantage of modularization is not only readability (though it
may be inconvenient to search/edit hundreds of files, but you normally
don't do that), but also separate compilation. If you change little
(one file), you usually don't have to re-compile all of the modules.

	gcc -c gui.c	# produces gui.o
	gcc -c main.c	# produces main.o

	gcc -o main main.o gui.o -lallegro	# produces main.exe
						(linking)
        ...
	edit main.c	# changes main.c
	gcc -c main.c	# re-compile only main.c
or	gcc -o main main.c gui.o -lallegro
			   ^^^^^ doesn't have to be recompiled

In doing so, you should also consider using 'make', i.e.,
writing a Makefile:

	all:	main.exe

	main.exe:	main.o gui.o ../lib/liballegro.a

	main.o:	main.c gui.h

	gui.o:	gui.c gui.h ../include/allegro.h

(Make 'knows' how to make object files from C files etc.)
This is only an outline, read the documentation of make.
The more files you have, the more is it worth.

Leif

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019