Understanding of extern "C"

In this example, there are two files, namely f1.c and test.cc.

f1.c

void f1()
{
	return;
}


test.cc

extern "C" {
	extern void f1();
}

int main()
{
	f1();
	return 0;
}

Compiler will compile the source code according to the format of the files. Provided that extern "C" is not used, function f1 in f1.c will be a short name compared to a long name in test.cc in assemble language due to overload in C++.  With extern "C", developers can use C in C++.

We can make C style compile in .cpp or .cc files by extern "C".

f2.cc

extern "C" {
	void f2()
	{
		return;
	}
}

test.cc

extern "C" {
	extern void f2();
}

int main()
{
	f2();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章