Cyberithub

Solved "label can only be part of a statement and a declaration is not a statement"

Advertisements

In this article, we will see how to solve "label can only be part of a statement and a declaration is not a statement" error. Sometimes you might observe that when you try to compile a set of C source code files while trying to install a package through source code then you always end up having "label can only be part of a statement and a declaration is not a statement" error on the output due to certain error in the code.

This can really become frustrating when there are hundreds and hundreds lines of code which you are not much aware of but still need to look into the error to proceed with the installation. This is one such kind of error which we are going to look into below section with the help of an example.

 

Solved "label can only be part of a statement and a declaration is not a statement"

Solved "label can only be part of a statement and a declaration is not a statement"

Also Read: What is Merge Sort Algorithm [Explained with examples]

In my case, I am trying to download and install BBC Micro emulator from GitHub using source code but when I tried to configure and compile the BBC Micro emulator code then I observed below "label can only be part of a statement and a declaration is not a statement" error on the output as you can see below.

ARMulator/armemu.c: In function ‘ARMul_Emulate32’:
ARMulator/armemu.c:819:21: error: a label can only be part of a statement and a declaration is not a statement
819 | int do_int = 0;
| ^~~
make[1]: *** [Makefile:2250: ARMulator/b_em-armemu.o] Error 1
make[1]: Leaving directory '/home/cyberithub/b-em/src'
make: *** [Makefile:426: all-recursive] Error 1

From the above output, it shows that Line 819 has some problematic code which looks like below.

check_PMUintr:
                  int do_int = 0;
                  cp14r0 |= ARMul_CP14_R0_FLAG2;
                  (void) state->CPWrite[14] (state, 0, cp14r0);

                  ok = state->CPRead[14] (state, 1, & cp14r1);

Above error is simply because prior to C99, all declarations had to precede all statements within a block, so it wouldn't have made sense to have a label on a declaration. C99 relaxed that restriction, permitting declarations and statement to be mixed within a block, but the syntax of a labeled-statement was not changed. So to fix the error, I just added a semicolon(;) after colon(:) as you can see below.

check_PMUintr:;
                  int do_int = 0;
                  cp14r0 |= ARMul_CP14_R0_FLAG2;
                  (void) state->CPWrite[14] (state, 0, cp14r0);

                   ok = state->CPRead[14] (state, 1, & cp14r1);

After that, I again tried to compile my code and this time it compiled successfully as you can see below.

Making all in src
make[1]: Entering directory '/home/cyberithub/b-em/src'
CC ARMulator/b_em-armemu.o
CC ARMulator/b_em-arminit.o
CC ARMulator/b_em-armmem.o
CC ARMulator/b_em-armsupp.o
CC ARMulator/b_em-bag.o
CC ARMulator/b_em-thumbemu.o
CXX ARMulator/armdis.o
CC b_em-map.o
CC b_em-sprow.o
CXXLD b-em
CC m7makechars.o
CCLD m7makechars
CC hdfmt.o
CCLD hdfmt
CC jstest.o
CCLD jstest
CC sdf-gtest.o
CC sdf-geo.o
CCLD gtest
CC sdf2imd.o
CCLD sdf2imd
CC bsnapdump.o
bsnapdump.c: In function ‘dump_model’:
bsnapdump.c:215:5: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result [-Wunused-result]
215 | fread(bytes, sizeof(bytes), 1, fp);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CCLD bsnapdump
cp b-em ..
make[1]: Leaving directory '/home/cyberithub/b-em/src'
make[1]: Entering directory '/home/cyberithub/b-em'
make[1]: Nothing to be done for 'all-am'.
make[1]: Leaving directory '/home/cyberithub/b-em'

Hope above solution works for you as well. Please let me know your feedback in the comment box !!

Leave a Comment