extern-c

What kinds of C++ functions can be placed in a C function pointer?

∥☆過路亽.° 提交于 2020-06-11 22:33:01
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

不羁岁月 提交于 2020-06-11 22:32:34
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

南楼画角 提交于 2020-06-11 22:32:30
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

What kinds of C++ functions can be placed in a C function pointer?

匆匆过客 提交于 2020-06-11 22:32:11
问题 I have a C library that uses a struct of function pointers for callbacks. The callbacks will be called from C code. extern "C" { typedef struct callbacks_t { void (*foo) (const char*); int (*bar) (int); } callbacks_t; }// extern C What kinds of C++ functions can I safely place in those function pointers to be called from the C library? Static member functions? Fully specified template functions? Non-capturing Lambdas? g++ seemingly lets me use all of the above, but I question the safety when

How does extern “C” work in C++? [duplicate]

旧城冷巷雨未停 提交于 2020-01-24 09:44:07
问题 This question already has answers here : What is the effect of extern “C” in C++? (13 answers) Closed 3 years ago . I see some code in C++ using extern "C" at the beginning of the file like this: #ifdef __cplusplus extern "C" {} #endif What does this mean? How does it work? 回答1: It's probably not like that, but more like: #ifdef __cplusplus extern "C" { #endif //some includes or declarations #ifdef __cplusplus } #endif It tells the compiler to use C name mangling for whatever is declared

Is it possible to subclass a C struct in C++ and use pointers to the struct in C code?

泄露秘密 提交于 2019-12-20 16:47:05
问题 Is there a side effect in doing this: C code: struct foo { int k; }; int ret_foo(const struct foo* f){ return f.k; } C++ code: class bar : public foo { int my_bar() { return ret_foo( (foo)this ); } }; There's an extern "C" around the C++ code and each code is inside its own compilation unit. Is this portable across compilers? 回答1: This is entirely legal. In C++, classes and structs are identical concepts, with the exception that all struct members are public by default. That's the only

Could we use extern “C” in C file without #ifdef __cplusplus?

偶尔善良 提交于 2019-12-17 16:26:31
问题 Why shouldn’t extern "C" be specified for a function that needs to be defined as a C function? What effect would that have on the compiler when compiling the file as a C source? If there is no effect on the C compiler, can’t we just define a function in a header file as below by removing the #ifdef __cplusplus check? extern "C" { int MyFunc(); } An answer to another question says that the #ifdef is needed, but I don’t understand why: Regarding #2: __cplusplus will be defined for any

How does an extern “C” declaration work?

丶灬走出姿态 提交于 2019-12-17 08:48:06
问题 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? 回答1: 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

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

亡梦爱人 提交于 2019-12-17 06:35:38
问题 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))

error: expected unqualified-id on extern “C”

让人想犯罪 __ 提交于 2019-12-14 02:32:16
问题 I have a cpp code in which I want to call a c function. Both compile well to .o files, but when the clang++ is executing for compilation, I receive the following error: file.cpp:74:12: error: expected unqualified-id extern "C" ^ The code in the cpp file is the following: void parseExtern(QString str) { #ifdef __cplusplus extern "C" { #endif function_in_C(str); #ifdef __cplusplus } #endif } How can I avoid the error ? I can't compile the c file with clang++, I really need to use extern. Thanks