Joe Pruett wrote: >> All C++ gurus, >> >> I'm a C++ newbie, so be gentle ;) >> >> I'm trying to compile a vendor provided c++ file on RHEL 4, test_api .cpp, >> and on compile it dumps with an error: >> >> undefined reference to 'get_auto_event(event_ap*, int)' >> >> The file I'm compiling calls get_auto_event. This function is referenced in >> a header file autosys_api_auto.h that has this line: >> >> extern int get_auto_event(EVENT_API *eapi, int polling); >> >> Now in the same directory there is a get_auto_event.cpp file that has the >> fully defined function: >> >> int get_auto_event(EVENT_API *eapi, int polling); >> ... >> >> My question is, how does the compiler know about that get_auto_event.cpp >> defines get_auto_event from just the extern keyword in autosys_api_auto.h? >> Does it just match by the name? Do I have to compile get_auto_event.cpp >> before I can compile test_api.cpp, or will the compiler handle that >> automagically? (The fact that get_auto_event.cpp doesn't compile is another >> issue. That is trying to include a missing header file). >> > > not truly a c++ issue, but a linker issue. to make your program work you > need to compile both files into object files and then link them together. > the simpleminded way is something like: > > g++ test_api.cpp get_auto_event.cpp -o test_api > > that will recompile both files every time. more complex is to compile to > .o files and then link them together. as richard indicated, usually that > would be done with a makefile and the make program so you don't have to > keep track of what files need to be rebuilt. i would guess that your > sample code might have included a makefile, and if so, just type make and > see if that gets you what you want. > To expand:
The "extern" keyword in C++ (and C, for that matter), tells the compiler "I'm defining this entity for you. It's out there somewhere, and I'm going to take care of getting it to you later, but for now this is what you need to use it". Think of it as a data sheet for the thing. I use the word "entity" because you can use "extern" to define a function as done here, or to define some global variable that you wisely and sparingly want to make available (or foolishly and profligately want to make available). So when you compile test_api.cpp, the compiler puts in the code to get_auto_event, and it puts an entry into the .o file that tells the linker "Yo! I need this function!". Then when things get to the link step and nothing has the definition of get_auto_event (because you didn't compile to a get_auto_event.o file, and didn't submit it to the linker), the linker sees that it needs something it doesn't have and it bombs. So you need to get your get_auto_event.cpp file to compile, then you'll have the function you need for the link pass (or you'll have other things missing -- not that such things _ever_ happen when you're wrapping your arms around adopted code). -- Tim Wescott Wescott Design Services Voice: 503-631-7815 Cell: 503-349-8432 http://www.wescottdesign.com _______________________________________________ PLUG mailing list [email protected] http://lists.pdxlinux.org/mailman/listinfo/plug
