Archived

This topic is now archived and is closed to further replies.

Wymsical

Getting inherting from a .a to work in Kdevelop (&gcc?)

Recommended Posts

i posted this problem originally in the genral programming forum but i found that this problem pertains only to gcc and kdevelop. the problem is this: i create a class: class foo { public: foo() {} virtual ~foo() {} static int i; }; and in the cpp file... int foo::i = 0; now i compile this into libfoo.a i then create another class: class foobar : public foo { foobar() { cout << i; } ~foobar() {} }; i compile this into libfoobar.a however the problem occurs when i try and use a foobar in another program, it complains about an undefined reference to i. when i compile the programi link to libfoobar.a and libfoo.a

Share this post


Link to post
Share on other sites
Even if foobar inherits from foo, as foo::i is a static member of foo, it isn''t a member of foobar.

You will have to refer to it as foo::i

foobar() { std::cout << foo::i; }

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by Fruny
Even if foobar inherits from foo, as foo::i is a static member of foo, it isn''t a member of foobar.

You will have to refer to it as foo::i

foobar() { std::cout << foo::i; }



''i'' is a unique member shared across all classes of type ''foo'' and/or inheriting directly or indirectly from ''foo'', and is part of ''foo'' as well as of ''foobar''.

Static members have external linkage resolution. The compiler won''t complain but the linker will fail to find your static member, as it is not part of your ''libfoobar.a''.

ar -r libfoobar.a foo.o foobar.o

That should fix your problem. Of course, you''re duplicating object code here, as foo.o is now part of both libfoo and libfoobar (in this case, libfoo isn''t needed anymore, it''s only smaller and ok to use if you''re sure you''ll never need the ''foobar'' class). If you want to avoid that, use shared objects and not archives.

I hope this makes sense, I''m tired.

Have fun.

Share this post


Link to post
Share on other sites
Okay, so let me get this straight:
if i want to keep using this separate library-architecture, libfoo will have to be a shared library for foobar to work properly?

Share this post


Link to post
Share on other sites