Using libraries on the MCU - Page 1
Forum section: electronics / general

author: nForce
author_rank: Frequent Contributor
timestamp: November 03, 2018, 04:08:43 pm
content: If I am writing a program in C for a microcontroller. Can I use libraries which are a standard for a personal computer, or there are some limitations?Let's suppose I want to use the tan function, can I use math.h?Thanks.

author: nctnico
author_rank: Super Contributor
timestamp: November 03, 2018, 04:13:26 pm
content: Quote from: nForce on November 03, 2018, 04:08:43 pmYou can but be aware that math operations are likely to performed in software without a dedicated floating point processor. This means that your controller will need enough flash to store the libraries and that the math operations will be considerably slower. All in all you can use math.h and you'll need to link the software floating point library.

author: ataradov
author_rank: Super Contributor
timestamp: November 03, 2018, 06:09:09 pm
content: Also things like malloc()/free() may not be supported due to memory constrains.

author: hussamaldean
author_rank: Unknown
timestamp: November 03, 2018, 07:06:04 pm
content: or you could use piece of the library in your code to improve the performance and reduce the size needed since the whole library has to be compiled like what I do in 90% of my projects

author: richardman
author_rank: Frequent Contributor
timestamp: November 03, 2018, 07:19:26 pm
content: The short answer is "it depends on the compiler and the target MCU".The long answer:There are certain functions, e.g. tan in math.h that are part of the standard C library. So all conforming C compilers should support it. However, not all compilers are conforming, and sometimes even if it is, it may not support all functions, e.g. ones that depend on a file system (stdio.h) or heap (malloc/free) due to lack of SRAM.Then there's a speed and size consideration for your MCU. An 8 bit AVR may not have enough space to support a floating point function in addition to your code.The short short answer is, "maybe".

author: hans
author_rank: Super Contributor
timestamp: November 04, 2018, 07:01:24 am
content: In C++ you could even consider using the STL library, but these will often use memory allocation. Memory allocation is often undesirable, so there arestaticSTL libraries (3rd party) that are more tailored towards embedded.Then again you may still want to consider not using C++ at all.In C, you can use printf() to print serial data. But where will it appear? Some manufacturers will include, often part of the boilerplate code for a chip or compiler, a char redirection function that for example always spits out the data on the first UART of the chip. You could redirect this to another peripheral.. doesn't even have to be UART.But does it make sense to use getch or sscanf? They are polling functions. Have never tried actually, but conceptually it doesn't make much sense to program that way on embedded.In particular, I think many embedded compilers won't support the stdin/stdout/stderr file streams.

author: hli
author_rank: Frequent Contributor
timestamp: November 04, 2018, 11:11:21 pm
content: When using libraries, especially when they are thought more for Linux or other operation system, be wary of their non-stated requirements. Stdin / stdout have been mentioned, but anything with file access needs some porting. Also, most libraries working with time or date will probably rely on some operation system support - just for getting the timezone (or current date / time).

author: TK
author_rank: Super Contributor
timestamp: November 04, 2018, 11:25:19 pm
content: When you are programming in C or C++ for a microcontroller, you are using a compiler from the toolkit for a specific target (Cross compiler: runs on a host OS, generates code for a TARGET micro that is different from the host) microcontroller. i.e. avrgcc when developing for AVR microcontrollers. You just cannot use any library that is available on the host system because it contains code compiled for intel X86 architecture (mac os x, windows, linux on X86), not AVR. If you check the documentation for avrgcc and tan() is available, then you can certainly use it because it is supported on the target architecture.