Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #13143

Re: Using multiple files in (Borland) C

From James Kuyper <jameskuyper@verizon.net>
Newsgroups comp.lang.c
Subject Re: Using multiple files in (Borland) C
Date 2011-11-11 17:42 -0500
Organization A noiseless patient Spider
Message-ID <4EBDA4BD.2090002@verizon.net> (permalink)
References <f0b32de4-eb76-47ac-8934-1fd6c31cdcb8@v5g2000yqn.googlegroups.com>

Show all headers | View raw


On 11/11/2011 04:59 PM, Ruud wrote:
> Hallo allemaal,
> 
> For most of my projects I used Turbo Pascal so far. I studied C but
> more to see how programs were made and then to write them in TP. I
> decided to start in writing in C as well and best practice is just to
> do it. But I ran into a problem: I don't know exactly how to program
> using multiple files.
> Those of you familiar with Pascal will know 'units' and 'uses'. AFAIK
> you need H-files in C to do the same trick. But that didn't work out
> under Borland C 3.1. Until I added the two files (in this case) to a
> project AND used an H-file, I had success. But when I added a variable
> to the H-file, the compiler complainted about declaring this variable
> in both CPP files. I looked through various books I have, including
> user na programmers manual of BC 3.1 but I couldn't find a chapter
> that explained things (or I missed it).
> 
> Can anybody point me to an URL, PDF, document or whatever that
> explains the ins and outs of using multiple files, preferably under
> BC?

The problem here is that you're allowed to have no more than one
definition of any variable with external linkage. Actually, you should
avoid having any variables with external linkage - they cause lots of
problems - but if you really feel you need one, then make sure it has no
more than one definition anywhere in your entire program. The purpose of
header files is to share code between multiple translation units. If you
define such a variable in a header file, the variable will be defined
everywhere that you #include that header into.

What you should do is declare the variable in the header file, rather
than defining it. You can do that by using the 'extern' keyword.
However, you should separately define the variable in one and only one
module. You need to #include that header in every part of your program
that uses the variable. You should also #include it wherever the
variable is actually defined, even if the variable is not used it that
part of the program, just so you can get an error message if the
declaration in the header is inconsistent with the definition.

Example:

common.h:
	extern int shared_variable;

filea.c:
#include <common.h>
int get_shared(void) {return shared_variable;}

fileb.c:
#include <common.h>
int shared_variable = 5;

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Using multiple files in (Borland) C Ruud <ruud.baltissen@gmail.com> - 2011-11-11 13:59 -0800
  Re: Using multiple files in (Borland) C Keith Thompson <kst-u@mib.org> - 2011-11-11 14:33 -0800
  Re: Using multiple files in (Borland) C James Kuyper <jameskuyper@verizon.net> - 2011-11-11 17:42 -0500
    Re: Using multiple files in (Borland) C "osmium" <r124c4u102@comcast.net> - 2011-11-11 19:38 -0600
  Re: Using multiple files in (Borland) C Ruud <ruud.baltissen@gmail.com> - 2011-11-12 00:29 -0800
  Re: Using multiple files in (Borland) C Bill Reid <hormelfree@gmail.com> - 2011-11-12 11:08 -0800
    Re: Using multiple files in (Borland) C Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-14 01:54 -0800
      Re: Using multiple files in (Borland) C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-14 14:16 +0000
        Re: Using multiple files in (Borland) C Nick Keighley <nick_keighley_nospam@hotmail.com> - 2011-11-14 07:57 -0800
          Re: Using multiple files in (Borland) C Ben Bacarisse <ben.usenet@bsb.me.uk> - 2011-11-14 16:35 +0000
            Re: Using multiple files in (Borland) C Keith Thompson <kst-u@mib.org> - 2011-11-14 12:49 -0800
              Re: Using multiple files in (Borland) C Tim Rentsch <txr@alumni.caltech.edu> - 2012-01-25 02:02 -0800
  Re: Using multiple files in (Borland) C Jorgen Grahn <grahn+nntp@snipabacken.se> - 2011-11-12 23:07 +0000
    Re: Using multiple files in (Borland) C Ruud <ruud.baltissen@gmail.com> - 2011-11-14 12:48 -0800

csiph-web