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 |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-29-2006, 03:34 PM
|
#1
|
|
Member
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Rep:
|
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.
|
|
|
|
03-29-2006, 04:22 PM
|
#2
|
|
Senior Member
Registered: Oct 2003
Location: Zurich, Switzerland
Distribution: Debian/unstable
Posts: 1,357
Rep:
|
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.
|
|
|
|
03-29-2006, 11:39 PM
|
#3
|
|
Member
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Original Poster
Rep:
|
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.
|
|
|
|
03-30-2006, 01:26 AM
|
#4
|
|
Guru
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,861
Rep: 
|
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.
|
|
|
|
03-30-2006, 01:42 AM
|
#5
|
|
Member
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Original Poster
Rep:
|
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!
|
|
|
|
03-30-2006, 07:38 AM
|
#6
|
|
Senior Member
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065
Rep:
|
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..
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 04:30 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|