Linux Format forums Forum Index Linux Format forums
Help, discussion, magazine feedback and more
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Problems compiling with g++

 
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming
View previous topic :: View next topic  
Author Message
otrus



Joined: Mon Jan 02, 2006 1:11 am
Posts: 5
Location: Norway

PostPosted: Mon Jan 02, 2006 1:53 am    Post subject: Problems compiling with g++ Reply with quote

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)
Back to top
View user's profile Send private message
1slipperyfish
Forum Jester


Joined: Mon May 09, 2005 3:52 pm
Posts: 2431
Location: wigan

PostPosted: Mon Jan 02, 2006 5:17 pm    Post subject: Reply with quote

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

Back to top
View user's profile Send private message
otrus



Joined: Mon Jan 02, 2006 1:11 am
Posts: 5
Location: Norway

PostPosted: Tue Jan 03, 2006 5:18 pm    Post subject: Reply with quote

Sorry, using Ubuntu and Gnome right now. Are there any alternatives for Gnome (or is there a way to use kdevelop under gnome?)
Back to top
View user's profile Send private message
M-Saunders
LXF regular


Joined: Mon Apr 11, 2005 1:14 pm
Posts: 2893

PostPosted: Tue Jan 03, 2006 5:24 pm    Post subject: Reply with quote

Try Anjuta: http://anjuta.sourceforge.net

M
Back to top
View user's profile Send private message Visit poster's website
Gordon
LXF regular


Joined: Thu Apr 07, 2005 6:01 pm
Posts: 209
Location: Bradford, West Yorkshire

PostPosted: Tue Jan 03, 2006 8:23 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message Yahoo Messenger
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Wed Jan 04, 2006 12:30 am    Post subject: Reply with quote

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 Smile, 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 Rolling Eyes



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 ...
Back to top
View user's profile Send private message
otrus



Joined: Mon Jan 02, 2006 1:11 am
Posts: 5
Location: Norway

PostPosted: Fri Jan 06, 2006 4:12 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message
jjmac
LXF regular


Joined: Fri Apr 08, 2005 2:32 am
Posts: 1996
Location: Sydney, Australia

PostPosted: Wed Jan 11, 2006 6:32 am    Post subject: Reply with quote

>>
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 ...
Back to top
View user's profile Send private message
View previous topic :: View next topic  
Display posts from previous:   
Post new topic   Reply to topic    Linux Format forums Forum Index -> Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
Linux Format forums topic RSS feed 


Powered by phpBB © 2001, 2005 phpBB Group


Copyright 2011 Future Publishing, all rights reserved.


Web hosting by UKFast