Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
21 years ago

undefined reference to `fwrite'

Hi all,

We got this error if we use the (small) Newlib C library:

-- start --

Error pure.o * In function `__cxa_pure_virtual': undefined reference to `fwrite' pact_sb line 0

-- end

FYI: we do remove/comment all printf statements

How can we get rid of this error?

Any one helps?

Regards,

Kwok Wong

3 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by kwokwong@Dec 29 2004, 10:51 AM

    hi all,

    we got this error if we use the (small) newlib c library:

    -- start --

    error pure.o * in function `__cxa_pure_virtual': undefined reference to `fwrite' pact_sb line 0

    -- end

    fyi: we do remove/comment all printf statements

    how can we get rid of this error?

    any one helps?

    regards,

    kwok wong

    --- Quote End ---

    The reference is made in the file pure.cc:

    --start --# ifdef _GLIBCPP_HAVE_UNISTD_H# include <unistd.h># define writestr(str) write(2, str, sizeof(str) - 1)# ifdef __GNU_LIBRARY__

    /* Avoid forcing the library&#39;s meaning of `write&#39; on the user program

    by using the "internal" name (for use within the library). */

    /*# define write(fd, buf, n) __write((fd), (buf), (n))*/# endif# else# include <cstdio># define writestr(str) std::fputs(str, stderr)# endif

    extern "C" void

    __cxa_pure_virtual (void)

    {

    writestr ("pure virtual method called\n");

    std::terminate ();

    }

    -- end

    Who can tell what this funtions does and/or which ohter (libray) funtion requires this functions?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The function __cxa_pure_virtual() is a placeholder function inserted automatically by the compiler. It is used whenever you declare a pure virtual function in a base class (that is, a function that won&#39;t have an implementation provided:

    class an_abstract_base_class
    {
        virtual void do_something() = 0;
    }

    Since you can&#39;t create instances of an_abstract_base_class, you shouldn&#39;t be able to call the non-existant function an_abstract_base_class::do_something(). The compiler still needs to create a vtable (virtual function dispatch table) for abstract classes (those with one or more pure virutal methods like an_abstract_base_class), so it has to put something in the vtable entries corresponding to pure virtual functions: it uses __cxa_pure_virtual(). In properly written code __cxa_pure_virtual() should never get called. If it does somehow get called it&#39;s a signal that something has gone seriously wrong with your code.

    So, to eliminate the undefined reference error you have two choices: Either remove all pure virtual functions from you project, or provide your own version of __cxa_pure_virtual() that doesn&#39;t use facilities which aren&#39;t available on your hardware platform (e.g. just go into an infinite loop flashing an LED).
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks for the reply.

    I&#39;ll look for all unimplemented pure virtual methods.