Quality RTOS & Embedded Software

 Real time embedded FreeRTOS RSS feed 
Quick Start Supported MCUs PDF Books Trace Tools Ecosystem


Lib with conditional compiling...

Posted by ulmus on 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?

or something else?

RE: Lib with conditional compiling...

Posted by Richard Damon on 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.

RE: Lib with conditional compiling...

Posted by ulmus on 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.

RE: Lib with conditional compiling...

Posted by Richard Damon on 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.

RE: Lib with conditional compiling...

Posted by ulmus on 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.

[ Back to the top ]    [ About FreeRTOS ]    [ Privacy ]    [ Sitemap ]    [ ]

Copyright (C) Amazon Web Services, Inc. or its affiliates. All rights reserved.

Latest News

NXP tweet showing LPC5500 (ARMv8-M Cortex-M33) running FreeRTOS.

Meet Richard Barry and learn about running FreeRTOS on RISC-V at FOSDEM 2019

Version 10.1.1 of the FreeRTOS kernel is available for immediate download. MIT licensed.

View a recording of the "OTA Update Security and Reliability" webinar, presented by TI and AWS.


FreeRTOS and other embedded software careers at AWS.

FreeRTOS Partners

ARM Connected RTOS partner for all ARM microcontroller cores

Espressif ESP32

IAR Partner

Microchip Premier RTOS Partner

RTOS partner of NXP for all NXP ARM microcontrollers


STMicro RTOS partner supporting ARM7, ARM Cortex-M3, ARM Cortex-M4 and ARM Cortex-M0

Texas Instruments MCU Developer Network RTOS partner for ARM and MSP430 microcontrollers

OpenRTOS and SafeRTOS

Xilinx Microblaze and Zynq partner