|
From: Jason H. <jas...@bt...> - 2002-04-05 02:39:39
|
I'll answer the header question first and then use that to tell you what = an "undefined reference" means, or at least I'll try. :) The reason we use headers and cpp files seperately is due to the need to = modularise programs. This is done for several reasons, such as the = ability to add other code into yours, meaning to make use of a function = or library of functions you or someone else has written, i.e. the = standard library; or when projects become too big for it to be all in = one file, then you will want to break your code up into functional = units, each performing a seperate task, which is good on a variety of = levels, like making the code more managable and speeding up compilation = because only the modified sections need to be recompiled. In the header file you will put all the prototypes and definitions that = are used in it's related .cpp (or .c) file. The .cpp file itself = contains the code that performs the action of the functions, methods and = such. It will also contain a instantiation of memory used for static = members and such. Note that there is a difference between definition and = instantiation. I can be said that a header represents a definition, = while the cpp file represents the instantiation. The definition = describes what is within the cpp file. Back to modularisation, suppose we have two modules (or units, i.e. a = cpp file and library) one of which makes use of a function defined = within the other, how does the compiler know how to call that function? = It will look at the header of the other module and know that function = 'add' takes two parameters one string and one int and returns a bool, = for example. It will set up the call as appropriate in it's own code. It = doesn't need to know how the other function is implemented, or even if = it has been implemented, it can take that for granted. At the end of compilation you will be left with group of object file = (.o), one for each cpp file, which depend upon each other and maybe some = library or other. What you do next is 'link' them all together into an = executable with a linker. When the compiler made the object file it = didn't know where the function was, just how to make use of it. The = linkers responsibility is to take the call in one object and resolve it = to another module, directing the call to the right place within the = code. The compiler leaves little bits of information in the object file = to make the linker's job easier. Once all the 'references' have been = resolved to the correct places the linker can wrap it all up in the = information that forms a executable file, which depends upon the target = system. As you may have guessed your undefined references are where the linker = has looked for some function, but cannot find it in any of the modules = that it has been told to link. Your problem will be that the file = containing those functions needs to be made available to the linker. I = can see that you are dealing with some opengl stuff so I would reckon = that you need to link the libraries that go with the headers for the = opengl. Remember that the header only tells the compiler about the = function, it doesn't give it the code that does the things you need. There should be instructions with the library about how to link in the = correct library. Generally you will add the library files (.a) as = "further object files" in the project options and I believe you can do = it with "-l" based compiler options, although I could never get that to = work (dunno why). Someone else may explain it? You may be wondering why you don't get these undefined references for = the standard library functions and methods that you call. That is = because Dev-C++ does that bit for you, it knows where they and will link = them automatically, the same for when writing windows programs. When you = select to compile for a windows program (no console) in the options, = Dev-C++ will make sure the extra code needed is linked in with yours. Feel free to e-mail me to clarify any points I have made. Jason. ----- Original Message -----=20 From: Stein Nyg=E5rd=20 To: DevCpp=20 Sent: Thursday, April 04, 2002 8:23 PM Subject: [Dev-C++] $_70 wierdness! Hi guys! Anyone know what this means? f:\development\opengl\sengine v2.5\scenes.o(.text+0xcf):scenes.cpp: = undefined reference to `$_70::load(char *, char *)' f:\development\opengl\sengine v2.5\scenes.o(.text+0x1e0):scenes.cpp: = undefined reference to `$_70::draw(bool, bool)' f:\development\opengl\sengine v2.5\scenes.o(.text+0x277):scenes.cpp: = undefined reference to `$_70::free(void)' And can someone please explain me what it is with headers? Although = I've been programming for several years, I'm still confused about what = goes in a *.h-file and what goes into a *.cpp. To me it seems kinda like the only difference, is that it takes longer = to compile if stuff is saved as .cpp-files instead of header files, but = I can't believe that's it :/ Thanks!!! |