problem compiling static library

Discussion in 'C++' started by nvangogh, May 27, 2014.

  1. nvangogh

    nvangogh Guest

    I am trying to build a static library. I am having a problem when I try
    to compile it. I am getting two errors. The first is at line 82 and the
    second is at line 120. Both errors seem to be related to the same thing,
    namely 'memset was not declared in this scope'

    It is a very long file so I have posted only the section of the file up
    to the first error as the second error further down seems to be similar.
    ------------------------------------------------------------------

    // This is a X Window/Posix implementation of the playpen interface.
    //
    // Author: Jean-Marc Bourguet
    //
    // This file shares part of the implementation with the MS Windows one by
    // copying the code. A better way would be to separate platform-specific
    // and platform-independent code into different translation units.
    // However, this would complicate the build steps and considering the code
    // is intended to be used (if not understood) by novices, this has not been
    // done.

    // Implemented interface

    #include "keyboard.h"
    #include "mouse.h"
    #include "playpen.h"

    // C++ standard headers

    #include <assert.h>
    #include <map>
    #include <stdexcept>
    #include <stdlib.h>
    #include <streambuf>

    // Posix headers

    #include <pthread.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <termios.h>
    #include <unistd.h>

    // X Window headers

    #include <X11/Xlib.h>
    #include <X11/Xutil.h>
    #include <X11/keysym.h>

    namespace
    {

    using namespace studentgraphics;

    int const key_unknown = 0xFE;

    //
    ======================================================================
    // Platform-independant utility classes
    //
    ======================================================================

    //
    **********************************************************************
    // Inherit privately from this class to disable copy constructor and
    // assignment operator

    class CopyDisabler
    {
    public:
    CopyDisabler();

    private:
    CopyDisabler(CopyDisabler const&);
    CopyDisabler& operator=(CopyDisabler const&);
    };

    inline
    CopyDisabler::CopyDisabler()
    {
    }

    //
    **********************************************************************
    // The mapping between hue and RGB value

    struct HueRGB256
    {
    HueRGB rgbs[colours];

    HueRGB256();
    };

    HueRGB256::HueRGB256()
    {
    memset(rgbs, 0, sizeof(HueRGB) * colours);
    -----------------------------

    The compiler errors are as follows:

    ||=== Build: Release in fgw (compiler: GNU GCC Compiler) ===|
    /home/n/tutorial/source/playpen_unix1.cpp||In constructor
    ‘{anonymous}::HueRGB256::HueRGB256()’:|
    /home/n/tutorial/source/playpen_unix1.cpp|82|error: ‘memset’ was not
    declared in this scope|
    /home/n/tutorial/source/playpen_unix1.cpp||In member function ‘void
    {anonymous}::pixels::Clear(studentgraphics::hue)’:|
    /home/n/tutorial/source/playpen_unix1.cpp|120|error: ‘memset’ was not
    declared in this scope|
    /home/n/tutorial/source/playpen_unix1.cpp||In member function ‘void*
    {anonymous}::SingletonWindowImpl::WorkerThread()’:|
    /home/n/tutorial/source/playpen_unix1.cpp|685|warning: suggest
    parentheses around comparison in operand of ‘&’ [-Wparentheses]|
    /home/n/tutorial/source/playpen_unix1.cpp|330|warning:
    ‘{anonymous}::SingletonWindowImpl::instance_’ defined but not used
    [-Wunused-variable]|
    ||=== Build failed: 2 error(s), 2 warning(s) (0 minute(s), 11 second(s))
    ===|
    --------------------------------

    So both errors relate to 'memset' not being declared. Can anyone see how
    I can correct this?

    Thanks
     
    nvangogh, May 27, 2014
    #1
    1. Advertisements

  2. nvangogh

    nvangogh Guest

    It's ok guys - I found the answer in an old usenet post:

    #include <cstring>
    using std::memset;
     
    nvangogh, May 27, 2014
    #2
    1. Advertisements

  3. memset() is, if I'm not completely mistaken. declared in <cstring>,
    so if you add it to your include files the compiler should be happy.

    Regards, Jens
     
    Jens Thoms Toerring, May 27, 2014
    #3
  4. nvangogh

    Jorgen Grahn Guest

    memset comes, according to my C language man pages, from <string.h>,
    so #include <cstring> and call it std::memset and you should be fine.

    But note that it's a slightly bad idea to do what the author does here:
    initializing an array of HueRGB objects by paving it over with 0 bytes.

    Give HueRGB a default constructor and you don't have to do anything
    here. Or, use std::fill() as a "type-aware memset".

    /Jorgen
     
    Jorgen Grahn, May 27, 2014
    #4
  5. nvangogh

    nvangogh Guest

    Thanks
     
    nvangogh, May 27, 2014
    #5
  6. nvangogh

    nvangogh Guest

    Thanks - for the moment I will leave it as it is because I don't
    understand this code. I got the library compiled now.
     
    nvangogh, May 27, 2014
    #6

  7. Add

    Try including the header that has the declaration of it.

    V
     
    Victor Bazarov, May 27, 2014
    #7
  8. Could it be because memset is in the std namespace and you are not?
    Would std::memset(..) work?

    Recommend including the C++ headers cassert and cstdlib instead of the
    C headers as coded.
     
    Barry Schwarz, May 27, 2014
    #8
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.