extern var's and linking

Discussion in 'C++' started by Bill Nolan, Dec 30, 2004.

  1. Bill Nolan

    Bill Nolan Guest

    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
     
    Bill Nolan, Dec 30, 2004
    #1
    1. Advertising

  2. Bill Nolan wrote:
    > 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
     
    Victor Bazarov, Dec 30, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. thomson
    Replies:
    10
    Views:
    2,498
    Eliyahu Goldin
    Jun 20, 2005
  2. thomson
    Replies:
    0
    Views:
    386
    thomson
    Jun 20, 2005
  3. Dave
    Replies:
    2
    Views:
    377
    E. Robert Tisdale
    Dec 2, 2003
  4. Thomas Matthews
    Replies:
    5
    Views:
    2,435
    tom_usenet
    Aug 2, 2004
  5. Andre
    Replies:
    5
    Views:
    542
    Keith Thompson
    Jul 17, 2012
Loading...

Share This Page