Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-29-2006, 03:34 PM   #1
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
How do you like to set up your files with a header.h file?

I was told recently that having files use the same header.h file might cause conflicts if you do the following...

If you declare lets say int x; and make it globaly used among all files using that .h file.




#include <iostream>

#include <cstdio>

#include <cstdlib>

#include <SDL/SDL.h>

void fightque();
int fight();
int fightstart(int who, int player, int mob);
void display_bmp(char *file_name, int x2, int y2);

using namespace std;


... As you can see if I use this file amongst all my .cpp files it causes conflicts because there is multiple declarations of the same variables. As of wrighting this thread, I have came up with an idea and that is to make one headerfile that includes the predecessor files(I think thats what there called... the #include "bleh." and etc) and make one other that include only the variables needed for that .cpp file. Now before go to hell (work) and back (home), and try this out, I would like to ask this question... how do you (refering to anyone individualy) set up your program .c .cpp (or others that are similar) files with headerfiles. I was told that most real world programs use .h files, I'm not going to say all do, for that be ignorant of me as I know little of real world programs through the source, so I would like to know how you use .h files in your program, and make it function without the problem (or others that are not coming to my head)described above. Thank you all and I will answer any questions or clarify things (same thing as a question(?)) if needed.
Old 03-29-2006, 04:22 PM   #2
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
The example you show should work.
There is no variables or functions defined -> no memory allocated.
The entries you have there are only prototypes, a kind of promises that
those functions will be available at linking time.
Old 03-29-2006, 11:39 PM   #3
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Dam, once again I failed to realize the directory in which I am copying information from. That was from some other dated folder of the program. I mean to use the file that contained int x, y;. And now since it came to mind I think I need to use a pointer in order for me to get information from one file to the next. Problem is that if I put int* x, y; (I believe thats the correct way to create pointer integers)
into the headerfile, and I #include it into every document necessary for it, it'll come up with multiple integers defined. How would you get information from one variable in one file to another without coming into multiple definition conflicts? Would you create a separate .h file for each file needing that certain varaible? Once again, if it is too vague or need more info please ask me and I'll do my best to answer them.
Old 03-30-2006, 01:26 AM   #4
LQ Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Easy - in your header, just declare the globals as "extern":
#if !defined(_MY_HEADER_H)
#define _MY_HEADER_H

  extern int x;
  extern int y;
#include "myheader.h" everywhere you need it.

You need to *define* the variable (which will actually allocate the storage for your global), in exactly *one* place:
EXAMPLE: main.cpp:
#include "myheader.h"

  // Globals
  int x = 0;
  int y = 0;
You can get more details here (scan down to "Section 4.4.1: Effect of Scope"):

Last edited by paulsm4; 03-30-2006 at 01:29 AM.
Old 03-30-2006, 01:42 AM   #5
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Thank you very much, tomarow when spring break starts for us still in high school, I'll be up till the crack of dawn researching that link you gave me. Much grass!
Old 03-30-2006, 07:38 AM   #6
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
its common to see



or some variation of that..

i have never see a header defined the way Paul did.. but if it works it works..


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Including header files and source files for classes Feenix Programming 8 09-28-2005 10:53 AM
TOS in IP Header set to Minimize-Dealay - no difference ddaas Linux - Networking 5 07-26-2005 07:32 PM
set file permissions for 100 directories & its files cevjr Linux - Security 6 01-06-2005 10:23 AM
c header files in linux in place of header files in windows? harun_acs Programming 1 03-17-2004 02:24 AM
[makefile] Do header files need to be set targets? chuanyung Programming 2 03-11-2004 09:22 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:07 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration