The extern "C" construct - does it allow the use of C-style (int-based) enums?

Discussion in 'C++' started by Chris Cahoon, Jun 12, 2007.

  1. Chris  Cahoon

    Chris Cahoon Guest

    Hello,

    I am trying to include a header file which was written in C, but my
    project is in C++. Even when I wrap the include statement or the
    entire header files with extern "C" (used according to the
    instructions on c++-faq-lite), I get the same error, which is this:

    invalid conversion from `int' to `DmtxDirection'

    where DmtxDirection is an enum with integer values inside (obviously).

    The offending function:

    static DmtxDirection
    TurnCorner(DmtxDirection dir, int turn)
    {
    DmtxDirection newDir;

    newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >> 1) : 0x0f & (dir
    << 1);

    return (newDir) ? newDir : (dir ^ 0x0f) & 0x09;
    }

    And the enum:

    typedef enum {
    DmtxDirNone = 0x00,
    DmtxDirUp = 0x01 << 0,
    DmtxDirLeft = 0x01 << 1,
    DmtxDirDown = 0x01 << 2,
    DmtxDirRight = 0x01 << 3,
    DmtxDirHorizontal = DmtxDirLeft | DmtxDirRight,
    DmtxDirVertical = DmtxDirUp | DmtxDirDown,
    DmtxDirRightUp = DmtxDirRight | DmtxDirUp,
    DmtxDirLeftDown = DmtxDirLeft | DmtxDirDown
    } DmtxDirection;


    Long story short - should the extern "C" construct allow the library
    to use enums in this manner, or do I need to take a different course?

    Thanks for any advice.

    Chris
     
    Chris Cahoon, Jun 12, 2007
    #1
    1. Advertising

  2. Chris  Cahoon

    Dizzy Guest

    Chris Cahoon wrote:

    > Hello,

    <snip>

    > Long story short - should the extern "C" construct allow the library
    > to use enums in this manner, or do I need to take a different course?


    It does not. extern "C" doesn't alter the behaivour of not allowing implicit
    conversion from int to enum. However, you can use static_cast for that,
    static_cast<DmtxDirection>(value).

    --
    Dizzy
     
    Dizzy, Jun 12, 2007
    #2
    1. Advertising

  3. Re: The extern "C" construct - does it allow the use of C-style (int-based)enums?

    Chris Cahoon wrote:
    > Hello,
    >
    > I am trying to include a header file which was written in C, but my
    > project is in C++. Even when I wrap the include statement or the
    > entire header files with extern "C" (used according to the
    > instructions on c++-faq-lite), I get the same error, which is this:
    >
    > invalid conversion from `int' to `DmtxDirection'
    >
    > where DmtxDirection is an enum with integer values inside (obviously).


    extern "C" changes "linkage" of functions and variables (i.e., it
    affects the symbol names in the object files). It doesn't affect enums
    in any way. You might want to add a type cast from int to enum to your code:

    >
    > The offending function:
    >
    > static DmtxDirection
    > TurnCorner(DmtxDirection dir, int turn)
    > {
    > DmtxDirection newDir;
    >
    > newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >> 1) : 0x0f & (dir
    > << 1);


    newDir = static_cast <DmtxDirection> ((turn = DMTX_TURN_CW ...


    D.
     
    Davlet Panech, Jun 12, 2007
    #3
  4. Chris  Cahoon

    James Kanze Guest

    On Jun 12, 6:14 pm, Davlet Panech <> wrote:
    > Chris Cahoon wrote:
    > > I am trying to include a header file which was written in C, but my
    > > project is in C++. Even when I wrap the include statement or the
    > > entire header files with extern "C" (used according to the
    > > instructions on c++-faq-lite), I get the same error, which is this:


    > > invalid conversion from `int' to `DmtxDirection'


    > > where DmtxDirection is an enum with integer values inside (obviously).


    > extern "C" changes "linkage" of functions and variables (i.e., it
    > affects the symbol names in the object files). It doesn't affect enums
    > in any way. You might want to add a type cast from int to enum to your code:


    > > The offending function:


    > > static DmtxDirection
    > > TurnCorner(DmtxDirection dir, int turn)
    > > {
    > > DmtxDirection newDir;


    > > newDir = (turn == DMTX_TURN_CW) ? 0x0f & (dir >> 1) : 0x0f & (dir
    > > << 1);


    > newDir = static_cast <DmtxDirection> ((turn = DMTX_TURN_CW ...


    Which will cause a few problems when the header is compiled as
    C:). If he has access to the header, and can modify it, then
    he can use a C style cast here. If he doesn't, he'll need to
    "pre-process" the header somehow, and include the pre-processed
    version. (If he's using make, it's almost trivial to get the
    system to invoke the preprocessing automatically. If he's using
    an IDE, I don't know.)

    --
    James Kanze (GABI Software, from CAI) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 13, 2007
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  2. Hal Styli
    Replies:
    14
    Views:
    1,710
    Old Wolf
    Jan 20, 2004
  3. =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=

    Enums without identifier, enums and typedef

    =?utf-8?b?QXNiasO4cm4gU8OmYsO4?=, Jan 19, 2007, in forum: C Programming
    Replies:
    10
    Views:
    1,193
    Keith Thompson
    Jan 20, 2007
  4. sinbad

    extern int *a , int a[10]

    sinbad, Aug 10, 2007, in forum: C Programming
    Replies:
    6
    Views:
    556
    Keith Thompson
    Aug 10, 2007
  5. Mukesh
    Replies:
    4
    Views:
    647
    Paul N
    Mar 26, 2010
Loading...

Share This Page