In one 'c' file I define the following function:
void fnTemp(int jj, int kk);
void fnTemp(int jj, int kk)
{
...do some stuff...
}
In a different 'c' file I have the following:
extern void fnTemp(int jj);
void fnTryIt(void)
fnTemp(3);
This will link without any errors. How do I get the linker to tell me that I have two definitions for fnTemp()?
I know that I could put the declaration for fnTemp() in a .h file and include the .h file in both .c files which will then trigger a compiler error. But I have an instance where that can cause other issues.
Thank you for any help.
The C compiler and the linker cannot know that you call the function with wrong parameters. That's the nature of C.
You might switch to C++ (even if you do not use the C++ features). C++ "decorates" the functions, so it will have a function __vfnTempII in one file and you call __vfnTempI in the other. And the linker will complain.
I was hoping maybe there was some type of linking option to do this. I tried switching to C++ but due to external libraries and includes this is not possible. The build fails in ways that I can't correct.
Thank you for taking the time to answer.
The linker is most often the same, no matter if C or C++. It is the compiler which makes the difference. Best protectection is to use only header files and switch on all warnings.
Kevin Franzen said: But I have an instance where that can cause other issues.
I would solve this because it is likely an issue you created somehow.
We took the time to move the external declarations to inside include files. The good news is we didn't find any other issues.
Again, thank you for your help.
I am surprised the linker does not throw an error - I see you are using IAR tools, but armlink from the Arm Compiler for Embedded toolchain emits:
Error: L6200E: Symbol fnTemp multiply defined (by foo2.o and foo1.o).
Actually no. I tried this:
extern void helloExit(int a); /* atExit function */ void displayExit(void) { helloExit(1); kprintf(1, " ended\n"); }
and in the other file:
void helloExit(void) { kprintf(1, " ended\n"); }
I checked with armcc 5.06 and get no linker error. Tried Clang 6.6.3, no error/no warning.