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


Groups > comp.lang.c > #13138

Re: Using multiple files in (Borland) C

From Keith Thompson <kst-u@mib.org>
Newsgroups comp.lang.c
Subject Re: Using multiple files in (Borland) C
Date 2011-11-11 14:33 -0800
Organization None to speak of
Message-ID <ln7h3670h8.fsf@nuthaus.mib.org> (permalink)
References <f0b32de4-eb76-47ac-8934-1fd6c31cdcb8@v5g2000yqn.googlegroups.com>

Show all headers | View raw


Ruud <ruud.baltissen@gmail.com> writes:
> 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).

You shouldn't *define* variables in header files.  You can *declare*
variables in .h files and *define them in .c files.  That way you'll get
only one definition of the variable across your entire program.

A contrived example:

==> foo.h <==
#ifndef FOO_H
#define FOO_H

extern int foo_var;

void foo_func(void);

#endif

==> foo.c <==
#include "foo.h"
#include <stdio.h>

int foo_var = 10;

void foo_func(void) {
    printf("In foo_func, foo_var = %d\n", foo_var);
}

==> bar.h <==
#ifndef BAR_H
#define BAR_H

extern int bar_var;

void bar_func(void);

#endif

==> bar.c <==
#include "bar.h"
#include <stdio.h>

int bar_var = 10;

void bar_func(void) {
    printf("In bar_func, bar_var = %d\n", bar_var);
}

==> main.c <==
#include "foo.h"
#include "bar.h"

int main(void) {
    foo_func();
    bar_func();
    ++foo_var;
    --bar_var;
    foo_func();
    bar_func();
    return 0;
}

The declaration "extern int foo_var;" says that "foo_var" is defined
*somewhere else*.  If you defined it in the header, you'd get a
definition for each translation unit (.c file) that includes the header.
By defining it in the .c file, and compiling that file only once, you
get just one definition.

If you include the header in multiple files, you'll get multiple
*declarations*, but that's ok as long as there's just one *definition*.

Note that in most cases you're probably better off avoiding global
variables altogether.  But if you need them, this is the way to
do it.

-- 
Keith Thompson (The_Other_Keith) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

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