C ++ Extern / Multiple Definitions

I am trying to interact with Ada in C ++ using externs. What is the difference between these two implementations?

Implementation A

namespace Ada
{
    extern "C"
    {
        int getNumber();
        int index;
        int value;
    }
}

Implementation B

namespace Ada
{
    extern "C"
    {
        int getNumber();
    }
    extern "C" int index;
    extern "C" int value;
}

Both implementations compile just fine. But Impl-A fails to bind, I get a multiple definition error for index and value. I'm just trying to understand the differences.

+3
source share
3 answers

extern "C" , "C" . , , c. , extern int . , , - int named index int named, . -A ints - extern "C" , c .

, , , . (), , .

. Charle , ++.

+7

(.. extern "C" extern "C++"), , , , , , extern , . (7.5 .7 ++ 03)

:

extern "C" { int a; } // a is declared and defined

extern "C" int a; // a is just a declaration (as if extern int a;)

extern "C" int a = 0; // a is a definition because of the initializer.
+7

, ,

namespace Ada
{
    extern "C"
    {
        int getNumber();
        extern int index;
        extern int value;
    }
}

index value, . (. .)

+3

Source: https://habr.com/ru/post/1763157/


All Articles