-1

I have generated a C file which contains some arrays with their size as here:

nor.c (generated file)

const float nor[] = {
-0.0972819, 0.906339, -0.4112, -0.056471, 0.340899, -0.938402, -0.284611, 0.269598, -0.919953, 0.04142, -0.149024, -0.987966, 0.12454, -0.702485, -0.700717, 0.0027959, -0.188166, -0.982133 };

const unsigned int nor_size= 18;

when I called this array from my main file as shown here:

extern float nor[nor_size];

I got some error: cannot use nor_size as a const

how to solve this?

thanks

2 Answers2

1

how to solve this?

The best would be for the generator of the nor.c file to also generate a corresponding nor.h file

#ifndef NOR_INCLUDE_GUARD
#define NOR_INCLUDE_GUARD

extern const float nor[18];
extern const unsigned int nor_size;

#endif

main.c uses that .h file

// Rather than
// extern float nor[nor_size];

// Include
#include "nor.h"

Also recommend the generator of the nor.c fil to append an f suffix to each constant.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
0

In your main.c remove extern float nor[nor_size]; then add

extern const float nor[];
extern const unsigned int nor_size;

and use them as appropriate. Indeed, nor_size might then be loaded (in processor registers at the machine code level) several times.

For more, read this C reference, and later some C standard like n1570 or better.

If you are allowed to compile all your C (both handwritten and generated) code with some recent GCC compiler (or cross-compiler), invoke it at least with -Wall -Wextra -g options.

don't forget to read the documentation of your C compiler!

You also need to understand what is your linker, and read its documentation. Maybe it could be GNU binutils.

You could use some static source code analyzer, like Clang static analyzer, or Frama-C, or Bismon. FYI, Bismon has 24604 lines of generated C code, and the Bigloo compiler has a lot of them too.

for Frama-C or Bismon, contact me by email to basile.starynkevitch@cea.fr and mention the URL of your question

Read also a good C programming book, like Modern C

In 2021, you could be interested by the DECODER project. It is strongly related to your question.

An interesting book describing a software which generates all the half-million lines of code of its C source (available here) is Pitrat's Artificial Beings: the Conscience of a Conscious Machine (ISBN 978-1848211018). You could enjoy reading that book.

See also this answer about generating C code.

You could also be interested by GNU bison. It is a free software parser generator, mostly written in C, and generates C code. My recommendation is to download its source code and take inspiration from it.

By experience, when you are generating C code, it is easier to improve the generator (so that the generated C code compiles cleanly without warnings) that to change the generated C code. But do document well your C code generator. Read about partial evaluation techniques.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547
  • it doesn't work, I think that the compiler doesn't see my generated c file –  Apr 15 '21 at 11:45
  • This means you are using your compiler wrongly. Did you read the documentation of your compiler?. – Basile Starynkevitch Apr 15 '21 at 11:46
  • I use the visual studio compiler (VS2019) –  Apr 15 '21 at 11:49
  • why I have to read its doc, I write the code and then the compiler will do the job –  Apr 15 '21 at 11:53
  • Your question is showing that the compiler is not doing the job you expect. – Basile Starynkevitch Apr 15 '21 at 11:54
  • I have to link the generated c file with my path i think –  Apr 15 '21 at 11:57
  • yes but what do you think about what I said? –  Apr 15 '21 at 12:01
  • I was not able to understand what you have said, and from my perspective, you said nothing and did not write enough. – Basile Starynkevitch Apr 15 '21 at 12:04
  • 1
    There is no need to include a huge appendix with barely on-topic tool recommendations to every post. Newbies will _not_ benefit from static analysers and other more advanced tools, quite the opposite. – Lundin Apr 15 '21 at 13:32
  • I disagree. Static analyzer tools are useful to newbies. Exactly like is useful the `-Wall -Wextra` options to `gcc`; and when coding a C code generator, trying to *avoid* warnings from GCC (or from some static analyzer tool) is a good habit to have. Look BTW into the coding rules related to bootstrapping GCC when contributing to it. – Basile Starynkevitch Apr 15 '21 at 19:20