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

CCS and MPLAB X code highlighting with conditional compiling

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

CCS and MPLAB X code highlighting with conditional compiling
PostPosted: Mon Apr 13, 2020 11:48 am     Reply with quote

MY FAULT, KEEP THIS AS A GUIDE TO WHAT YOU CAN DO, AND WHAT YOU CANT. THERE IS NO SUCH PROBLEM! READ IF YOU WANT, THE SOLUTION IS AT THE END.

Hi!
Ive been using CCS and MPLAB X together for a long time.
I have a problem that makes my project to show errors all along the code. Just show errors on IDE, but are not errors while compiling. So its "aesthetic error" Smile

I usually make projects for several PICs at the same time. The same code works for all of them, but i use conditional compiling when some feature is only available on some of them.

For example: lets say a relay board. The same code can control 4 relays and 8 relays. Two different PICs are used. A 8 pin PIC and a 14 pin PIC (no need to discus if this is the best approach, its just an example).

Inside MPLAB X i create 2 different configurations for the same project. One with the small PIC, and other with the bigger one. And inside "CCS compiler options" i add a preprocessor macro. "4_RELAYS" for the small PIC, and "8_RELAYS" for the bigger one.

Once you do this, the dropdown menu next to the compile button will have 2 options. The ones we just created. Each option will choose one or the other configuration for the PIC. And also will create a new #define. The one added to the preprocessor macros.

I know it may sound a bit complicated if you never did this, but i promise im about to finish the example Embarassed

Now on the code i can create code that only applies to one or the other PIC. Using for example:
Code:
#if defined(4_RELAYS)
#include <12F1840.h>
#elif defined(8_RELAYS)
#include <16F1825.h>
#endif


Code:
#if defined(4_RELAYS)
... some code only for the 4 relay board
#elif defined(8_RELAYS)
... some other code only for the 8 relay board
#endif


As you can see, this is quite useful. I helps me keep one main code for different PICs, and choosing one or the other option in the dropdown, my project will compile accordingly. NICE!

NOW the problem...
When doing this, the preprocessor macros are not present in the code, but generated by the compiler. So MPLAB dont know this exist. Which makes the conditional compilation code to be not parsed. All markdown and highlighting is compromised. Special functions included in the device.h stops being parsed, and fills all the code with errors because of functions not declared.

So, does anyone know a better way to do this? Or maybe is there a way to let MPLAB create the macro instead of the compiler? This way MPLAB will be aware of it, and parse the code correctly...

I know not everyone will be aware of this feature, and very few will use it. So i dont know if there is someone out there that can help Sad
But i think its very helpful! So maybe even if there is no good solution for this, at least some of you would have learned some interesting trick.

Thanks!

SOLUTION:
The mentioned above does work OK with a minor change. You can not use a NUMBER as the first character of a #define. Like "4_RELAYS" or "8_RELAYS".
If you do, then MPLAB will not take it into account. Although when compiling, CCS will dont throw any error and compile OK. But still, as MPLAB dont consider this define, then it will not parse correctly the code that needs it.
Just remember kids, #define can not start with a number... the same as variables... Embarassed


Last edited by Marttyn on Mon Apr 13, 2020 12:21 pm; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Apr 13, 2020 12:14 pm     Reply with quote

I don't use MPLAB X but I know that CCS uses if defines all the time in their examples and drivers and it works fine under MPLAB V8.92.
I've not had a problem code cutting for different PICs with different LCD modules past 2 decades....so I suspect it might be an MPLAB X probelm ??
Others who use 'X' will reply soon......
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

PostPosted: Mon Apr 13, 2020 12:22 pm     Reply with quote

Solved Embarassed
Glad to find the solution, but ashamed for such a lame mistake...
Ive been carring with this problem for years
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Apr 13, 2020 2:39 pm     Reply with quote

re: Just remember kids, #define can not start with a number... the same as variables...

hmm, anyone know where this is listed ? I can't easily find that or say a list of 'reserved' words (words or text) that are not allowed or used internally.

For instance, most versions of BASIC, have a chart/list of 'reserved words'

Just curious, as I'm not a schooled C programmer.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 13, 2020 3:00 pm     Reply with quote

Do a web search for this. Put it on your desktop. Look up any C item.
Quote:
K&R C language pdf

In this case, go to the page on "Variable Names".
temtronic



Joined: 01 Jul 2010
Posts: 9244
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Mon Apr 13, 2020 3:33 pm     Reply with quote

Thanks..
The ONLY C I know is CCS C. But as CCS C isn't 100% K&R C, I'd assume that some keywords or syntax is unique to CCS.
I'm just surprised that CCS doesn't list 'reserved words' or 'legal syntax' in the manual. Some things, like how long can a variable name be ?
When I started cutting code variables could be 1 or 2 characters long.

I assume most programmers here were taught C in school ?
Marttyn



Joined: 06 Mar 2015
Posts: 28
Location: Spain

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

PostPosted: Mon Apr 13, 2020 4:12 pm     Reply with quote

Don't know where I've learned (and later forgot) that variables and constants can not start with a number... Maybe in VB?
Anyway, CCS still does not accept it. You can try it and you will get an error: "Expecting an identifier".
But if you use it as explained, in the "preprocessor macro", then this is not checked... Maybe a bug, or maybe on purpose. But MPLAB don't like it.
jeremiah



Joined: 20 Jul 2010
Posts: 1355

View user's profile Send private message

PostPosted: Mon Apr 13, 2020 5:01 pm     Reply with quote

temtronic wrote:
re: Just remember kids, #define can not start with a number... the same as variables...

hmm, anyone know where this is listed ? I can't easily find that or say a list of 'reserved' words (words or text) that are not allowed or used internally.

For instance, most versions of BASIC, have a chart/list of 'reserved words'

Just curious, as I'm not a schooled C programmer.


So I looked at the C standard and section 6.10 says that #define is followed by identifier. Then identifier is recursively defined in 6.4.2 to start with a "identifier-nondigit". They define that not to include 0-9.

One thing that most people don't know is that any identifier, whether define, variable, or function that starts with one or two underscores is technically reserved by the C standard (so that compiler vendors and suppliers of the standard library have those for their implementations). EDIT: Yep, section 7.1.3 of the standard calls this out so it is available for the standard library.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
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