-fsigned-char not working?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

-fsigned-char not working?

1,315 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KevinWMatthews on Thu Mar 20 10:40:31 MST 2014
Hello,

I'm running a project in version 6.1.4 and have haven't been able to get the -fsigned-char compiler flag to take effect. I have entered the flag under both MCU C++ Compiler->Miscellaneous->Other Flags and MCU C Compiler->Miscellaneous->Other Flags.

I've been testing this using the following code:
#include <limits.h>
#if CHAR_MIN < 0
#warning( "default char is signed" )
#else
#warning( "default char is unsigned" )
#endif

The "unsigned" warning is shown.

Any ideas on how to force the default value for char to be signed?

Thanks,

-Kevin
0 Kudos
Reply
6 Replies

1,234 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Tue Mar 25 02:35:10 MST 2014
I see no problems in my testing here with the values from Newlib's  limits.h being picked up when code is compiled with -fsigned-char. I suggest you check you build log to ensure that the option is being passed into the compiler in your project.

http://www.lpcware.com/content/faq/lpcxpresso/build-console

If you are convinced there is an issue here, then please post a simple buildable example project that shows it up:

http://www.lpcware.com/content/faq/lpcxpresso/how-importexport-projects

[We will investigate updating Redlib's definitions of limits.h to cope with building for signed chars for a future release.]

Regards,
LPCXpresso Support
0 Kudos
Reply

1,234 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by TheFallGuy on Mon Mar 24 15:22:28 MST 2014
What are the options given to the compiler? (Not the linker.) Look in the build log to see what options are supplied.
0 Kudos
Reply

1,234 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KevinWMatthews on Mon Mar 24 10:44:25 MST 2014
Hi,

We are planning to go back and use more explicit variable types. It's a bit of a project, sadly, but it seems like the way to go.

I'm still curious about the -fsigned-char issue. As far as I can tell we are using Newlib. I read through
http://www.lpcware.com/content/faq/lpcxpresso/switching-selected-c-library
to verify that we had Newlib selected there.

I also looked at our linker script file and found that we appear to be using Newlib (Nohost):
GROUP(libgcc.a libc.a libstdc++.a libm.a libcr_newlib_nohost.a crti.o crtn.o crtbegin.o crtend.o)
(taken from our .ld file).

Any thoughts?

Thanks!
0 Kudos
Reply

1,234 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Sat Mar 22 07:13:23 MST 2014
You are probably using Redlib rather than Newlib.

http://www.lpcware.com/content/faq/lpcxpresso/redlib-and-newlib

The Redlib implementation of limits.h assumes that char is unsigned (as is standard on ARM).

I would suggest that you might be better off changing you code to explictly use unsigned and signed chars (or better still use the types defined in stdint.h such as int8_t and uint8_t) rather than using the -fsigned-char option to force the signedness of 'char'.  Doing this will prevent possible similar implementation issues of this type in the future with your codebase.

Regards,
LPCXpresso Support
0 Kudos
Reply

1,234 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by KevinWMatthews on Fri Mar 21 15:40:29 MST 2014
Shouldn't having __CHAR_UNSIGNED__ undefined result in CHAR_MIN being defined as SCHAR_MIN? After looking in limits.h (under newlib_fixed), the code seems to point to this happening. However, this isn't the behavior that I observe.

Here's the code that I'm referring to:
#ifdef __CHAR_UNSIGNED__
# undef CHAR_MIN
# if __SCHAR_MAX__ == __INT_MAX__
#  define CHAR_MIN 0U
# else
#  define CHAR_MIN 0
# endif
# undef CHAR_MAX
# define CHAR_MAX UCHAR_MAX

#else
# undef CHAR_MIN
# define CHAR_MIN SCHAR_MIN
# undef CHAR_MAX
# define CHAR_MAX SCHAR_MAX
#endif

The code in bold executes regardless of the state of -fsigned-char.

I've attached the limits.h file that I'm referring to. Perhaps I'm looking in the wrong place?
0 Kudos
Reply

1,234 Views
lpcware
NXP Employee
NXP Employee
Content originally posted in LPCWare by lpcxpresso-support on Thu Mar 20 10:59:00 MST 2014
In GCC, the symbol
__CHAR_UNSIGNED__
is defined as 1 for unsigned (the default) or is undefined when you use -fsigned-char

i.e. The compiler does as you ask, but your check is wrong.
0 Kudos
Reply