Linking libraries with gcc on Windows

Posted on 2006-05-30
Last Modified: 2010-04-01
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.
Question by:markvp
    LVL 4

    Expert Comment

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

    Author Comment

    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
                A(bool b);

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

          class C: public A
                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).

    Author Comment

    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.

    Accepted Solution

    Closed, 125 points refunded.
    Community Support Moderator

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
    This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
    The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
    The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now