extern-c

extern “C” functions in compiled object code

对着背影说爱祢 提交于 2019-12-13 20:07:06
问题 Environment is Microsoft Visual C++ 2015 and Windows 7. Is there anything special about inline extern "C" functions defined in a header? I am consuming an SDK in which one of the headers contain such a beast. In my application I have a lone TU (translation unit) whose only job in life is to include the aforementioned header. That's all. Nothing else is in it. If I dig into the generated object file, I see the extern "C" function being pulled in. This is causing me some unwanted side effects

Why does Visual Studio fail to give an undefined reference error when extern “C” is specified?

狂风中的少年 提交于 2019-12-10 20:08:03
问题 Given this code: A2.H _declspec(dllimport) void SomeFunc(); struct Foo { Foo(); ~Foo(); }; inline Foo::Foo() { } inline Foo::~Foo() { SomeFunc(); } A1.H #include "A2.h" extern "C" void TriggerIssue(); // <-- This! extern "C" inline void TriggerIssue() { Foo f; } MyTest.cpp #include "A1.h" int main() { return 0; } Please see here for a background to the issue. When MyTest.cpp is compiled into an executable, the linker complains that SomeFunc() is an unresolved external. This seems to be caused

how does extern “C” allow C++ code in a C file?

我只是一个虾纸丫 提交于 2019-12-10 04:20:32
问题 In order to use C++ code in a C file, I read that we can just do extern "C" { (where the c++ code goes here)} , but when I try printing something out using cout, I keep getting an error because it does not recognize the library . I think I am just confused on how extern "C" allows you to use C++ code in C. 回答1: The opposite is true. You can use extern C to add code you want to compile as C code using a C++ compiler. Unless I'm missing something you can't compile C++ code with a C compiler.

difference between extern “C” and simply extern [duplicate]

懵懂的女人 提交于 2019-12-07 11:58:59
问题 This question already has answers here : What is the effect of extern “C” in C++? (13 answers) Closed 4 years ago . I have seen C/C++ code using extern "C" declared in function signatures and also while including a C header into a CPP file. but some functions just declare extern before their signature(without the "C"). QN1: are these both ways of defining functions have same effect or do they imply different things? sorry if I am very silly but I am not able to find this difference through

extern and extern “C” for variables

≡放荡痞女 提交于 2019-12-06 02:29:48
问题 I'm writing a C++ shared library for a C program to use. However, I have a question about extern and extern "C" . Consider the following code My header file is like this: #ifdef __cplusplus extern "C" int global; extern "C" int addnumbers(int a, int b); #else extern int global; #endif This works perfectly fine; I just have to declare int global; in either my .cpp or my .c file. However, what I don't understand is: What is the difference between extern "C" and extern here? I tried commenting

difference between extern “C” and simply extern [duplicate]

爷,独闯天下 提交于 2019-12-06 01:44:23
This question already has answers here : What is the effect of extern “C” in C++? (13 answers) Closed 4 years ago . I have seen C/C++ code using extern "C" declared in function signatures and also while including a C header into a CPP file. but some functions just declare extern before their signature(without the "C"). QN1: are these both ways of defining functions have same effect or do they imply different things? sorry if I am very silly but I am not able to find this difference through Google. Eg: extern int someFunction( void *ret_val); extern "C" int someFunction( void *ret_val); QN2: if

Does extern “C” have any effect in C?

扶醉桌前 提交于 2019-12-04 16:50:34
问题 I just got some C code that uses extern "C" to declare external functions like this: extern "C" void func(); Is this valid C? I'm getting an error at this line, but I'm not sure if it's because of this or something else. 回答1: No, it's not valid C. It should only be used in C++ code to refer to functions defined in C code. The extern "C" should be surrounded in a ifdef __cplusplus / #endif block: // For one function #ifdef __cplusplus extern "C" #endif void func(); // For more than one

Can C++ functions marked as Extern “C” throw?

我的梦境 提交于 2019-12-04 16:24:57
问题 I've got C++ functions that I want to declare using extern "C" even though they are only called in C++ code. Yes, I know this is strange but it's something I would like to do for consistency since we have mixed C and C++ declarations. I just want to make sure that declaring a C++ function as extern "C" won't affect the behavior of throwing. It would look something like this: extern "C" void foo() {throw exception;} int bar() { try { foo(); } catch (exception e) { return 1; } } 回答1: "Can C++

extern and extern “C” for variables

不羁岁月 提交于 2019-12-04 08:49:16
I'm writing a C++ shared library for a C program to use. However, I have a question about extern and extern "C" . Consider the following code My header file is like this: #ifdef __cplusplus extern "C" int global; extern "C" int addnumbers(int a, int b); #else extern int global; #endif This works perfectly fine; I just have to declare int global; in either my .cpp or my .c file. However, what I don't understand is: What is the difference between extern "C" and extern here? I tried commenting out extern "C" int global and it works! Why? I know that extern "C" is used for making C linkage. That's

Does extern “C” have any effect in C?

家住魔仙堡 提交于 2019-12-03 10:36:24
I just got some C code that uses extern "C" to declare external functions like this: extern "C" void func(); Is this valid C? I'm getting an error at this line, but I'm not sure if it's because of this or something else. No, it's not valid C. It should only be used in C++ code to refer to functions defined in C code. The extern "C" should be surrounded in a ifdef __cplusplus / #endif block: // For one function #ifdef __cplusplus extern "C" #endif void func(); // For more than one function #ifdef __cplusplus extern "C" { #endif void func1(); void func2(); #ifdef __cplusplus } #endif this is a C