Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when using inttypes.h for PRImacros for esp_err_t (IDFGH-10042) #11319

Closed
3 tasks done
aggaddam opened this issue May 3, 2023 · 7 comments
Closed
3 tasks done
Assignees
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF

Comments

@aggaddam
Copy link

aggaddam commented May 3, 2023

Answers checklist.

  • I have read the documentation ESP-IDF Programming Guide and the issue is not addressed there.
  • I have updated my IDF branch (master or release) to the latest version and checked that the issue is present there.
  • I have searched the issue tracker for a similar issue and not found a similar issue.

IDF version.

release/v5.1

Operating System used.

Linux

How did you build your project?

Command line with idf.py

If you are using Windows, please specify command line type.

None

Development Kit.

ESP32C3-DEVKIT

Power Supply used.

USB

What is the expected behavior?

Unsilenced the warning for -Wno-format and replaced %d with %"PRId32" for esp_err_t and size_t. It should work with out typecast

What is the actual behavior?

Unsilenced the warning for -Wno-format and replaced %d with %"PRId32" for esp_err_t. But still, I am getting build errors with the below codes

Example 1: Getting error

#include <inttypes.h>
void app_main(void)
{
    esp_err_t esp_error = ESP_OK;
    ESP_LOGD(TAG, "esp_err = %"PRId32"", esp_err);
}

Example 2: No error after typecast to int32_t

#include <inttypes.h>
void app_main(void)
{
    esp_err_t esp_error = ESP_OK;
    ESP_LOGD(TAG, "esp_err = %"PRId32"", (int32_t)esp_err);
}

Example 3: Getting error

#include <inttypes.h>
void app_main(void)
{
    size_t required_size = 0; 
    ESP_LOGD(TAG, "required_size = %"PRIu32"", required_size);  
}

Example 4: No error after typecast to uint32_t

#include <inttypes.h>
void app_main(void)
{
    size_t required_size = 0; 
    ESP_LOGD(TAG, "required_size = %"PRIu32"", (uint32_t)required_size);  
}

What data types are defined for esp_err_t and size_t in esp idf v5.1? Does the application need to typecast for these values to fix the build errors?

Steps to reproduce.

  1. Step
  2. Step
  3. Step
    ...

Debug Logs.

No response

More Information.

No response

@aggaddam aggaddam added the Type: Bug bugs in IDF label May 3, 2023
@github-actions github-actions bot changed the title Error when using inttypes.h for PRImacros for esp_err_t Error when using inttypes.h for PRImacros for esp_err_t (IDFGH-10042) May 3, 2023
@espressif-bot espressif-bot added the Status: Opened Issue is new label May 3, 2023
@igrr
Copy link
Member

igrr commented May 3, 2023

I think esp_err_t is defined as int in all IDF versions, so it doesn't need the PRI- (exact width) integer format specifiers in this case. You can simply print it as %d.

@aggaddam
Copy link
Author

aggaddam commented May 3, 2023

@igrr In my opinion, it's hard to follow both specifiers. As espressif recommends that all the format specifiers should be changed to PRI-based specifiers to support different platforms and compilers. Is espressif planning to fix these issues in future versions?

what about the size_t case?

@igrr
Copy link
Member

igrr commented May 3, 2023

Sorry if the recommendation in the docs wasn't formulated clearly. We don't recommend replacing all the format specifiers with the PRI-based ones.

inttypes.h specifiers should only be used for fixed-width integer types. See the "Format macro constants" section over at that page, various PRI* macros are used for different types defined in stdint.h, such as uint32_t, uintmax_t, intptr_t, and so on.

For other types, refer to the documentation of printf family of functions. For example, for size_t the format specifier should be %zu.

One note regarding %zu is that when "nano" formatting implementation is used in Newlib (CONFIG_NEWLIB_NANO_FORMAT=y), there is no support for z format modifier. For that reason, we don't use z modifier a lot in IDF, since we generally have to stay compatible with "nano" formatting implementation. If you aren't using it in your project, though, you can use z modifier for size_t, which is better for portability.

@AxelLin
Copy link
Contributor

AxelLin commented May 4, 2023

One note regarding %zu is that when "nano" formatting implementation is used in Newlib (CONFIG_NEWLIB_NANO_FORMAT=y), there is no support for z format modifier. For that reason, we don't use z modifier a lot in IDF, since we generally have to stay compatible with "nano" formatting implementation. If you aren't using it in your project, though, you can use z modifier for size_t, which is better for portability.

So you are aware that this issue needs fix: #9631

@igrr
Copy link
Member

igrr commented May 4, 2023

@AxelLin yes, we are aware of that one and a few others.

@espressif-bot espressif-bot added Status: In Progress Work is in progress Status: Done Issue is done internally Resolution: NA Issue resolution is unavailable and removed Status: Opened Issue is new Status: In Progress Work is in progress labels May 11, 2023
@0xjakob
Copy link
Contributor

0xjakob commented May 12, 2023

@aggaddam We clarified the migration guide a bit. It will be available on master soon.

@zikalino
Copy link
Contributor

closing considering migration guide was udated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally Type: Bug bugs in IDF
Projects
None yet
Development

No branches or pull requests

6 participants