extern var's and linking

B

Bill Nolan

Hi all,

I am having a problem with a bit a C++ code I am using and was hoping
for some pointers.
the code looks like this:
"libA.h"
.....
.....
typedef struct foo{
long (*funcA) ( proto-A ) ;
long (*funcB) ( proto-B ) ;
etc...
}foo;
extern foo *pfoo ;

"libA.cpp"
#include <libA.h>
....
int entry_func(){
return secodary_func();
}

LOCAL int secondary_func(){
baz = (*pfoo->funcA) ( vars ... );
.....
}

Compile and link libA.cpp as part of a library libA.a
then use it in libB

"libB.cpp"
#include <libA.h>
stuff using libA's entry_func()

Compile and link libB.cpp in to libB.a

use both libB and libA in hello_broken.cpp
"hello_broken.cpp"
// No includes for either libA or libB <---- I think this is broken ,
but it seems
to work

compile hello_broken
link using g++ -I<include paths> -L<paths> -lA -lB hello_broken.o -o
runme.exe

returns an error that "pfoo" is an undefined refrance, I agree with the
linker that I did not tell it where to find pfoo, but I lack the
understanding to fix it and was hopeing you could help. Including the
header "libA.h" does not fixt it, which was my first response.
This is all in code that I do not maintain, It is being used in a mixed
C and CPP program any help or pointers would be much apprecaited.
Additionaly If anyone has a sugjestion for a good book or website on
C++ for a C programer I would be in your debt.
--bill
 
V

Victor Bazarov

Bill said:
I am having a problem with a bit a C++ code I am using and was hoping
for some pointers.
the code looks like this:
"libA.h"
....
....
typedef struct foo{
long (*funcA) ( proto-A ) ;
long (*funcB) ( proto-B ) ;
etc...
}foo;
extern foo *pfoo ;

That's a declaration. Where is the definition?
"libA.cpp"
#include <libA.h>
...
int entry_func(){
return secodary_func();
}

LOCAL int secondary_func(){
baz = (*pfoo->funcA) ( vars ... );
....
}

Compile and link libA.cpp as part of a library libA.a
then use it in libB

"libB.cpp"
#include <libA.h>
stuff using libA's entry_func()

Compile and link libB.cpp in to libB.a

use both libB and libA in hello_broken.cpp
"hello_broken.cpp"
// No includes for either libA or libB <---- I think this is broken ,
but it seems
to work

compile hello_broken
link using g++ -I<include paths> -L<paths> -lA -lB hello_broken.o -o
runme.exe

returns an error that "pfoo" is an undefined refrance, I agree with the
linker that I did not tell it where to find pfoo, but I lack the
understanding to fix it and was hopeing you could help. Including the
header "libA.h" does not fixt it, which was my first response.

Including a declaration in any number of translation units is not going
to solve the problem. You need to add _one_ definition to _one_ of
your C++ source files. A definition would _not_ contain 'extern' and/or
would have an initialiser.
This is all in code that I do not maintain, It is being used in a mixed
C and CPP program any help or pointers would be much apprecaited.
Additionaly If anyone has a sugjestion for a good book or website on
C++ for a C programer I would be in your debt.

groups.google.com and search for "good book c++".

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top