extern "C" doubt

1
2
3
 extern "C" {
		char *getwd(char *);
	};         


May I know what does this code means? As I know when there is any C header then we should use extern "C" if we want to compile them into a c++ module.Am i right?
But what is char *getwd(char *); since it is not a header of C. I need explicit explanation and simple example.Thanks.
Last edited on
There is no difference between that and:
1
2
3
4
5
6
7
//header.h
char *getwd(char *);

//Main.cpp
 extern "C" {
#include "header.h"
}; 
Because #include directive just copy-pasting all content of header into target file.
Some info on extern "C":
http://stackoverflow.com/questions/1041866/in-c-source-what-is-the-effect-of-extern-c
http://www.cplusplus.com/forum/general/1143/
1
2
3
extern "C" {
		char *getwd(char *);
	};



DOes the above code doin anythg?Since char * doesnt state where the path of current directory save to.The code above actually done ntg or is doing thing as you mentioned in the previous post ? How i know char *getwd(char *); is written in header.h ?
1. char *getwd(char *); is a declaration of a function that takes a char pointer, returns a char pointer, and has name 'getwd'. Where did you find that piece of code?
man getwd wrote:
For portability and security reasons, use of getwd() is deprecated.


2. The extern "C" tells the linker and the C++ compiler that this declared function prototype corresponds to an implementation of the function that has been compiled with C compiler.

3. When you use functions and types written by someone else and available as libraries, you also use the documentation of those libraries to find out what they have and do.
It is telling linker that that fuction is using C-style calls, name mangling etc. So you can link file containing that function which was compiled by C-compiler.

Basically it tells: this function was compiled in C, not C++. It is special, threat it accordingly during code generation.
> Basically it tells: this function was compiled in C, not C++.

It tells: This function has C linkage (name binding, parameter passing and return).

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <functional>
#include <initializer_list>
 
extern "C" void foo( std::initializer_list<int> init_lst, std::function< void(int) > fn )
{ for( const auto& i : init_lst ) fn(i) ; }
 
int main()
{
    foo( { 0, 1, 2, 3, 4 }, [] ( int i ) { std::cout << i << ' ' ; } ) ;
}


http://ideone.com/kISTPX
Topic archived. No new replies allowed.