Re: Defining new variables for use in multiple c files. How do I create header files that can be #included in multiple c files?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Wed, Jul 09, 2008 at 03:56:21PM -0400, Bradley Hanna wrote:
> I am trying to define a variable that needs to be manipulated by one c
> file in the kernel and read by another c file in a different directory. 
> I have defined the variable in a header file that is #included in both c
> files.  The problem is when I compile the kernel I get an error saying
> that the variable cannot be defined more than once.  I added a
> preprocessor condition like this:
> 
> #ifndef _NEW_HEADER_H
> #define _NEW_HEADER_H
> 
> //define the variable
> 
> #endif
> 
> it was my assumption that the variable would only be defined once
> because of this macro but I am still having the same problems.
> 
> How do I create header files that can be #included in multiple c files
> like the files in the source tree?

Standard C problem with a standard solution. You define the variable in
a single C file and declare it in the header file with the keyword
extern. So foo.c has:

int bar;

And foo.h has:

extern int bar;

If baz.c wants to use the variable bar, it will only have to include
foo.h:

#include "foo.h"

/* ... */

bar = 42;


Erik

-- 
Erik Mouw -- mouw@xxxxxxxxxxxx

Attachment: signature.asc
Description: Digital signature


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux