Problems compiling with g++

Code junkies hangout here

Moderators: ChrisThornett, LXF moderators

Problems compiling with g++

Postby otrus » Mon Jan 02, 2006 1:53 am

Being used to Windows style developement suites (like visual c++, or borland c++) I'm used to leaving most of the compiling details to the IDE. I'm also used to being able to build most of my code without problems using Borland c++ Builder command line tools. But g++ for some reason doesn't compile or preprocess my sourcefiles together (or whatever) and therefore I can't include the same headerfile in more than one sourcefile! This only applies to homemade headers. I use the usual

#ifndef __HEADER_H__
#define __HEADER_H__

---
---

#endif

to prevent the code from being included more than once, and for the most part there's no problem. But if i declare a global object (or variable) in the header, say, a standard errorhandling object, g++ complains that it gets declared in every file that includes the header! Madness! I've tried both compiling all the files each time, with something like

'g++ -o output file1.cpp file2.cpp'

and using a MAKEFILE to compile and assemble each file to object, and then to link them together. Nothing works! I can't belive programmers in bigger projects have to compile or preprocess everything in some intricate manner to get to use normal fileinclusion, so I'm probably doing some stupid Windows error.

(By the way, I'll be posting this same under programming to reach more eyes)
otrus
 
Posts: 5
Joined: Mon Jan 02, 2006 1:11 am
Location: Norway

Postby 1slipperyfish » Mon Jan 02, 2006 5:17 pm

what distro are you using? as kdevelop is very much like devC++ etc if you aren't a command line jockey :D
paul
i am a follower of the culture

Image
User avatar
1slipperyfish
Forum Jester
 
Posts: 2434
Joined: Mon May 09, 2005 2:52 pm
Location: wigan

Postby otrus » Tue Jan 03, 2006 5:18 pm

Sorry, using Ubuntu and Gnome right now. Are there any alternatives for Gnome (or is there a way to use kdevelop under gnome?)
otrus
 
Posts: 5
Joined: Mon Jan 02, 2006 1:11 am
Location: Norway

Postby M-Saunders » Tue Jan 03, 2006 5:24 pm

User avatar
M-Saunders
LXF regular
 
Posts: 2893
Joined: Mon Apr 11, 2005 12:14 pm

Postby Gordon » Tue Jan 03, 2006 8:23 pm

Is your global object/variable defined/declared inside or outside of the

#ifndef __HEADER_H__
#define __HEADER_H__

---
---

#endif

If it's declared/defined outside of the above construct then that will explain the problem. If it's declared/defined inside then I'm not sure what the problem is.

You may be better off defining your object/variable inside a regular c,c++ file and then using the

extern

keyword inside a header to declare the object/variable. More information can be found in

The C++ Programming Language by Bjarne Stroustrup ISBN 0-201-53992-6
Violence is the last refuge of the incompetent
User avatar
Gordon
LXF regular
 
Posts: 209
Joined: Thu Apr 07, 2005 5:01 pm
Location: Bradford, West Yorkshire

Postby jjmac » Wed Jan 04, 2006 12:30 am

Howdy,


>>
This only applies to homemade headers. I use the usual
>>


The construct is designed to prevent multiple definitions from system headers, that may well be included by "other" system headers ... depending on what your package, or what you are including.

It sounds like the error is occurring at link time, as would occur, if your including the same header in each unit your compiling.

Try including it in one unit only, such as the unit that your using as your "main" source file, the one containing the main function.

Then i'd have another header full of "extern" declarations, as suggested above ... to let the linker know that a declaration exists for the types in one of the other objects. That you would include in every source unit that you intend to use a type in. Safe enough to include it everywhere. Except for the one that has the original inclusion in.

If your just compiling a single home-rolled source file, then it wouldn't be a problem.

>>
g++ complains that it gets declared in every file that includes the header! Madness!
>>

Not madness :), quite correct behaviour. And it does suggest your are actually including it it a number of files. So the result is to be expected.

As stated though, if that same header was being included by a number of other headers that were being included in a single unit, then it would prevent its' multiple inclusion. As per its' purpose and extensive use by the system headers.

It can be a pain though, i know :roll:



jm
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia

Postby otrus » Fri Jan 06, 2006 4:12 am

I read a couple of places now that you have to use extern in the header that gets included everywhere and either put the actual declaration in another header or a source (.c/.cc/.cpp etc.) file. I never even knew this was actually how the c/c++ languages worked before since stupid (or maby smart..?) MSVC and Dev-cpp took care of all that! I'll sue them for ruining my programming skills and causing me to use weeks of valuble worktime being frustrated over gcc.
otrus
 
Posts: 5
Joined: Mon Jan 02, 2006 1:11 am
Location: Norway

Postby jjmac » Wed Jan 11, 2006 6:32 am

>>
I read a couple of places now that you have to use extern in the header that gets included everywhere and either put the actual declaration in another header or a source
>>

Basically, the "extern" keywoer just tells the compiler that the type has been declared/defined in another unit, and thats were the assembeller for allocating memory for the variable will also be.

cbuilder will, and has also used, the same construct. If you were to click on cbuildes "new" menu item, bringing up its' selection dialog and choose a blank ".h" template, it would generate the same type of thing. Probably something like "H_NAME".

Easy to solve with a header of extern declarations though. You need to include it in any unit that will be using a variable so that the compiler will trust that it actually exists (grin)


jm
http://counter.li.org
#313537

The FVWM wm -=- www.fvwm.org -=-

Somebody stole my air guitar, It happened just the other day,
But it's ok, 'cause i've got a spare ...
jjmac
LXF regular
 
Posts: 1996
Joined: Fri Apr 08, 2005 1:32 am
Location: Sydney, Australia


Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron