MESSAGE
DATE | 2010-02-13 |
FROM | Ruben Safir
|
SUBJECT | Subject: [NYLXS - HANGOUT] C++ Workshop - side discussing on C++ variable declarations
|
Date: Thu, 11 Feb 2010 19:24:44 -0500 From: Ruben Safir To: NYLUG Technical Discussion Subject: Re: [nylug-talk] C++ Workshop - side discussing on C++ variable declarations User-Agent: Mutt/1.5.20 (2009-06-14)
On Thu, Feb 11, 2010 at 07:01:11PM -0500, Ajai Khattri wrote: > On Thu, 11 Feb 2010, Ruben Safir wrote: > > > Anyone have any constructive insight on this conversation I had with a > > friend? > > I dont see what the problem is. If you're linking to code that is outside > the current file you must tell the compiler that it is defined EXTERNally. > > Often when your code is spread across several files, you might want to > refer to symbols defined in other files or other libraries, so you must > declare them ahead of your code. >
Its the idea that if you define the var (that is, not use the extern keyword), that it can create a problem that is bothering me and getting under my skin.
There is also the lack of clarity with declaration, and defining that is bothing me. It gets down to compiler theory and behavior, which I'm trying to slowly bring in with accuracy to the working write up. I know more than most, but not nearly enough. Hopefully, doing this will get me to clarify a lot of the compiler issues. Most of all, I want to right correct material.
BTW - for anyone interested, that is Billy Donahue. He has filled in a lot of the gaps that seem to be not in the texts, or that is unclear.
Ruben
> > > > (16:35:10) NYLXS: > > variables declared outside of main are global scope > > (16:35:14) billy31337: > > right > > (16:35:21) NYLXS: > > is that to the file or to the entire program? > > (16:35:26) billy31337: > > entire program > > (16:35:36) billy31337: > > unless they are declared 'static' > > (16:35:46) NYLXS: > > So I have a real question now...forget static for a moment > > (16:36:01) billy31337: > > then they are of 'internal linkage' and invisible outside the file > > (16:36:07) billy31337: > > ok > > (16:36:23) NYLXS: > > variables declared in a header file file are also then global in scope? > > (16:36:28) billy31337: > > yes > > (16:36:29) NYLXS: > > that was the real question > > (16:36:42) NYLXS: > > wow > > (16:37:05) billy31337: > > but you have to be careful to never DEFINE anything in a header.. only > > DECLARE. > > (16:37:16) billy31337: > > for variables, the difference is the 'extern' keyword. > > (16:37:46) NYLXS: > > not enough information > > (16:38:07) billy31337: > > foo.h: > > extern int foo; // declare > > foo.cpp: > > int foo; // define > > (16:38:12) NYLXS: > > how do to define an vairable? You difine a function > > (16:38:38) billy31337: > > any global declared in a header has an 'extern' in front of it.; > > (16:38:39) NYLXS: > > well that is depressing > > (16:39:05) billy31337: > > and there's a corresponding cpp file somewhere with the variable defined > > (same thing without the extern) > > (16:39:23) billy31337: > > why is that depressing > > (16:39:24) billy31337: > > ? > > (16:39:56) NYLXS: > > Because it means I didn't understand a fundemenatal thing and damn books > > aren't clear on this > > 16:40 > > (16:40:11) billy31337: > > totally correct point. > > (16:40:11) NYLXS: > > Lets start from the beginning. > > (16:40:21) billy31337: > > they almost NEVER cover physical design. > > (16:40:36) billy31337: > > that is, what goes in headers, what goes in source.. linker problems, > > etc. > > (16:40:48) NYLXS: > > what is the difference between declaring and defining a variable > > (16:41:38) billy31337: > > ok. a _declaration_ is a description of a variable (usually in a > > header) so that code which wants to access it knows how to link to it > > and how big it is. > > (16:41:41) billy31337: > > and what type it is. > > (16:42:32) billy31337: > > a _definition_ is in a .c file, and actually allocates the storage for > > the thing in its resultant .o file > > (16:43:05) NYLXS: > > define link in this context > > (16:43:32) billy31337: > > link is the final construction of the executable from one or more object > > files. > > (16:44:18) NYLXS: > > why wouldn't the storage issue not take place just at run time? > > (16:44:20) billy31337: > > the linker is the program which looks into the .o files, trying to > > satisfy their dependencies by looking into the other .o files to find > > matching symbols. > > (16:44:37) billy31337: > > because this isn't java. > > (16:44:49) billy31337: > > and the storage is in the binary. > > (16:44:58) NYLXS: > > But my variable is not going to be in an external library or o file > > 16:45 > > (16:46:14) NYLXS: > > The object file creation is a substep of the compiler that can be dumped > > to the hard drive as an object file > > (16:48:44) NYLXS: > > something else bothers me about this > > (16:49:11) billy31337: > > ld > > (16:49:14) billy31337: > > ld is a linker > > (16:49:34) billy31337: > > gcc invokes it for you, but sometimes you just want to use ld directly. > > (16:49:42) NYLXS: > > why use the syntax twice, ones in the header and once in the library > > source file > > 16:50 > > (16:50:02) billy31337: > > because it's C. > > (16:50:37) NYLXS: > > If I don't define it in the header then my program will still compile > > and work > > (16:50:47) NYLXS: > > s/dfine/decalre/ > > (16:50:50) billy31337: > > declare in header > > (16:50:51) billy31337: > > right > > (16:50:53) billy31337: > > yes > > (16:51:13) billy31337: > > but outside source files won't be able to access the variable. > > (16:51:25) billy31337: > > imagine an executable made of 1000 .o files. > > (16:51:37) billy31337: > > they all have to agree on the name and type of your int. > > (16:51:47) billy31337: > > that's what the declaration is for. > > (16:52:11) NYLXS: > > Its a debugging issue? > > (16:52:17) billy31337: > > not really. > > (16:52:28) billy31337: > > if someone calls sizeof(foo) it has to know its type > > (16:52:54) NYLXS: > > and it can't do that without the declaration? > > (16:52:59) NYLXS: > > I think it can > > (16:53:16) billy31337: > > or the code generated by an expression like "shrinkFoo(&foo)" has to > > know where to get foo's pointer address. > > (16:53:21) billy31337: > > it can't. > > (16:53:27) billy31337: > > without the declaration it can't. > > (16:53:46) billy31337: > > C will complain that it doesn't know what foo is. > > (16:54:18) NYLXS: > > even though you defined it in your library source file as > > (16:54:23) NYLXS: > > int 1 = 100 > > (16:54:25) NYLXS: > > ; > > (16:54:43) NYLXS: > > int I = 100; > > (16:54:45) billy31337: > > but that library source isn't available to your application code. > > (16:54:50) billy31337: > > at compile time. > > 16:55 > > (16:55:21) NYLXS: > > what? > > (16:55:29) NYLXS: > > If I create a function > > (16:56:00) NYLXS: > > func(int){ int I = 100; sizeof(I); } > > (16:56:07) NYLXS: > > that won't work? > > (16:56:32) billy31337: > > that will work. > > (16:56:39) billy31337: > > because 'I' is local > > (16:56:58) NYLXS: > > int I = 100; > > (16:57:13) NYLXS: > > func(int){ > > (16:57:26) billy31337: > > the extern thing is only important for communicating about globals > > ACROSS object files. > > (16:57:27) NYLXS: > > wait > > (16:57:52) NYLXS: > > To make them GLOBAL > > (16:58:05) billy31337: > > they're global anyway > > (16:58:29) NYLXS: > > Ok - I'm confused > > (16:58:37) billy31337: > > but several C files might want to use the same global. > > (16:58:44) billy31337: > > that global is only defined in one place. > > (16:58:51) NYLXS: > > one sec ... I want to look up extern > > (16:58:53) billy31337: > > this is necessary. > > (16:59:00) NYLXS: > > defined or declared? > > (16:59:10) billy31337: > > defined. one c file defines it. > > (16:59:48) billy31337: > > you have to understand what the linker is. > > 17:00 > > (17:00:13) billy31337: > > write a c program, and run 'nm' on it. > > (17:01:48) NYLXS: > > http://www.cppreference.com/wiki/keywords/extern > > (17:01:51) billy31337: > > http://www-zeuthen.desy.de/apewww/APE/software/nlibc/html/errno_8h-source.html > > <== look at line 473 for the definition of 'errno' > > (17:02:05) billy31337: > > nevermind.. let's look at your link > > (17:04:03) NYLXS: > > This got more complicated than I anticipated very fast. > > (17:04:47) NYLXS: > > You declare a variable (optinally in a header file) and define it > > 17:05 > > (17:05:04) NYLXS: > > If I define it in the header file > > (17:05:38) NYLXS: > > int abc; that is a dfinition and although it shouldn't be done in a > > header, if you do so it has global scope > > (17:06:18) billy31337: > > yes. and it will be given storage in every C file that includes that > > header, which is bad. > > (17:06:49) NYLXS: > > Go it > > (17:06:57) billy31337: > > because when the linker goes to build your app by combining these object > > files, they'll all have an 'int abc' and the linker won't be able to > > tell which is the right one. > > (17:07:10) NYLXS: > > extern int abc; will declare it without defining it > > (17:07:16) billy31337: > > right. > > (17:07:48) billy31337: > > it will leave a 'hole' in your object file to be filled in at link time > > for every instance of 'abc'. > > (17:07:52) NYLXS: > > then only the library sources that define it will create space for it > > (17:07:58) billy31337: > > the linker's only job is to go through object files filling in holes. > > (17:08:10) NYLXS: > > one sec = phone > > 17:15 > > (17:19:59) NYLXS: > > OK - so in a header file, if you want a real declaration you have to > > have the extern keyword > > 17:20 > > (17:20:44) NYLXS: > > unless otherwise your actually defineing it in the header file which > > causes more overhead > > (17:20:53) NYLXS: > > and should be avoided > > > > -- > Aj. > > _____________________________________________________________________________ > Hire expert Linux talent by posting jobs here :: http://jobs.nylug.org > The nylug-talk mailing list is at nylug-talk-at-nylug.org > The list archive is at http://nylug.org/pipermail/nylug-talk > To subscribe or unsubscribe: http://nylug.org/mailman/listinfo/nylug-talk
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
? Copyright for the Digital Millennium _____________________________________________________________________________ Hire expert Linux talent by posting jobs here :: http://jobs.nylug.org The nylug-talk mailing list is at nylug-talk-at-nylug.org The list archive is at http://nylug.org/pipermail/nylug-talk To subscribe or unsubscribe: http://nylug.org/mailman/listinfo/nylug-talk
----- End forwarded message -----
-- http://www.mrbrklyn.com - Interesting Stuff http://www.nylxs.com - Leadership Development in Free Software
So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998
http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
"Yeah - I write Free Software...so SUE ME"
"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."
"> I'm an engineer. I choose the best tool for the job, politics be damned.< You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."
? Copyright for the Digital Millennium
|
|