Problem with snprintf()

Post here first, or if you can't find a relevant section!
mmonsterski
Posts: 14
Joined: Wed Mar 08, 2017 12:06 pm

Problem with snprintf()

Postby mmonsterski » Wed Mar 08, 2017 12:16 pm

Hello
I'm trying to port code from Arduino Uno to Bluepill but I have problem with snprintf(). When I'm compiling code I have error:

Code: Select all

test_snprintf:9: error: 'snprintf' was not declared in this scope


In my opinion it's library function and it should be available for use.

stevestrong
Posts: 1011
Joined: Mon Oct 19, 2015 12:06 am
Location: Munich, Germany

Re: Problem with snprintf()

Postby stevestrong » Wed Mar 08, 2017 1:00 pm

Search the forum (top right field on the page) for "sprintf".

mmonsterski
Posts: 14
Joined: Wed Mar 08, 2017 12:06 pm

Re: Problem with snprintf()

Postby mmonsterski » Wed Mar 08, 2017 1:16 pm

I know that sprintf() is included but in this specific application I want to use safer snprintf(). That's why I'm asking about this function.

User avatar
Rick Kimball
Posts: 777
Joined: Tue Apr 28, 2015 1:26 am
Location: Eastern NC, US
Contact:

Re: Problem with snprintf()

Postby Rick Kimball » Wed Mar 08, 2017 3:08 pm

try:

#include <cstdio>

... someplace at the top of the your code
-rick

mmonsterski
Posts: 14
Joined: Wed Mar 08, 2017 12:06 pm

Re: Problem with snprintf()

Postby mmonsterski » Thu Mar 09, 2017 7:55 am

Include <cstdio> just give compiler error.

I've found solution:
http://stm32duino.com/viewtopic.php?f=3&t=1241
In stdio.h snprintf in in preprocessor if:

Code: Select all

#if !defined(__STRICT_ANSI__) || (__STDC_VERSION__ >= 199901L)
[...]
int   _EXFUN(snprintf, (char *__restrict, size_t, const char *__restrict, ...)
               _ATTRIBUTE ((__format__ (__printf__, 3, 4))));
[...]
#endif /* !__STRICT_ANSI__ */

Adding -std=gnu++11 and -std=gnu11 undefine __STRICT_ANSI__

User avatar
RogerClark
Posts: 5577
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Problem with snprintf()

Postby RogerClark » Thu Mar 09, 2017 8:46 pm

Interesting...


There is a separate thread where we are testing whether adding. -std=gnu++11. causes any problems.

But we had not considered also adding -std=gnu11 for C files.

mmonsterski
Posts: 14
Joined: Wed Mar 08, 2017 12:06 pm

Re: Problem with snprintf()

Postby mmonsterski » Fri Mar 10, 2017 7:05 am

I found that because example project using snprintf() compile successfully at Arduino Due. After reading stdio.h and http://stackoverflow.com/questions/5580921/how-can-i-make-c0x-and-strict-ansi-get-along and little investigation I've found at platform.txt for Due:

Code: Select all

compiler.c.flags=[...] -std=gnu11 [...]
compiler.cpp.flags=[...] -std=gnu++11 [...]

I just copy/paste this compiler switches and this solved my problems.

User avatar
RogerClark
Posts: 5577
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Problem with snprintf()

Postby RogerClark » Fri Mar 10, 2017 9:13 am

OK

The plan is to add both the C and C++ flags

I hope to update the repo this weekend

User avatar
BennehBoy
Posts: 418
Joined: Thu Jan 05, 2017 8:21 pm
Location: Yorkshire
Contact:

Re: Problem with snprintf()

Postby BennehBoy » Fri Mar 10, 2017 10:00 am

RogerClark wrote:I hope to update the repo this weekend


Are there any other PR's going in also?
-------------------------------------
https://github.com/BennehBoy

User avatar
RogerClark
Posts: 5577
Joined: Mon Apr 27, 2015 10:36 am
Location: Melbourne, Australia
Contact:

Re: Problem with snprintf()

Postby RogerClark » Fri Mar 10, 2017 8:41 pm

I will need to look at the list if PRs that are pending.

There are some PRs which I will never action,because they solve a problem for one individual but break things for everyone else.

I know there is @stevstrong's SPI PR but it slows down the SPI, last time I tried it, and speed decreases usually dont go down well with users even though there is a valid bug fix that causes the slowdown.

But as Victor is currently working on improved DMA for SPU, I was going to wait and do all the SPI changes at the same time.

So its likely I will only change the compiler switches and change to using nanolib at the weekend


Return to “General discussion”

Who is online

Users browsing this forum: No registered users and 1 guest