extern-c

What does mean for a name or type to have a certain language linkage?

≯℡__Kan透↙ 提交于 2019-12-03 05:31:35
问题 According to (c) ANSI ISO/IEC 14882:2003, page 127: Linkage specifications nest. When linkage specifications nest, the innermost one determines the language. A linkage specification does not establish a scope. A linkage-specification shall occur only in namespace scope (3.3). In a linkage-specification, the specified language linkage applies to the function types of all function declarators, function names, and variable names introduced by the declaration(s). extern "C" void f1(void(*pf)(int)

What does mean for a name or type to have a certain language linkage?

淺唱寂寞╮ 提交于 2019-12-02 19:01:44
According to (c) ANSI ISO/IEC 14882:2003, page 127: Linkage specifications nest. When linkage specifications nest, the innermost one determines the language. A linkage specification does not establish a scope. A linkage-specification shall occur only in namespace scope (3.3). In a linkage-specification, the specified language linkage applies to the function types of all function declarators, function names, and variable names introduced by the declaration(s). extern "C" void f1(void(*pf)(int)); // the name f1 and its function type have C language // linkage; pf is a pointer to a C function

When to use extern “C” in simple words? [duplicate]

别来无恙 提交于 2019-11-29 01:07:55
This question already has an answer here: What is the effect of extern “C” in C++? 13 answers Maybe I'm not understanding the differences between C and C++, but when and why do we need to use extern "C" { ? Apparently its a "linkage convention". I read about it briefly and noticed that all the .h header files included with MSVS surround their code with it. What type of code exactly is "C code" and NOT "C++ code"? I thought C++ included all C code? I'm guessing that this is not the case and that C++ is different and that standard features/functions exist in one or the other but not both (ie:

Why do you need “extern C” for C++ callbacks to C functions?

笑着哭i 提交于 2019-11-28 11:56:37
I find such examples in Boost code. namespace boost { namespace { extern "C" void *thread_proxy(void *f) { .... } } // anonymous void thread::thread_start(...) { ... pthread_create(something,0,&thread_proxy,something_else); ... } } // boost Why do you actually need this extern "C" ? It is clear that the thread_proxy function is private internal and I do not expect that it would be mangled as "thread_proxy" because I actually do not need it mangled at all. In fact, in all my code that I had written and that runs on many platforms, I never used extern "C" and this had worked as-is with normal

When to use extern “C” in C++? [duplicate]

杀马特。学长 韩版系。学妹 提交于 2019-11-28 04:33:34
Possible Duplicate: Why do we need extern “C”{ #include <foo.h> } in C++? I have often seen programs coded like: extern "C" bool doSomeWork() { // return true; } Why do we use an extern "C" block? Can we replace this with something in C++? Is there any advantage to using extern "C" ? I do see a link explaining this but why do we need to compile something in C when we already have C++? extern "C" makes names not mangled. It used when: We need to use some C library in C++ extern "C" int foo(int); We need export some C++ code to C extern "C" int foo(int) { something; } We need an ability to

Why do you need “extern C” for C++ callbacks to C functions?

大兔子大兔子 提交于 2019-11-27 19:22:06
问题 I find such examples in Boost code. namespace boost { namespace { extern "C" void *thread_proxy(void *f) { .... } } // anonymous void thread::thread_start(...) { ... pthread_create(something,0,&thread_proxy,something_else); ... } } // boost Why do you actually need this extern "C" ? It is clear that the thread_proxy function is private internal and I do not expect that it would be mangled as "thread_proxy" because I actually do not need it mangled at all. In fact, in all my code that I had

Is extern “C” only required on the function declaration?

孤街醉人 提交于 2019-11-27 19:19:18
I wrote a C++ function that I need to call from a C program. To make it callable from C, I specified extern "C" on the function declaration . I then compiled the C++ code, but the compiler (Dignus Systems/C++) generated a mangled name for the function. So, it apparently did not honor the extern "C" . To resolve this, I added extern "C" to the function definition . After this, the compiler generated a function name that is callable from C. Technically, the extern "C" only needs to be specified on the function declaration. Is this right? (The C++ FAQ Lite has a good example of this.) Should you

When to use extern “C” in simple words? [duplicate]

冷暖自知 提交于 2019-11-27 15:37:40
问题 This question already has an answer here: What is the effect of extern “C” in C++? 13 answers Maybe I'm not understanding the differences between C and C++, but when and why do we need to use extern "C" { ? Apparently its a "linkage convention". I read about it briefly and noticed that all the .h header files included with MSVS surround their code with it. What type of code exactly is "C code" and NOT "C++ code"? I thought C++ included all C code? I'm guessing that this is not the case and

How does an extern “C” declaration work?

倖福魔咒の 提交于 2019-11-27 06:54:35
I'm taking a programming languages course and we're talking about the extern "C" declaration. How does this declaration work at a deeper level other than "it interfaces C and C++"? How does this affect the bindings that take place in the program as well? extern "C" is used to ensure that the symbols following are not mangled (decorated). Example: Let's say we have the following code in a file called test.cpp : extern "C" { int foo() { return 1; } } int bar() { return 1; } If you run gcc -c test.cpp -o test.o Take a look at the symbols names: 00000010 T _Z3barv 00000000 T foo foo() keeps its

static vs extern “C”/“C++”

房东的猫 提交于 2019-11-27 00:58:20
What is the difference between a static member function and an extern "C" linkage function ? For instance, when using "makecontext" in C++, I need to pass a pointer to function. Google recommends using extern "C" linkage for it, because "makecontext" is C. But I found out that using static works as well. Am I just lucky or... class X { public: static void proxy(int i) {} } makecontext(..., (void (*)(void)) X::proxy, ...); vs extern "C" void proxy(int i) {} makecontext(..., (void (*)(void)) proxy, ...); EDIT: Can you show a compiler or architecture where the static member version does not work