Best practice and semantics of nested namespace functions and using extern "C"

I am creating a C ++ library with a C-ABI interface.

Thus, GCC considers the extern "C" qualifier with respect to mangling:

namespace x {

    extern "C" int monkey(int x) {
        return 1;
    }

    int chimpanzee(int x) {
        return 1;
    }
}

Corresponding conclusion nm:

00000000004005cd T _ZN1x10chimpanzeeEi
00000000004005bf T monkey

Question: I want to leave the functions that are involved in C-ABI inside the namespace, for maximum flexibility for reuse. Important Note: After compiling the library, I will give the linker a map file (GCC) or module definition file (MSVC).

  • Is the standard output behavior - will the other main compilers (MSVC in specific) smooth the bands?
  • - , ABI?
  • C-ABI ?
+3
2

MSVC.

, ( ), . , .

, :

1) , . , , C- extern "C":

7.5.3 [ ]

, C , "C" ++, "++". [:

complex sqrt(complex); // C + + linkage by default 
extern "C" { double sqrt(double); // C linkage } 

-end ]

, C namespace s, extern "C" , . ...

3) , . :

main.h

#ifndef MAIN_API
#   define MAIN_API __declspec(dllexport)
#endif

namespace x
{
    extern "C" MAIN_API void foo();
};

namespace y
{
    extern "C" MAIN_API void foo();
};

main.cpp

#include <cstdlib>
#include <iostream>
using namespace std;
#define MAIN_API __declspec(dllexport)
#include "main.h"

void x::foo()
{
    cout << "x::foo()\n";
}

void y::foo()
{
    cout << "y::foo()\n";
}

int main()
{
}

, extern "C" -ed x::foo() y::foo() , : foo()

2) . C-ABI , , , , . - namespace . - :

#ifndef MAIN_API
#   define MAIN_API __declspec(dllexport)
#endif

namespace x
{
    extern "C" MAIN_API void x_foo();
};

namespace y
{
    extern "C" MAIN_API void y_foo();
};
+2

, , , . ++, 3rd Edition, . 208: " C-. ++, , . printf() std ... std::printf() C printf()."

+5

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


All Articles