Thursday, November 27, 2014

Linking C and C++ functions using extern specifier

The way C and C++ compilers generate the assembly code is a little different, this could give lots of linker errors while trying to link a function written in a C file in a C++ code or vice-versa when using the extern specifier.

Always remember that C/C++ codes are directly converted to object and assembly codes that are just linear in nature like:

_printf_:
;Actual code to print
 _main_:
call _printf_    ;now the actual code under label _printf_ is called

Something like goto style code. Thus, printf() function might be atlast converted to _printf_ label in the object/assembly code. This label is used for linking stage.

Lets take a project which contains one C file and one CPP file.

test1.c
void function_c()
{
printf("From C");
}

test2.cpp
extern function_c();
void function_cpp()
{
function_c();
}

This will not work because C compiler generates function name for function_c something like "_function_c_". But the C++ compiler (as it supports function overloading concepts) looks for the label "?function_c@@YAXXZ", hence it wont compile. To resolve this use the extern "C" option:

test2.cpp
extern "C" function_c();
void function_cpp()
{
function_c();
}

For more insights, read about Name mangling in C++: http://en.wikipedia.org/wiki/Name_mangling#Name_mangling_in_C.2B.2B

Wednesday, January 1, 2014

Learn to customize an OS and create your own flavour

Its always a dream for geeky IT students to customize OS code to add some stuffs like, adding an additional screen during OS boots, adding your name in right click menu everything. But since they don't get proper guidance and direction to explore about this, most of them get bored and their enthusiasm goes down.

Students! What if you get a very good e-course which will teach you FROM SCRATCH everything required (technologies, tools, mind sets, innovative ideas) to download a live OS code, open the code and do something geeky with it, play with the Kernel, may be modify the OS TCP/IP code to be more effective and lot more. May be you got a very good research concept about OS but since you can't implement it and measure the result your new modified algorithm produces, you left presenting a great tech research paper.

No more worries! If you think that your college project is not/less innovate, its mundane, but you feel you are dedicated and have the zeal to learn and code complex things, here is what you need to do.

I have experimented with some OS codes and felt that my work will be a great asset for college students/researchers/hobbyists especially for research projects and I am documenting my work at: http://hobbycoders.com/elearning/course/view.php?id=2

Hobby Coders is non-profit team of coders who work for passion. Please register here to get email notifications when new chapters/topics/course are added.