Lib with conditional compiling...

ulmus71 wrote on Wednesday, February 22, 2012:

I have some library i want conditionaly compile: with or without freertos.
What is the best defined const  in FreeRTOS to use with ‘#ifdef xxxxx’ statement?

#ifdef INC_FREERTOS_H
or something else?

richard_damon wrote on Wednesday, February 22, 2012:

You are going to have to add some define to tell you source code if you want to use FreeRTOS or not. The issues is that you will want to conditionally include the needed FreeRTOS headers based on this. This of course means you will need to build two versions of your library (one with and one without this define) and link to the appropriate version.

Otherwise you are going to run into a chicken and egg issue, to try and test a FreeRTOS define, you will have to know if you need to include the FreeRTOS headers before you have included the headers that would define the symbol. The only way to do that would be to use some compiler option to force the inclusion of the base FreeRTOS header at the start of your program. It is probably better to add the define in your compiler config (something like -DFREERTOS) than to force the header inclusion.

It is generally impossible to have the library check at link time to see if the rest of the program is using a software package and somehow “automagically” change the code of the library based on this, you need to pre-generate the different versions of the library, then there MAY be a way to select which library to use, or you need to change which library is specifed in the link options.

ulmus71 wrote on Wednesday, February 22, 2012:

I was not so clear i’d wanted to. When i was saying ‘library’ i was thinking about source library (*.c, *.h files), not precompiled library.

richard_damon wrote on Wednesday, February 22, 2012:

If you have .c files, it does’t really make a difference. Each .c file is generally compiled independently, so it can not tell if the FreeRTOS files are being used by other .c files. You are still going to need to add some define in the compliers configuration to tell it that you are using FreeRTOS or not. If your library is a “pure header” library (which is unusual in C, it means ALL operations need to be with inline functions, and any globals needed will need to be setup by the user) then you could require it to be include after the include of FreeRTOS.h and detect the INC_FREERTOS_H define, but that could lead to interesting problems if the user make a mistake and puts it before the include.

An older style of using FreeRTOS was to define a variable in the compiler (with a forced define) to indicate which port to use, and you could use that to select. The more recent standard is to just put the appropriate port directory on the include path, which won’t help you. That is why I suggest you select for yourself a configuration define to allow you to detect if you are going to use FreeRTOS or not.

The other option is to use a “porting” layer.  If you can define your library based on some generic interface, and then have a porting .h/.c file(s) that define that interface to the environment you are targeting to. You then do like FreeRTOS and add the appropriate sub directory to the include path, and maybe need to add the appropriate myport.c to your project. (this assumes that your design doesn’t radically differ based on targeted environment, although since that myport.h would include FreeRTOS.h if you were using FreeRTOS, you could then check for INC_FREERTOS_H being defined. 

ulmus71 wrote on Wednesday, February 22, 2012:

Many thanks!
I decided to use my own '#define ’ in my lib header file, it would be easier and cleaner to use.
The most diferences in compiling library using frrertos or not, is about using mutexes and semaphores instead of using bit flags and loops.