Article View: comp.sys.mac.programmer.codewarrior
Article #472Re: linking/including
From: Steven Daedelus
Date: Wed, 12 Jan 2005 22:17
Date: Wed, 12 Jan 2005 22:17
60 lines
4414 bytes
4414 bytes
In article <vRgFd.4749$KJ2.3072@newsread3.news.atl.earthlink.net>, "Paul" <remove_this_its_just_paul@patchdance.com> wrote: > "Steven Daedelus" <sdaedelus@ira.gov> wrote in message > news:sdaedelus-89709B.15582112012005@comcast.dca.giganews.com... > > Hi all, > > > > Help a fairly clueless guy: Let's say I add source1.cpp to a project. It > > refers to functionX defined in source2.cpp. Now, if I put include > > "source2.cpp" at the top of source1.cpp, it works ok. If I add > > source2.cpp to the project, it tells me that functionX is undefined. > > > > Perhaps I just don't understand linking. Could someone explain this to > > me? > > > When you #include "source2.cpp", you effectively add the entire file to the > beginning of source1.cpp. Everything in source2.cpp is compiled before > everything in source1.cpp, and it works. > > When you add source2.cpp to the project, it is compiled separately and > linked in; however, source1.cpp no longer sees it (the error you're seeing > means that the compiler doesn't know what functionX is, and can't generate > code to call it: the linker isn't involved at this point, and the compiler > only sees what you tell it to.) > > > If you #include source2.cpp everywhere it's referenced, it willl work but > source2.cpp will have to be compiled every time a file that #include's it is > Any changes to source2.cpp would force every file that #included it to be > rebuilt as well > Your binary might not get bigger (modern linkers are pretty good at spotting > redundant code and removing it) but your build times will increase. > > > You should: > Add source2.cpp to your project: that way it gets compiled once > #include source2.h (which should at least include a declaration of > functionX) to every file that needs it. That way the compiler knows > functionX is defined and the linker will look for and reference it properly. > Now you can change source2.cpp without forcing anything else to recompile > (as long as the declarations in source2.h don't change.) > > Another way is to declare functionX extern in source1.cpp. This simply tells > the compiler that the function is defined and prevents an error, and the > linker takes care of locating the right code in source2.cpp. This isn't > generally a good idea for several reasons, notably that you will have > trouble if functionX's declaration ever changes (you'll have to hunt down > every single extern declaration and fix it, instead of fixing it once in > source2.h.) > > > This is just the way C works: a separate header that lets files refer to > each other (a type of forward declaration.) Pascal and other > module-structured languages handle this by effectively generating their own > headers from the source code and #including it automatically. there have > been several attempts to do this with C-style languages (google MakeHeaders, > for example, but it's pretty limited w/ C++.) Oh, of course. That makes perfect sense. Many thanks.
Technical Details
Message-ID:
<sdaedelus-2CE6EC.17174012012005@comcast.dca.giganews.com>Path:
arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mailReferences:
<sdaedelus-89709B.15582112012005@comcast.dca.giganews.com> <vRgFd.4749$KJ2.3072@newsread3.news.atl.earthlink.net>Show Full Headers
Complete Article Headers
X-Google-Thread: f635e,8ec5fd6a4335841b X-Google-Thread: 10bcce,8ec5fd6a4335841b X-Google-Attributes: gidf635e,gid10bcce,public X-Google-Language: ENGLISH,ASCII-7-bit Path: arc1-ca.pugleaf.net!g2news1.google.com!news4.google.com!news.glorb.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local1.nntp.dca.giganews.com!nntp.comcast.com!news.comcast.com.POSTED!not-for-mail NNTP-Posting-Date: Wed, 12 Jan 2005 16:17:11 -0600 From: Steven Daedelus <sdaedelus@ira.gov> Newsgroups: comp.sys.mac.programmer.codewarrior,codewarrior.mac Subject: Re: linking/including Organization: Irish Republican Army References: <sdaedelus-89709B.15582112012005@comcast.dca.giganews.com> <vRgFd.4749$KJ2.3072@newsread3.news.atl.earthlink.net> User-Agent: MT-NewsWatcher/3.4 (PPC Mac OS X) Date: Wed, 12 Jan 2005 17:17:40 -0500 Message-ID: <sdaedelus-2CE6EC.17174012012005@comcast.dca.giganews.com> Lines: 60 NNTP-Posting-Host: 24.91.165.114 X-Trace: sv3-QszxResa5EtbNS/9fDeKCxKvYbwAIn/weeHLglxTt0J5tvd+SQkUukAzoQiwd1btJunV7ZuiY/koqbK!vQDX+kyuL/kSAkNv3Lnxya326OOGIlLNdAmX2vnMVnE4vBDLetFcZSu+cYz0ne47XTR3aVK7taYb!EwdmcfsdhgrzB6d04WrXYTWhc1QJdA== X-Complaints-To: abuse@comcast.net X-DMCA-Complaints-To: dmca@comcast.net X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.22