Add large array to c file without recompiling it - arrays

I use header files to store a dataset in float arrays like so:
//header
float MNIST_test_data_label[1000][10] = {{...},...};
I include these headers in my main.c and can use the arrays, but the IDE (CodeBlocks) keeps making these headers when I build. I searched for hours now, trying to find a solution to make large amounts of data in form of arrays available to my project as "resources" without the compiler constantly rebuilding them.
Does anyone know a solution for this kind of problem?

Add an external declaration to the header. Add the actual definition to a dedicated C file:
// header
extern float MNIST_test_data_label[1000][10];
some C file:
float MNIST_test_data_label[1000][10] = {{...},...};
Note that a file with a single line as above should suffice.

Related

Can't resolve C warning in STM32CubeIDE

Facing a a warning which I am not able to get rid of. I am using stm32 MCU and STM32CubeIDE with a standard C11 compiler. Array gpioOutPins is used by in a function call gpio.c file. This function which contains this function call is called from inOut.c file.
Please note that the inOut.c file is in User Application layer while the gpio.c file is in the Kernel (Core) section of the project tree as can be seen below. I was not able to accommodate the whole project tree in the snapshot.
I don't understand why this warning is generated.
Any help is appreciated. Thank you.
An array is deifned in a header file gpio.h:
static uint16_t gpioOutPins[GPIO_OUT_CH_NR] =
{
DOUT_OD_OUT4_Pin,
DOUT_OD_OUT6_Pin,
DOUT_OD_OUT5_Pin,
DOUT_OD_OUT7_Pin,
DOUT_LED_DISABLE_Pin,
DOUT_BUZZ_Pin,
DOUT_OD_OUT8_Pin,
DOUT_OD_OUT3_Pin,
DOUT_OD_OUT2_Pin,
DOUT_OD_OUT1_Pin,
DOUT_ALARM_Pin,
DOUT_12V_PWR_Pin,
DOUT_12V_PWR_Pin
};
The directory structure looks like this:
The warning generated by the compiler is this:
warning: 'gpioOutPins' defined but not used [-Wunused-variable]
Header (.h) files are not a good place to define global variables, because when they are included in source (.c) files, multiple independent copies of them come to existence. They share the same name, but they are actually different variables. And if they are not static, the linker rejects them because of multiple definitions.
Your inOutTask.c probably includes gpio.h header directly or indirectly, so another copy of gpioOutPins comes into existence, which is distinct from the one used in gpio.c. Because you don't use gpioOutPins in inOutTask.c, you get the warning.
The proper way is to move the definition into gpio.c, remove the static keyword, and add extern uint16_t gpioOutPins[GPIO_OUT_CH_NR]; to gpio.h

Vim autocompletion from library included in another file

I use Fedora 24. I write Ansi C programs in Vim, using it's native autocompletion under ^N. And everything works fine until I define library in another file like this:
fileA.c
#include <allegro5/allegro.h>
#include "fileB.c"
fileB.c
...
If I try to autocomplete in fileA.c it looks up in both file b and library.
But in file a it searches only in fileA.c. How can I look up for library's functions and constants in fileB.c?
When you are in fileA.c, completions are pulled (among other places) from all includes in that file and includes in those includes, recursively.
This happens because the default value of 'complete' contains i.
When you are in fileB.c, completions can't be pulled from any include because you don't seem to have any but they can still be pulled from the current buffer (. in complete), other buffers (w, b, u), and even tags files (t).
That said, explicitly including a resource is necessary in C if you want to use it so I'm not sure it is wise to expect any resource to be available in your code without proper includes.

Remove precompiler directive from .h header file

I'm facing a problem that I don't know how to solve.
Suppose the following typedef struct into a test.h header file:
typedef struct example_struct {
#ifdef CONFIG_A
int A;
#endif;
int B;
} example_struct_t;
I am compiling the code using this header file passing CONFIG_A to the GCC with -D option. This way I am able to include A member to the struct or remove if not needed for a given use case.
Now suppose I generate a shared library (.so) and I would like to distribute it. So, I have the .so library and the headers with precompiler directives. The problem is that I would like not to include the -DCONFIG_A in the program using the library, I mean, I would need to hold the options employed at the library compilation time not only in the source files (.c) but also in the header. That is to say, if a compile the library with -DCONFIG_A option I suppose that program using the library shouldn't include that option in compilation time.
Are the precompiled headers the solution for this problem or is there any other alternative (avoiding include a config.h header in every files defining precompiler directives)?
Thank you so much for the guidance.
You can "generate" the code for the structure definition and ship the generated definition alongside the corresponding library. One idea is to keep your structures in a header with no #includes, in which case you can run the C preprocessor on them to get a file with no #ifdefs (which you can then use to build and ship).
Another way is to do something special in your build system. For example CMake has #cmakedefine which you can use inside a C or C++ source file and then generate code from that.

Adding custom C library in Arduino 1.5.7 IDE

Context:
I would like to add a custom library to a piece of Arduino code in the Arduino 1.5.7 IDE to ensure code is decentralized and readable
Attempted solution:
I make a folder called "mathsfunctions". In it I put two text files, one with a .c and another with a .h name extension.
The .c file is called "mathsfunctions.c" and has the following code in it:
#include "mathsfunctions.h"
int multiply (int a, int b)
{
return a*b;
}
The .h file is called "mathsfunctions.h" and has the following code in it:
int multiply (int, int);
In the main file, I add in the following include preprocessor directive:
#include "mathsfunctions.h"
//The rest of the code
After the above was coded, I imported the library. To do this, I did the following:
Toolbar -> Sketch -> Add Library -> c:.....\mathsfunctions
I can confirm that this is indeed imported because after doing such action, the same mathsfunctions folder appears in the Arduino libraries folder:
C:.....\Arduino\libraries\mathsfunctions
Problem: Upon compiling, the error dialogue box gives the following error:
mathsfunctions.h: No such file or directory
Assistance Required: Any idea on what the problem could be?
You should only have put the header and the source in the same directory as your main file. Also I would suggest putting the implementation in the header since this is the way that people generally include extra functions in C. I am unsure if C supports extra source files but it does support extra headers.

Auto generate header files for a C source file in an IDE

I am trying to use Eclipse and NetBeans for programming in C (not C++). Is there a feature/plugin for them which automatically keeps the source and header files in sync?
As in, when I implement a function in the source file, does it automatically insert the correct lines in the header file?
I did look at solutions like lzz, but they are not what I am looking for.
Eclipse CDT allows you to write a prototype in the header file, and automatically add it to the C file.
Instructions
Add function prototype to .h file void foobar()
Select the function name "foobar" (try double clicking)
In the toolbar click Source -> Implement Method
Wizard it up
Thats probably the best you're gonna get out of the box
Agree with approach proposed by Ryu. In C, I would not automatically create declarations in headers. This should be an explicit action making public some symbol from the C module.
However if declaration/implementation are already setup and you want to modify any of them, I imagine that with Eclipse you may want to use Toggle Function Definition in a possible workflow where you copy in clipboard intermediate toggling results and paste them later over the changed declaration or implementation declaration.
Also use rename refactoring intensively when you change things.

Resources