Microchip XC8 compiler problems - Page 1
Forum section: electronics / general

author: jwrodgers
author_rank: Unknown
timestamp: August 03, 2017, 10:05:37 pm
content: Hi, just installed XC8, and I'm having problems when I try to include <xc.h>:My main file:Code:/** File: newmain.c* Author:** Created on August 3, 2017, 10:26 PM*/#include <stdio.h>#include <stdlib.h>#include <xc.h>/***/void main( void ) {return ( EXIT_SUCCESS );}And when I try and build it, I get the following error:Code:make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-confmake: Entering directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/anotherCDelMe.X'make -f nbproject/Makefile-default.mk dist/default/production/anotherCDelMe.X.production.hexmake: Entering directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/anotherCDelMe.X'"/Applications/microchip/xc8/v1.42/bin/xc8" --pass1 --chip=16LF18346 -Q -G --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/newmain.p1 newmain.cmake: *** [build/default/production/newmain.p1] Error 1/Applications/microchip/xc8/v1.42/include/xc.h:8: error: (102) #endif must be in an #ifmake: *** [.build-conf] Error 2make: *** [.build-impl] Error 2(908) exit status = 1nbproject/Makefile-default.mk:106: recipe for target 'build/default/production/newmain.p1' failedmake: Leaving directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/anotherCDelMe.X'nbproject/Makefile-default.mk:90: recipe for target '.build-conf' failedmake: Leaving directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/anotherCDelMe.X'nbproject/Makefile-impl.mk:39: recipe for target '.build-impl' failedBUILD FAILED (exit value 2, total time: 324ms)And my xc.h file:Code:#ifndef _XC_H_#define _XC_H_#ifdef __XC8#include <htc.h>#endif#endif//_XC_HI am sure this is likely something very simple, but I've been trying for hours now and I'm not making any progress. Thanks for any help or pointers you can give me.?

author: Ian.M
author_rank: Super Contributor
timestamp: August 03, 2017, 10:46:48 pm
content: C preprocessor errors tend to propagate for many lines and finally break the surface nowhere near their original cause. Its probably not seeing the first #ifndef _XC_H_ line, due to a damaged stdio.h or stdlib.h file. Comment out all the #includes except #include <xc.h> and see what happens. You don't need them except for library functions that explicitly mention them on their page in the manual and #include <xc.h> should be first anyway.If you've been careless viewing files in an editor you may have accidentally changed one - look at the modified dates of all the files in and below the XC8 include folder and see if any stand out. If you cant locate the offending file and copy it from another machine with the same XC8 version, you may have to do a reinstall.There may also be undesirable interactions between the Dropbox app and the XC8 compiler. Try creating a new project in a folder that isn't synced by Dropbox.

author: Andy Watson
author_rank: Super Contributor
timestamp: August 03, 2017, 11:16:32 pm
content: ? !Where did you get the make file from ?Never mind. Go back to the start - keep it as simple as possible so that you can see where the errors are coming from. You should be able to compile "newmain.c" from the command line. E.g. :xc8 --chip=16F1459 newmain.cSubstitute whatever processor you are using in place of the 16F1459.Bash downCorrect all the errors that occur whilst learning how the compiler behaves. Now put what you have learnt into the make file - and onward...I am not sure if this is an error, however, one problem is that your main program is declared as "void", but you try to return a value. The next problem, hypothetically assuming you get this code through the compiler, is where do you expect this program to "return" to ?I have just tried to compile this:Code:void main(void){char x;while(1) {x++;}return(0);}and the compiler throws an error at the return statement.

author: jwrodgers
author_rank: Unknown
timestamp: August 03, 2017, 11:46:07 pm
content: Thanks for the ideas, I've tried commenting out the other includes, and reinstalled the compiler xc8, and creating a new project outside of the dropbox.After this it still did not work.I then tried the minimum code, and it worked. Created a new project, and added a new c main file (MPLAB IDE options) and got this file:Code:#include <xc.h>#include <stdio.h>#include <stdlib.h>int main(int argc, char** argv) {return (EXIT_SUCCESS);}(This is from MPLAB, not sure where it would return that badboy to!) Anyway, it is compiling now ok:Code:CLEAN SUCCESSFUL (total time: 56ms)make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-confmake: Entering directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/FRDinC.X'make -f nbproject/Makefile-default.mk dist/default/production/FRDinC.X.production.hexmake: Entering directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/FRDinC.X'"/Applications/microchip/xc8/v1.42/bin/xc8" --pass1 --chip=16LF18346 -Q -G --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" -obuild/default/production/frdmain.p1 frdmain.c"/Applications/microchip/xc8/v1.42/bin/xc8" --chip=16LF18346 -G -mdist/default/production/FRDinC.X.production.map --double=24 --float=24 --opt=+asm,+asmfile,-speed,+space,-debug,-local --addrqual=ignore --mode=free -P -N255 --warn=-3 --asmlist -DXPRJ_default=default --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-osccal,-resetbits,-download,-stackcall,+clib --output=-mcof,+elf:multilocs --stack=compiled:auto:auto "--errformat=%f:%l: error: (%n) %s" "--warnformat=%f:%l: warning: (%n) %s" "--msgformat=%f:%l: advisory: (%n) %s" --memorysummary dist/default/production/memoryfile.xml -odist/default/production/FRDinC.X.production.elf build/default/production/frdmain.p1Microchip MPLAB XC8 C Compiler (Free Mode) V1.42Build date: Apr 12 2017Part Support Version: 1.42Copyright (C) 2017 Microchip Technology Inc.License type: Node Configuration:: warning: (1273) Omniscient Code Generation not available in Free modeMemory Summary:Program space used 7h ( 7) of 4000h words ( 0.0%)Data space used 6h ( 6) of 800h bytes ( 0.3%)EEPROM space used 0h ( 0) of 100h bytes ( 0.0%)Data stack space used 0h ( 0) of 7F0h bytes ( 0.0%)Configuration bits used 0h ( 0) of 4h words ( 0.0%)ID Location space used 0h ( 0) of 4h bytes ( 0.0%)You have compiled in FREE mode.Using Omniscient Code Generation that is available in PRO mode,you could have produced up to 60% smaller and 400% faster code.See http://www.microchip.com/MPLABXCcompilers for more information.make: Leaving directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/FRDinC.X'make: Leaving directory '/Users/mollyandchloe/Dropbox/MPLABXProjects/FRDinC.X'BUILD SUCCESSFUL (total time: 1s)Loading code from /Users/mollyandchloe/Dropbox/MPLABXProjects/FRDinC.X/dist/default/production/FRDinC.X.production.hex...Loading completedNot sure what happened! Thanks for the help.

author: Ian.M
author_rank: Super Contributor
timestamp: August 04, 2017, 12:03:50 am
content: @Andy,Looks like the O.P. is using the MPLAB X autogenerated default makefile: {ProjectFolder}/nbproject/Makefile-default.mkIts unlikely to be a makefile error :- /Applications/microchip/xc8/v1.42/bin/xc8.exe is barfing on the first pass of a release build near the end of /Applications/microchip/xc8/v1.42/include/xc.h, and the command line passed to it looks sane enough.@J.W.R.I'd put it down to MPLAB X weirdness - it gets a mite confused at times.It can also interact very badly with virus scanners - its worth disabling realtime scanning on reads or execution for anything in or under /Applications/microchip by excluding the folder. If possible leave scan on write enabled, and if not do a manual scan after any Micochip software installs or updates.

author: MarkF
author_rank: Super Contributor
timestamp: August 04, 2017, 01:23:47 am
content: I think the key here is that the#include <xc.h>must be declared first before other system includes.

author: JPortici
author_rank: Super Contributor
timestamp: August 04, 2017, 06:11:59 am
content: Quote from: Ian.M on August 03, 2017, 10:46:48 pmthis is most likely to be your problem. i used to have the same problems (and more) with google drive, from time to time a new build would fail because it was still busy uploading the old files

author: cv007
author_rank: Frequent Contributor
timestamp: August 04, 2017, 03:48:03 pm
content: QuoteThat seems odd looking to me- this is what a newmain.c looks like for me-#include <xc.h>void main(void){return;}(compiles without error)Maybe in the new project 'wizard' you are going off the beaten path.

author: rstofer
author_rank: Super Contributor
timestamp: August 05, 2017, 12:49:16 am
content: A void function (as shown in the earlier code) can not return a value.ETA: Embedded code never returns. There is always a superloopwhile(1) {... your code goes here}

author: cv007
author_rank: Frequent Contributor
timestamp: August 05, 2017, 02:43:16 pm
content: Quote...but it certainly can return. To where is up to the C runtime. In the case of XC8, you will end up with a jump back to 'start' (also generated by the C runtime). Why they don't put a while(1) in the generated code, I don't know. (I didn't make very clear- the newmain.c code shown was generated by MPLABX)When I see generated code for a micro that has 'int main(int argc, char** argv)', I would suspect something is not right. The 'generator' probably is thinking in 'pc' mode, and not microcontroller mode for some reason.

author: rstofer
author_rank: Super Contributor
timestamp: August 05, 2017, 10:50:36 pm
content: Quote from: cv007 on August 05, 2017, 02:43:16 pmIn the case of some early NXP ARMs (LPC2106, LPC2148), there is a one instruction spin loop immediately following the branch and link to main(). This is in the file crt.S, the startup file. In this case, it is not provided by any part of the GCC code, it is a separate user generated assembly language file that deals with vectors, startup, and, ultimately, the branch and link to main().If main happens to return, it will get stuck in the spin loop until the chip is reset.I haven't looked at other processors because I make sure my code won't return.

author: cv007
author_rank: Frequent Contributor
timestamp: August 06, 2017, 02:58:52 am
content: I would suspect most C runtimes for micros simply spin in a loop if returned from main.Of course, it would be odd to intentionally return from main in a microcontroller. I guess if only using interrupt code one could return from main if you knew that the loop was there (but why). In the case of XC8, I would suspect their reasoning for jumping back to 'start' is to keep the micro running (a software reset would probably be preferred if the reset instruction was available). So, it looks like XC8 considers a return from main as being a mistake, and yet MPLABX generated main code has a return in main. They seem a little conflicted.