[PLUG] C++ extern keyword

Joe Pruett joey at clean.q7.com
Fri Apr 17 22:49:19 UTC 2009


> 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.



More information about the PLUG mailing list