Remove precompiler directive from .h header file - c

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.

Related

Including .h and .c files to project on the IAR

I am programming stm8s and sht20 from sensirion company with I2C on the IAR. I'm using sht20 sample code: this link
I edited this sample code to my mcu. Then, for example I included i2c_hal.h to my main.c, but functions not working in my main.c file and IAR error is
ERROR LI005 no defition for I2c_Init()
Linking error
For example:
main.c
#include "stm8s.h"
#include "i2c_hal.h"
I2c_Init();
i2c_hal.h
#ifndef I2C_HAL_H
#define I2C_HAL_H
void I2c_Init ();
#endif
i2c_hal.c
#include "I2C_HAL.h"
void I2c_Init ()
{
SDA=LOW;
SCL=LOW;
SDA_CONF=LOW;
SCL_CONF=LOW;
SDA=HIGH;
SCL=HIGH;
}
I copied sht20 files to my project directory. What should I do for this error?
The header file is read by the preprocessor not the linker; if you get as far as linking, it is not a header file issue. The three basic build steps for C code are:
preprocess
compile
link
Your build is failing at the link state. The linker requires all compiled object files and any necessary libraries that constitute your application as input. In your case the most likely issue is that you have not compiled and linked i2c_hal.c (or strictly compiled i2c_hal.c and linked i2c_hal.obj). In the IAR IDE you simply explicitly add i2c_hal.c to your project along with main.c, and all should be good (all other dependencies being satisfied).
I suspect that i2c_hal.c will infact fail compilation since it is missing any declaration of SDA, SCL etc. - you probably need to include stm8s.h there also.
In general the process looks like this (this diagram actually omits pre-processing - i.e. expansion of headers, macros and conditional compilation etc. - but it was the otherwise clearest example I found; the original page does however mention the pre-processor stage, and the preprocessor is normally run automatically when you invoke the compiler in any case):
I have also the same issue with the spi. I got hal_spi_init() linking problem. To resolve the issue you need to enable the I2C in your stm32 hal drivers. In stm32xx_hal_conf.h file we have different #define modules. There you can enable the I2C module or just include the defined symbol in your IAR tool. Then Issue resolved
You need to add the C source files to the project. Header files shall not have any code or data, only the declarations of types , extern variables, macros, static inline functions and function prototypes.

Linux: Including header files through make instead of #include in c

I have a macro which is used in afile.c and bfile.c (both in module A)
This macro is in aheader.h
Both of these are in different modules/directories and aheader.h module is complied before module A is complied.
Now one way is to do #include "aheader.h" in each of the .c files.
But instead of doing this, is there a way to make some addition in the Makefile (like adding it to the list of headers) for module A,
so that aheader.h is picked for everywhere the macro is used?
#include "aheader.h" is the simple and correct thing to do. C has no feature to auto-include headers when a macro is used.
If you insist on doing it in the makefile, you can add -include aheader.h as a compilation flag. It will include it in all files.
It's possible to use the makefile to add this flag only when the macro is found in the C file, by using grep. But it's complicated makefile work, and I think you're better off without it.

Setting up a C project with multiple source files in MPLAB

I'm using the MPLAB IDE and the XC8 compiler for a C project for PIC18 devices. I'm building a project with multiple source files and don't know how to make the structure.
In the project, I have the following things:
A file main.c where the main code is located. From here, several files are included:
xc.h: to define chip-specific variables and so
stdlib.h, stdio.h, plib.h, delays.h: for the compiler's functions
enc28j60.h: a homebrew file with definitions and prototypes
A file enc28j60.c, where the functions of the prototypes in enc28j60.h go
I cannot compile enc28j60.c as standalone file because it depends on definitions in main.c.
I have a few questions on how to set this project up:
Should I add enc28j60.c to the source files of my MPLAB project? If I do this, MPLAB tries to compile the file, which fails. If I don't do this, the linker cannot find the symbols that are defined in enc28j60.c and prototyped in enc28j60.h.
Should I #include enc28j60.c from somewhere? If not, how does MPLAB know where to get the file?
Should I add enc28j60.h to the header files of my MPLAB project?
Should I #include enc28j60.h from somewhere? Right now, I do this in main.c, after the definitions enc28j60.h needs in order to run (and not throw #errors).
I managed to get this working by modifying my library and header files a bit.
At first, I added a file main.h where all the prototypes, #defines and #includes would go. Then, in every .h, file, I added this on top:
#ifndef SOME_LIB_IDENTIFIER // makes sure the lib only gets included once,
#define SOME_LIB_IDENTIFIER // has to be specific for every lib
#include "main.h" // to make sure everything's initialized (this line of course not in main.h)
And the last line of every .h file would be:
#endif
I added #include "enc28j60.h" to the top of the enc28j60.c file. This file now can be compiled.
In main.h, I added includes for xc.h, plib.h, stdlib.h, stdio.h and enc28j60.h. I nowhere included .c files.
I added both the main and enc28j60 header and source files to my MPLAB project. The source files get compiled well, both. The result is linked together.
In short
Add a main.h where all prototypes, #defines and #includes go
Add a header and footer to all your header file to make sure they're only included once. Also include main.h from these headers, to make sure every file uses the same definitions
Include a source file's corresponding .h file at the very first line of your source file. Do not include .c files
Add all header and source files (that are not built into the compiler) to your MPLAB project
Build (F10) should compile all files and link them together correctly
Don't include .c files. Include headers only. If you have declarations to be shared between files, put them in a separate header, and include that header whenever you need it.
After compiling each individual source file, link the resulting object files together. This involves the invocation of the compiler on all of the source files, then a one-time invocation of the linker on the object files (with supplemental libraries, etc.).

C Header Files with the same declarations but different implementations

I have two sets of header files and .c files in my project i will only ever be including one of these headers but i want the option to quickly swap the header im including. Both header files have some declarations that are exactly the same but the implementations in the .c files are different. Basically what I need is way to tell the compiler to only compile the .c file that is associated with the header im including elsewhere in my program.
You could always specify the .c or .o file that you're going to link against at compile/link time for instance
gcc -o myexe file1.c/file1.o
or
gcc -o myexe file2.c/file2.o
you could even make this a different make directive if you have a makefile if you have the same header file but 2 different implementations. I would recommend just using 1 header file and changing the underlying implementation, no point in having 2 headers with similar declarations.
If both header files are exactly the same then you don't need to maintain two header files. You can keep only one copy. Whichever code includes the header file can include this single header file only.
You can always specify which .c file you want to compile while compiling. In gcc, you can mention the C file to be compiled in the command line. In Visual Studio, you can include the correct C file.
I guess you should maintain only one header file and include that in your code. Introduce a flag to the makefile to link which implementation to be linked. You have not mentioned what are you using to build.

Linking to MATLAB generated code

I'm using embedded MATLAB to generate C code from an .m script. However, after the generation I'm left with about 15 files (most of them are source). If I want to write a standalone main in C how do I link to the MATLAB generated code? Is there a way to do this without writing my own Makefile?
So I figured out how to generate static libraries (.a) with the matlab generated code... can I build dynamically loaded libraries (.so)?
I don't know if this is particular to my project, but I find that I can include Matlab generated files the same way as you would normally include anything else.
I copy my generated matlab source files (.c and .h) into my project directly, and then I have to specifically add them to my project. ( I wonder if that has more to do with the IDE I use though).
Then, in main.c that you create, just add the line #include "yourGeneratedFile.h". Within main.c now, you should be able to use whatever functions were created by Matlab.
For instance, in an example:
within main.c, include the header file and use the function you need
#include "SPIUARTDemo30f.h"
//further down in the file
SPIUARTDemo30f_step(); //using the function I asked Matlab to generate
SPIUARTDemo30f.h and SPIUARTDemo30f.c are the generated files from Matlab:
within the .h you'll see
extern void SPIUARTDemo30f_step(void);
and, if you look at the .c you'll find:
void SPIUARTDemo30f_step(void)
{
/* lots of code */
}

Resources