CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

function overload (second opinion needed)
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

function overload (second opinion needed)
PostPosted: Mon Feb 17, 2020 11:02 am     Reply with quote

2/17/2020 EDIT: This may all be due to one test using "#case" and another not, so a type (test and Test) was being resolved to the same thing without #case, and treated as different functions with #case. If CCS confirms anything different, I'll update, else I think I understand the errors now.


This message related to 5.092 (PIC24 for us). Below I have a silly test program I was hoping someone here might be able to run and see if it builds or does not build for you.

Background: This may be related to three different quirks we have found.

The CCS compiler supports C++-style function overloads. Recently, we ran into an issue so I created a simple test program to show the problem and submitted it to CCS. A day or so later, they responded and said it worked for them. I retried the same code and then it worked here.

Today we ran into a similar issue on a different developer's project. It also seemed to be related to a void function like:

Code:
void function(void);


I went to revisit my original sample, and find it's back to not working (code unchanged, has been sitting in version control since written, PC freshly booted this morning and this is the first CCS project I tried to run).

It now looks like it may just be related to another issue where you can have duplicate functions (same name/parameters) in different files, and no warning is given -- moving things around can change which duplicate function is actually called.

This was built for a PIC24 and just has overloaded functions taking different parameters.

Today, I have to comment out "Test();" to get it to build. See comments for the errors it gives.

main.h:
Code:
#include <24FJ256GA106.h>

#device ICSP=1
#use delay(crystal=8MHz,clock=32MHz)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES CKSFSM                   //Clock Switching is enabled, fail Safe clock monitor is enabled

#use rs232(ICD)

#case

// End of main.h


main.c:
Code:
#include <main.h>

/*--------------------------------------------------------------------------*/

// Prototypes
void Test(void);
//void Test(int);

/*--------------------------------------------------------------------------*/
// Two types of structures:
typedef struct
{
   int a,b,c,d,e;
} TestStruct;

typedef struct
{
   int a,b,c,d,e;
} TestStruct2;

/*--------------------------------------------------------------------------*/
// string
void Test(char *s)
{
   printf ("string\r\n");
}

// float
void Test(float f)
{
   printf("float\r\n");
}

// double
void Test(double d)
{
   printf("double\r\n");
}

// structure
void Test(TestStruct test)
{
   printf("structure\r\n");
}

// structure 2
void Test(TestStruct2 test)
{
   printf("structure2\r\n");
}

// void
void Test(void) // edited to fix typo
{
   printf("void\r\n");
}

// int
void Test(int x)
{
   printf ("int\r\n");
}

/*--------------------------------------------------------------------------*/

void main()
{
   printf (__DATE__" "__TIME__"\r\n");

   // These are allowed:
   // structure
   TestStruct temp;
   Test(temp);
   
   // int
   Test(12);
   
   // int
   Test('a');
   
   // structure2
   TestStruct2 temp2;
   Test(temp2);

   // string
   char *string = "Hello world!";
   Test(string);

   // This will not compile. Sometimes.
   // If no prototype, it says "No overload function matches."
   // If prototype defined, it says "function used byt not defined."
   // void
   Test();

   // float
   float f = 3.14;
   Test(f);
   
   // double
   double d = 3.14;
   Test(d);

   while(TRUE) // CCS compiler #define
   {
      //TODO: User Code
   }
}

// End of main.c

_________________
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 ?


Last edited by allenhuffman on Mon Feb 17, 2020 1:01 pm; edited 2 times in total
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 11:41 am     Reply with quote

Here is a smaller version that CCS sent to me -- they said it worked for them, but we've checked two systems here and saw the same issue:

Code:
#include <main.h>

void Test(void);
void Test(int);


void Test(void) // edited to fix typo
{
    printf("void\r\n");
}

void Test(int x)
{
    printf ("int\r\n");
}

void main()
{
    printf (__DATE__" "__TIME__"\r\n");

    Test();
    Test(42);

    while(TRUE);
}

// End of main.c

_________________
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 ?


Last edited by allenhuffman on Mon Feb 17, 2020 12:58 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 17, 2020 11:41 am     Reply with quote

Presumably you're still using Multiple Compilation Units which most of us don't use.
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 11:42 am     Reply with quote

PCM programmer wrote:
Presumably you're still using Multiple Compilation Units which most of us don't use.


No -- only one source file. It was found in an MCU, but I then wrote an example that is just one file. In this case, main.h and main.c is all needed.
_________________
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 ?
ralph79



Joined: 29 Aug 2007
Posts: 87

View user's profile Send private message

PostPosted: Mon Feb 17, 2020 12:20 pm     Reply with quote

allenhuffman,

IMHO, i think it must be the name of functions and/or variables. In the past I had some strange behaviours when the name was very similar. Can you try to change to a complete different name of functions?
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 12:34 pm     Reply with quote

ralph79 wrote:
IMHO, i think it must be the name of functions and/or variables. In the past I had some strange behaviours when the name was very similar. Can you try to change to a complete different name of functions?


Yes, I expect that should work fine. This is a (seeming) bug in their extensions to allow C++ style function overloads in the CCS C compiler.

The puzzler is how it didn't work, then did work, now doesn't work, so I am just collecting data so I have more than just one "hey it doesn't work for me" data point. RIght now I have two different systems that produce the errors.
_________________
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 ?
ralph79



Joined: 29 Aug 2007
Posts: 87

View user's profile Send private message

PostPosted: Mon Feb 17, 2020 12:42 pm     Reply with quote

Quote:
This is a (seeming) bug in their extensions to allow C++ style function overloads in the CCS C compiler.

Quote:
The puzzler is how it didn't work, then did work, now doesn't work,


it could depend on the hex file created and in the the way the micro process that info.. in the last instance the micro could execute both function codes..
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Mon Feb 17, 2020 12:44 pm     Reply with quote

Doing a search and replace of Test to Test1 fixed it for me.

Apparently something about the word Test.

CCS PCWHD Version 5.092
_________________
Google and Forum Search are some of your best tools!!!!
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 12:47 pm     Reply with quote

ralph79 wrote:
it could depend on the hex file created and in the the way the micro process that info.. in the last instance the micro could execute both function codes..


Well, in this case, it won't compile. If there is no prototype, it gives "no overload function matches." Add the prototype, then it changes to "function used by not defined" (even though the function and matching prototype is earlier in the same file. In the pruned-down example (just two functions, int and void) it will give me "a numeric expression must appear here".

Just a compiler bug, I expect. Overloading like this is not part of standard C, from what I am reading, so I'd rather not use it (not portable to other compilers), but it's in existing code being developed where I work so... yeah.
_________________
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 ?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Feb 17, 2020 12:52 pm     Reply with quote

Here you have 'test' and 'Test', lower case and upper case on the first letter.
Is this intentional ?
Quote:
void test(void)
{
printf("void\r\n");
}

void Test(int x)
{
printf ("int\r\n");
}

Also, what dyeatman said.
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 12:55 pm     Reply with quote

PCM programmer wrote:
Here you have 'test' and 'Test', lower case and upper case on the first letter.
Is this intentional ?
Quote:
void test(void)
{
printf("void\r\n");
}

void Test(int x)
{
printf ("int\r\n");
}

Also, what dyeatman said.


No, just a typo. I use #case. That's what caused the "expected an int" error in that shortened example (sent to me from CCS, but with a typo introduced probably by me).
_________________
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 ?
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 12:58 pm     Reply with quote

PCM programmer wrote:
Here you have 'test' and 'Test', lower case and upper case on the first letter.
Is this intentional ?


Ah, their sample did not have #case and wasn't catching that. That explains the difference in what they get and what I get.
_________________
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 ?
Ttelmah



Joined: 11 Mar 2010
Posts: 19515

View user's profile Send private message

PostPosted: Mon Feb 17, 2020 12:59 pm     Reply with quote

As a comment. when you have function prototypes, the variable names
used in the prototype, must exactly match the names used in the actual
definition, or it will produce a new function.
So if you declare with different names, the prototype gets called
not the actual function!...
I managed to mistype a prototype on a set of functions where I had
one supporting 8bit values, one 16bit, one 32bit, one float, and one
a character string. Could not understand why the 16bit one wouldn't
work. However when I hovered over the call in the debugger, lo and
behold it listed 6 functions not 5. Two looking exactly the same, except
for the variable names. It was calling the first one.
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 1:05 pm     Reply with quote

Ttelmah wrote:
As a comment. when you have function prototypes, the variable names
used in the prototype, must exactly match the names used in the actual
definition, or it will produce a new function.
So if you declare with different names, the prototype gets called
not the actual function!...
I managed to mistype a prototype on a set of functions where I had
one supporting 8bit values, one 16bit, one 32bit, one float, and one
a character string. Could not understand why the 16bit one wouldn't
work. However when I hovered over the call in the debugger, lo and
behold it listed 6 functions not 5. Two looking exactly the same, except
for the variable names. It was calling the first one.


That's interesting. I'll look at that. The variable name in a prototype isn't even necessary:

void function (int, int, char *);

...but I like more info, so I tend to do this in personal projects:

void function (int /*count*/, int /*length*/, char * /*message*/);

I edited the top post of this with my discovery of #case which appears to be the root cause of the three different errors I could make this code generate. I sure wish they'd raised their programmers up on case sensitivity ;-)
_________________
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 ?
allenhuffman



Joined: 17 Jun 2019
Posts: 552
Location: Des Moines, Iowa, USA

View user's profile Send private message Visit poster's website

PostPosted: Mon Feb 17, 2020 1:08 pm     Reply with quote

Ttelmah wrote:
As a comment. when you have function prototypes, the variable names
used in the prototype, must exactly match the names used in the actual
definition, or it will produce a new function


This looks like it works now, at least on the pre-release 5.093 I am using.
_________________
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 ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group