This is most likely because of function overloading. When compiling with C, the call to fun(double)
is translated into a call to the assembly function _fun
, which will be linked in at a later stage. The actual definition also has the assembly name _fun
, even though it takes an int instead of a double, and the linker will merrily use this when fun(double)
is called.
C++ on the other hand mangles the assembly names, so you'll get something like _fun@int
for fun(int)
and _fun@double
for fun(double)
, in order for overloading to work. The linker will see these have different names and spurt out an error that it can't find the definition for fun(double)
.
For your second question it is always a good idea to declare function prototypes, generally done in a header, especially if the function is used in multiple files. There should be a warning option for missing prototypes in your compiler, gcc uses -Wmissing-prototypes
. Your code would be better if set up like
// file1.hpp
#ifndef FILE1_HPP
#define FILE1_HPP
void fun(int)
#endif
// file1.c
#include "file1.hpp"
...
// file2.c
#include "file1.hpp"
int main()
{
fun(5);
}
You'd then not have multiple conflicting prototypes in your program.