How do I compile code to a hex file with SDCC - Page 1
Forum section: electronics / beginners

author: .RC.
author_rank: Frequent Contributor
timestamp: March 11, 2023, 10:50:48 pm
content: I have built a LED DIY clock and want to slightly modify some code on github and upload it to the clock controller chip.The story so far is I have found some replacement firmware on github. I have tried it but I want to modify it to lower the LED brightness. I know what part of the code to change as the writer of the code explained what to change -->>https://github.com/zerog2k/stc_diyclock/issues/63and I can do that.What I do not know what to do is convert that to a hex file so I can upload it. I have installed SDCC. I can barely use the command prompt (windows 10) to get it to do stuff using other tutorials.But there are a heap of files in the github repository and I do not know what files I have to put into a folder for SDCC to turn into a working main.hex fileThere are .c files and .h files and makerfile and .py and so on. The tutorials I find online only seem to use one .c file to make a .hex.https://github.com/zerog2k/stc_diyclock/find/master

author: ataradov
author_rank: Super Contributor
timestamp: March 11, 2023, 11:01:51 pm
content: There is a Makefile, so just run 'make' in that directory and it would build you the hex file.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 01:44:50 am
content: Thanks. I got all the files off github. Modified the file. Saved it. Moved all the files to a folder in the C: directory.I then installed this MinGW package manager that apparently you need to run a makefile.I try to run the file and cmd prompt says thisc:\diyclock>makemkdir -p build/The syntax of the command is incorrect.Makefile:20: recipe for target 'build/adc.rel' failedmake: *** [build/adc.rel] Error 1

author: ataradov
author_rank: Super Contributor
timestamp: March 12, 2023, 02:48:05 am
content: Quote from: .RC. on March 12, 2023, 01:44:50 amWindows does not support "-p" argument. remove it, it should be fine. Or remove the whole call to mkdir and make "build" directory yourself.Since this Makefile was not writnem to be cross-platforms, you may run into a few other issues like this.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 04:12:43 am
content: OK, thanks again. I removed that, but it just threw up another error.It is way beyond my programming skill level which ends at;10 print "This is my basic programming skillz"20 goto 10run

author: ataradov
author_rank: Super Contributor
timestamp: March 12, 2023, 04:14:59 am
content: Well, if you show the error, it may be solved.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 04:47:53 am
content: Well I thought I would start to be annoying as my skills are so limited. I know the make program is working properly, as I was able to compile another file properly as a test.Here is the error messagec:\diyclock>makesdcc --code-size 4089 --xram-size 0 --data-loc 0x30 --disable-warning 126 --disable-warning 59 -Dstc15f204ea -o build/adc.rel -c src/adc.csrc/adc.c:49: error 257: Failed to open output file 'build/adc.asm' (No such file or directory)Makefile:20: recipe for target 'build/adc.rel' failedmake: *** [build/adc.rel] Error 1Running it with a debug, it throws up heaps of data, but the ending bit when it starts to error isMust remake target 'build/adc.rel'.sdcc --code-size 4089 --xram-size 0 --data-loc 0x30 --disable-warning 126 --disable-warning 59 -Dstc15f204ea -o build/adc.rel -c src/adc.cCreateProcess(C:\Program Files\SDCC\bin\sdcc.exe,sdcc --code-size 4089 --xram-size 0 --data-loc 0x30 --disable-warning 126 --disable-warning 59 -Dstc15f204ea -o build/adc.rel -c src/adc.c,...)Putting child 02b4eb30 (build/adc.rel) PID 45416664 on the chain.Live child 02b4eb30 (build/adc.rel) PID 45416664Main thread handle = 0000015csrc/adc.c:49: error 257: Failed to open output file 'build/adc.asm' (No such file or directory)Reaping losing child 02b4eb30 PID 45416664Makefile:20: recipe for target 'build/adc.rel' failedmake: *** [build/adc.rel] Error 1Removing child 02b4eb30 PID 45416664 from chain.

author: ataradov
author_rank: Super Contributor
timestamp: March 12, 2023, 04:57:46 am
content: Did you make "build" directory?You either need to remove "-p" or the whole mkdir line, but in that case you need to manually do what it was supposed to do - create a directory.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 06:14:17 am
content: I do not know how to make a build directoryIf I delete -p. It still throws up errorsc:\diyclock>makesdcc -o build/ src/main.c --code-size 4089 --xram-size 0 --data-loc 0x30 --disable-warning 126 --disable-warning 59 -Dstc15f204ea -DWITH_ALT_LED9 -DWITHOUT_LEDTABLE_RELOC -DSHOW_TEMP_DATE_WEEKDAY build/adc.rel build/ds1302.rel?ASlink-Error-<cannot open> : "build/adc.rel"Makefile:24: recipe for target 'main' failedmake: ***  Error 1

author: ataradov
author_rank: Super Contributor
timestamp: March 12, 2023, 06:22:12 am
content: Just create it in explorer in c:\diyclock.This output looks like you deleted the whole compilation stage and linking fails because nothing was compiled.To simplify this, use original Makefile, but replace "mkdir -p build/" with "mkdir build/". No other changes.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 06:55:55 am
content: OK, I see what you mean. You mean just a folder called build. That is easy enough. right click new folder, rename.The tutorial I watched on youtube put the output files into the folder where all the .c and .h files were.OK, I made the Build folder.I also did some googling and it was mentioned to put a minus sign before the mkdir.I ran it and it threw a heap of files into the build directory. 19 in total. Including a main.ihx file. But no main.hex file.I get an errorMakefile:24: recipe for target 'main' failedmake: ***  Error 255I feel like I am close, but no cigar yet.

author: ataradov
author_rank: Super Contributor
timestamp: March 12, 2023, 07:09:37 am
content: You need to post full output. This final error is useless on its own.Putting '-' in front of the command simply continues the run even if the command has failed, it is not helpful in this case.But looking at the thing, you are likely missing 'tail' command in your system. Remove the lines starting with "@ tail". They just print size information, they are not needed for the build.And it should have produces *.ihx file, which is the file you are looking for just under the different name. 'cp' does the rename, but since 'tail's failed, it sis not run. You can take that file and rename it yourself. Edit: I see you have the ihx file. Use that, this is the file you need.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 07:22:39 am
content: I removed the two @tail commands.The error is stillc:\diyclock>makesdcc -o build/ src/main.c --code-size 4089 --xram-size 0 --data-loc 0x30 --disable-warning 126 --disable-warning 59 -Dstc15f204ea -DWITH_ALT_LED9 -DWITHOUT_LEDTABLE_RELOC -DSHOW_TEMP_DATE_WEEKDAY build/adc.rel build/ds1302.relcp build/main.ihx main.hexprocess_begin: CreateProcess(NULL, cp build/main.ihx main.hex, ...) failed.make (e=2): The system cannot find the file specified.Makefile:24: recipe for target 'main' failedmake: ***  Error 2I see the error seems to be line 24 or includes that line which I marked out belowIt looks like thismain: $(OBJ)$(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(SDCCREV) $(CFLAGS) $^ <--- line 24....@ tail -n 5 build/main.mem | head -n 2@ tail -n 1 build/main.memcp build/$@.ihx $@.hexSo I changed it to thismain: $(OBJ)$(SDCC) -o build/ src/$@.c $(SDCCOPTS) $(SDCCREV) $(CFLAGS) $^cp build/$@.ihx $@.hexAnd got the above error.

author: ataradov
author_rank: Super Contributor
timestamp: March 12, 2023, 07:26:06 am
content: Windows may not have 'cp' either.Just use the *.ihx file. Or rename "cp" to "copy", windows must have that.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 08:11:04 am
content: Thanks so much. I just changed main.ihx to main.hex. Uploaded it to the stc ship and it worked. The clock is now dim and suitable for use in a bedroom at night.

author: .RC.
author_rank: Frequent Contributor
timestamp: March 12, 2023, 09:32:17 am
content: I did some more playing around and googling and discovered a packihx part of SDCCI did this from the cmd prompt with the files in their own folder. I ended up with a main.hex filec:\sdcc test>sdcc -c adc.cc:\sdcc test>sdcc -c ds1302.cds1302.c:123: warning 158: overflow in implicit constant conversionc:\sdcc test>sdcc main.c adc.rel ds1302.relmain.c:448: warning 59: function 'add_BCD' must return valuec:\sdcc test>packihx main.ihx > main.hexpackihx: read 134 lines, wrote 251: OK.

author: HwAoRrDk
author_rank: Super Contributor
timestamp: March 12, 2023, 10:46:20 am
content: Quote from: .RC. on March 12, 2023, 09:32:17 amUsing packihx here is really a redundant operation. The purpose of packihx is primarily to re-order the records in the file into ascending address order. You can see also that it outputs a different, shorter, record length than the input file - that's why the output file has roughly double the number of lines. The only real reason why you might need to use packihx is because whatever programmer you may be using to get the code onto the chip doesn't like non-contiguous addresses or requires a specific record length.For you, if you just want a .hex filename extension, just rename the file! Otherwise, you're just starting with a hex file and ending with a hex file that to most purposes are functionally identical.