extern-c

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

こ雲淡風輕ζ 提交于 2019-11-27 00:30:21
问题 This question already has answers here : Closed 10 years ago . 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++? 回答1: extern "C" makes names not mangled. It

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

不想你离开。 提交于 2019-11-26 19:49:25
问题 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

Call a C function from C++ code

♀尐吖头ヾ 提交于 2019-11-26 15:41:53
I have a C function that I would like to call from C++. I couldn't use " extern "C" void foo() " kind of approach because the C function failed to be compiled using g++. But it compiles fine using gcc. Any ideas how to call the function from C++? Compile the C code like this: gcc -c -o somecode.o somecode.c Then the C++ code like this: g++ -c -o othercode.o othercode.cpp Then link them together, with the C++ linker: g++ -o yourprogram somecode.o othercode.o You also have to tell the C++ compiler a C header is coming when you include the declaration for the C function. So othercode.cpp begins

Call a C function from C++ code

岁酱吖の 提交于 2019-11-26 04:33:17
问题 I have a C function that I would like to call from C++. I couldn\'t use \" extern \"C\" void foo() \" kind of approach because the C function failed to be compiled using g++. But it compiles fine using gcc. Any ideas how to call the function from C++? 回答1: Compile the C code like this: gcc -c -o somecode.o somecode.c Then the C++ code like this: g++ -c -o othercode.o othercode.cpp Then link them together, with the C++ linker: g++ -o yourprogram somecode.o othercode.o You also have to tell the

Why do we need extern “C”{ #include <foo.h> } in C++?

此生再无相见时 提交于 2019-11-26 02:57:19
Why do we need to use: extern "C" { #include <foo.h> } Specifically: When should we use it? What is happening at the compiler/linker level that requires us to use it? How in terms of compilation/linking does this solve the problems which require us to use it? C and C++ are superficially similar, but each compiles into a very different set of code. When you include a header file with a C++ compiler, the compiler is expecting C++ code. If, however, it is a C header, then the compiler expects the data contained in the header file to be compiled to a certain format—the C++ 'ABI', or 'Application

Combining C++ and C - how does #ifdef __cplusplus work?

我与影子孤独终老i 提交于 2019-11-26 01:23:07
问题 I\'m working on a project that has a lot of legacy C code. We\'ve started writing in C++, with the intent to eventually convert the legacy code, as well. I\'m a little confused about how the C and C++ interact. I understand that by wrapping the C code with extern \"C\" the C++ compiler will not mangle the C code\'s names, but I\'m not entirely sure how to implement this. So, at the top of each C header file (after the include guards), we have #ifdef __cplusplus extern \"C\" { #endif and at

Why do we need extern “C”{ #include <foo.h> } in C++?

南楼画角 提交于 2019-11-26 01:12:33
问题 Why do we need to use: extern \"C\" { #include <foo.h> } Specifically: When should we use it? What is happening at the compiler/linker level that requires us to use it? How in terms of compilation/linking does this solve the problems which require us to use it? 回答1: C and C++ are superficially similar, but each compiles into a very different set of code. When you include a header file with a C++ compiler, the compiler is expecting C++ code. If, however, it is a C header, then the compiler

How to call C++ function from C?

偶尔善良 提交于 2019-11-26 00:57:52
问题 I know this. Calling C function from C++: If my application was in C++ and I had to call functions from a library written in C. Then I would have used //main.cpp extern \"C\" void C_library_function(int x, int y);//prototype C_library_function(2,4);// directly using it. This wouldn\'t mangle the name C_library_function and linker would find the same name in its input *.lib files and problem is solved. Calling C++ function from C??? But here I\'m extending a large application which is written

What is the effect of extern “C” in C++?

纵然是瞬间 提交于 2019-11-25 21:36:57
问题 What exactly does putting extern \"C\" into C++ code do? For example: extern \"C\" { void foo(); } 回答1: extern "C" makes a function-name in C++ have 'C' linkage (compiler does not mangle the name) so that client C code can link to (i.e use) your function using a 'C' compatible header file that contains just the declaration of your function. Your function definition is contained in a binary format (that was compiled by your C++ compiler) that the client 'C' linker will then link to using the

How to call C++ function from C?

筅森魡賤 提交于 2019-11-25 20:40:17
I know this. Calling C function from C++: If my application was in C++ and I had to call functions from a library written in C. Then I would have used //main.cpp extern "C" void C_library_function(int x, int y);//prototype C_library_function(2,4);// directly using it. This wouldn't mangle the name C_library_function and linker would find the same name in its input *.lib files and problem is solved. Calling C++ function from C??? But here I'm extending a large application which is written in C and I need to use a library which is written in C++. Name mangling of C++ is causing trouble here.