View previous topic :: View next topic |
Author |
Message |
allenhuffman
Joined: 17 Jun 2019 Posts: 563 Location: Des Moines, Iowa, USA
|
CCS allows duplicate functions of the same name? |
Posted: Thu Dec 19, 2019 9:32 am |
|
|
A coworker told me about something he just ran into with the CCS compiler. I did a quick search, then thought I'd ask here if there is a way to enable a warning about this.
If you have the same function in two files, it does not warn you:
Code: | main.c:
int foo (int x)
{
return x + 1;
}
void main ()
{
....
} |
Code: | foo.c:
int foo (int x)
{
return x * 100;
} |
I just did a quick sanity check in GCC for X86 and saw it gives me the expected "multiple definition of 'foo'" warning. CCS compiler did not, and just calls the first one it finds, it seems.
I expect this is something to do with C++ function overloads, where it would be legal to have:
Code: | int foo (int x)
{
return x + 1;
}
int foo (char y)
{
return y + 1;
}
int foo (float f)
{
return float + 1.2;
} |
Is there a switch somewhere to enable a warning? _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Thu Dec 19, 2019 1:11 pm |
|
|
I don't know if there is a switch for this or not, but this is on purpose. CCS C has adopted a few features from C++. This is one of them. Another is the stream operators "<<" and ">>". I think there were some more, but blanking atm.
EDIT: oh they allow reference parameters for functions, which is a c++ only thing as well.
Last edited by jeremiah on Thu Dec 19, 2019 1:15 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Thu Dec 19, 2019 1:11 pm |
|
|
No.
If you use CCS as it is actually 'designed' to be used (single compilation
unit), it'll merrily warn.
It does not though perform any verification of this sort when 'linking' multiple
compilation units
Use single compilation. It really is how the compiler is designed. I never
know 'why' people insist on trying to use MCU's with CCS. It has big
disadvantages in the checking that can be done, and the optimisation the
compiler can perform.... |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Thu Dec 19, 2019 1:17 pm |
|
|
Oh! I read that wrong. It's a linker issue at that point. See my response in your variables question. It's a similar issue. Since Ttelmah says it normally warns, then it is most likely a bug. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Thu Dec 19, 2019 1:29 pm |
|
|
It won't warn with the example he gives, since he is using a PIC24,
and on a PIC24, and 'int' and a 'char' are not the same size/type of
variable. It'll therefore generate an overloaded function. Two functions
with the same name, and which is called will depend on the type of
the vraiable passed. |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Thu Dec 19, 2019 1:33 pm |
|
|
Ttelmah wrote: | It won't warn with the example he gives, since he is using a PIC24,
and on a PIC24, and 'int' and a 'char' are not the same size/type of
variable. It'll therefore generate an overloaded function. Two functions
with the same name, and which is called will depend on the type of
the vraiable passed. |
In his opening post he puts that he has TWO separate functions defined with the same exact declaration signature:
No char vs int there.
Take a look at the first two code blocks of his post. I read that last block and it threw me off as well (hence my first post). |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
Re: CCS allows duplicate functions of the same name? |
Posted: Thu Dec 19, 2019 1:39 pm |
|
|
allenhuffman wrote: | A coworker told me about something he just ran into with the CCS compiler. I did a quick search, then thought I'd ask here if there is a way to enable a warning about this.
If you have the same function in two files, it does not warn you:
Code: | main.c:
int foo (int x)
{
return x + 1;
}
void main ()
{
....
} |
Code: | foo.c:
int foo (int x)
{
return x * 100;
} |
Is there a switch somewhere to enable a warning? |
I did not find one in the manual. You might have to email CCS and see first if it is intended or not, and, if it is, if they have an undocumented way of doing it. My guess is there is not a way though. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 563 Location: Des Moines, Iowa, USA
|
|
Posted: Thu Dec 19, 2019 2:07 pm |
|
|
A warning for duplicate functions and globals would suffice.
I'll see what they say. It's a "good to know" type thing.
And yes, using
#include "foo.c"
#include "bar.c"
...inside main() should warn just fine, but then any file-scoped "static" variable is now available to every bit of code, which creates more chance of error. I'm a big fan of compartmentalizing code so it doesn't pollute the rest of the program with variables, etc.
When there is just one programmer, it's alot easier to keep variables clear, but if I made a Data Logger and inside I am using S_Index as a static global (using old coding convention from a former job), someone else might be thinking the same thing for their code and we'd end up with a problem that could take awhile to figure out.
"The more you know!" :-) _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
jeremiah
Joined: 20 Jul 2010 Posts: 1355
|
|
Posted: Thu Dec 19, 2019 6:55 pm |
|
|
Generally you would declare the variable in the C files, not the H files. If you need to mod the variable, you can provide getter/setter functions that are declared in the H file but defined in the C file. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19545
|
|
Posted: Fri Dec 20, 2019 1:26 am |
|
|
Dead right. Keep 90% of variables 'local' and there is no problem.
Only things that actually have to be global should be global.
The language does warn if you declare two identical function names
with the same variable type. Where it doesn't, is where these are in
two MCU's. |
|
|
allenhuffman
Joined: 17 Jun 2019 Posts: 563 Location: Des Moines, Iowa, USA
|
|
Posted: Fri Dec 20, 2019 8:23 am |
|
|
Ttelmah wrote: | The language does warn if you declare two identical function names
with the same variable type. Where it doesn't, is where these are in
two MCU's. |
The language does, but not the CCS compiler in the case of using multi-unit compiles, confirmed by support. Sigh. _________________ Allen C. Huffman, Sub-Etha Software (est. 1990) http://www.subethasoftware.com
Embedded C, Arduino, MSP430, ESP8266/32, BASIC Stamp and PIC24 programmer.
http://www.whywouldyouwanttodothat.com ? |
|
|
|