Mike T.
Mike T.

Reputation: 361

How to detect the system locale at compile time using gcc?

I'm using gcc to compile a C program (that is intended to be as portable as possible), and I have a separate module in which I define all the string constants I want to use. These are then defined as external constants in an include file used by the rest of the code.

I would like to use a different set of string constants based upon the user's locale, and my current thinking is to simply define a symbol with the desired language settings and use the pre-processor to conditionally compile the appropriate values. (Yes this does assume the user will be compiling the program themselves).

If I go down this route I can get the current language settings from the command line using

$ echo $LANG | cut -f 1 -d '.'
en_GB

but I have not managed to use this to define the symbol 'en_GB' (using -D) in a make file.

There may of course be a better ways to solve the problem!

It might be better to select the correct string constants at runtime but I don't want to have to update any of the source code other then the module the string constants are defined in.

Thanks

Upvotes: 0

Views: 285

Answers (1)

KamilCuk
KamilCuk

Reputation: 141493

How to detect the system locale at compile time using gcc?

It is not possible, in the way you want to do. gcc compiles code, it does not detect system locale.


Pass the locale with some prefix.

-DLANG_$(echo $LANG | cut -f 1 -d '.')=1

Then just check with preprocessor if the macro is defined

#if LANG_en_US
stuff
#endif

To use shell script inside Makefile, you have to use shell.

CFLAGS += -DLANG_$(shell echo $$LANG | cut -f 1 -d '.')=1

Upvotes: 2

Related Questions