Link to home
Start Free TrialLog in
Avatar of markvp
markvp

asked on

Linking libraries with gcc on Windows

I have the following situation with mingw gcc 3.4.4 on Windows 2000: static libraries a.lib and b.lib; b.lib uses class C defined in a.lib. application links to a.lib and b.lib (with command -la -lb). The linker gives an error saying "undefined reference to `vtable for C'". If I make b.lib use another class from a.lib, I get an extra similar error for that class.

What am I doing wrong? I tried to add the command -la when building b.lib, but that doesn't change anything (I think it is useless).

The same libraries and application work just fine with Visual Studio.
Avatar of e_tadeu
e_tadeu

Try using g++.exe instead of gcc.exe.
It seems you are using virtual functions, are your headers correctly defined?
Avatar of markvp

ASKER

When I said gcc, I actually meant the collection of compilers that includes g++. I do use g++. I'm sure my headers are defined correctly. The class looks like this:

namespace MyNamespace
{
      class A
      {
      public:
            A(bool b);

            virtual const String& GetType() const = 0;
            virtual String GetInfo() const = 0;
      };

      class C: public A
      {
      public:
            C(): A(true) {}

            virtual const String& GetType() const;
            virtual String GetInfo() const;
      };
}

The implementation in the cpp-file looks correct (and works perfectly in Visual Studio).

A static instance of C is created in a cpp-file in b.lib. Removing that instance solves the problem, which proves that the problem is in there (but of course I need that instance).
Avatar of markvp

ASKER

I found the problem. It is the order that the libraries are specified. So if it is -lb -la instead of -la -lb, it works fine.
ASKER CERTIFIED SOLUTION
Avatar of PAQ_Man
PAQ_Man
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial