LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 03-29-2006, 03:34 PM   #1
RHLinuxGUY
Member
 
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.

Ex:

#ifndef HEADERFILES_H

#define HEADERFILES_H



#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;

#endif

... 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
ToniT
Senior Member
 
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357

Rep: Reputation: 47
The example you show should work.
Why?
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
RHLinuxGUY
Member
 
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
paulsm4
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":
Code:
#if !defined(_MY_HEADER_H)
#define _MY_HEADER_H

  extern int x;
  extern int y;
  ...
#endif
#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:
Code:
#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"):
http://publications.gbdirect.co.uk/c...4/linkage.html

Last edited by paulsm4; 03-30-2006 at 01:29 AM.
 
Old 03-30-2006, 01:42 AM   #5
RHLinuxGUY
Member
 
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
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
its common to see

Code:
#ifndef HEADERNAME_H
#define HEADERNAME_H

...
code
...

#endif
or some variation of that..

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


Reply


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


All times are GMT -5. The time now is 04:35 AM.

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