8

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 a function is declared with an extern in its signature, is it necessary for the corresponding header file to be included inside a extern "C" block?

As pointed by another user in comments, the marked duplicate does not fully satisfy the question here. I am editing so that in future others may not be mislead into a different question.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Mohamed Iqzas
  • 976
  • 1
  • 14
  • 19
  • 1
    http://en.cppreference.com/w/cpp/language/language_linkage – Ivo Peterka Jan 20 '15 at 14:57
  • 2
    It seems that none of the answers in the "duplicate" question (and almost none to this question) describe what `extern` without "C" does (or how it differs from `extren "C"`). Here's the [docs](http://en.cppreference.com/w/cpp/language/storage_duration) and here's a stackoveflow [faq](http://stackoverflow.com/questions/1358400/what-is-external-linkage-and-internal-linkage-in-c) about it. Then there's another use with [templates](http://en.cppreference.com/w/cpp/language/class_template). – eerorika Jan 20 '15 at 15:08
  • @user2079303: if you would have answered intead of a comment i would have accepted your answer. Thank you very much. – Mohamed Iqzas Jan 21 '15 at 08:02
  • 1
    @MohamedIqzas I couldn't have answered since the question was already closed, unfortunately. – eerorika Jan 21 '15 at 08:31

3 Answers3

7

extern "C" disables name mangling. It will allow your C++ code to call functions from library compiled by C compiler

lowtech
  • 2,492
  • 2
  • 22
  • 31
4
extern "C" int someFunction( void *ret_val);  

will make someFunction have C linkage.

haccks
  • 104,019
  • 25
  • 176
  • 264
2

The presence of extern "C" in a C++ file is a personal intent to disable name-mangling transformation that the C++ compiler does to the functions in that file. When there is no name-mangling, then a client C code can call these functions. This is done when you have a mixture of C/C++ code and you need to keep track of language-specific features. In a bit more geeky way, the C linkage becomes compatible in presence of a Cpp compiler.

The code could be anything from a variable/typedef to a full function/module declaration.

But if you do this:

extern char c; // same goes true for extern int foo()

it means that you are saying "I am using char c, which has a declaration external to this file". More like in another module somewhere in the search-path. This is implicitly global. In runtime, if c changes, the change is reflected everywhere. This is provided that your compiler directives such as -Iinclude_file_dirs -Ssource_file_dirs etc. are provided correctly (on GCC or g++). Using a powerful IDE such as Visual Studio 2010 or later, you can do these very easily.

"extern" is a linkage keyword. You can combine it with "C" for compiler-specific linkage directives.

daparic
  • 3,794
  • 2
  • 36
  • 38
ha9u63a7
  • 6,233
  • 16
  • 73
  • 108
  • @ha963ar: Thank You. Is the statement "This is implicitly global, static" correct? I read that extern and static are different things in the links posted by user2079303 in question comments. – Mohamed Iqzas Jan 21 '15 at 08:21
  • 1
    @MohamedIqzas Thanks for pointing that out. It is definitely implicit Global (but not static). Static means the scope of the function/variable is as long as the programme. Corrected my answer now :) – ha9u63a7 Jan 21 '15 at 10:03
  • 1
    "extern "C" simply means that the following block of code can be compiled either using C or Cpp compiler." - that's wrong. – el.pescado - нет войне Jul 01 '16 at 13:27