[c]: 语言环境设置 -- setlocale()
一、语言环境设置【 Linux - Fedora 】
1、【Linux -- 类redhat 】语言环境设置
1.1、查看语言环境【/etc/locale.conf】:/etc/locale.conf
2、【Linux -- 类debian 】语言环境设置
2.1、查看语言环境【/etc/default/locale】:/etc/default/locale
3、【gcc 版本信息】
1 [wit@fedora tmp]$ gcc -v
2
3 Using built-in specs.
4
5 COLLECT_GCC=gcc
6
7 COLLECT_LTO_WRAPPER=/usr/libexec/gcc/x86_64-redhat-linux/13/lto-wrapper
8
9 OFFLOAD_TARGET_NAMES=nvptx-none
10
11 OFFLOAD_TARGET_DEFAULT=1
12
13 Target: x86_64-redhat-linux
14
15 Configured with: ../configure
16
17 --enable-bootstrap
18
19 --enable-languages=c,c++,fortran,objc,obj-c++,ada,go,d,m2,lto
20
21 --prefix=/usr
22
23 --mandir=/usr/share/man
24
25 --infodir=/usr/share/info
26
27 --with-bugurl=http://bugzilla.redhat.com/bugzilla
28
29 --enable-shared
30
31 --enable-threads=posix
32
33 --enable-checking=release
34
35 --enable-multilib
36
37 --with-system-zlib
38
39 --enable-__cxa_atexit
40
41 --disable-libunwind-exceptions
42
43 --enable-gnu-unique-object
44
45 --enable-linker-build-id
46
47 --with-gcc-major-version-only
48
49 --enable-libstdcxx-backtrace
50
51 --with-libstdcxx-zoneinfo=/usr/share/zoneinfo
52
53 --with-linker-hash-style=gnu
54
55 --enable-plugin
56
57 --enable-initfini-array
58
59 --with-isl=/builddir/build/BUILD/gcc-13.2.1-20231011/obj-x86_64-redhat-linux/isl-install
60
61 --enable-offload-targets=nvptx-none
62
63 --without-cuda-driver
64
65 --enable-offload-defaulted
66
67 --enable-gnu-indirect-function
68
69 --enable-cet
70
71 --with-tune=generic
72
73 --with-arch_32=i686
74
75 --build=x86_64-redhat-linux
76
77 --with-build-config=bootstrap-lto
78
79 --enable-link-serialization=1
80
81 Thread model: posix
82
83 Supported LTO compression algorithms: zlib zstd
84
85 gcc version 13.2.1 20231011 (Red Hat 13.2.1-4) (GCC)
4、【gcc 帮助信息】
1 [wit@fedora tmp]$ gcc --help
2 Usage: gcc [options] file...
3 Options:
4 -pass-exit-codes Exit with highest error code from a phase.
5 --help Display this information.
6 --target-help Display target specific command line options (including assembler and linker options).
7 --help={common|optimizers|params|target|warnings|[^]{joined|separate|undocumented}}[,...].
8 Display specific types of command line options.
9 (Use '-v --help' to display command line options of sub-processes).
10 --version Display compiler version information.
11 -dumpspecs Display all of the built in spec strings.
12 -dumpversion Display the version of the compiler.
13 -dumpmachine Display the compiler's target processor.
14 -foffload=<targets> Specify offloading targets.
15 -print-search-dirs Display the directories in the compiler's search path.
16 -print-libgcc-file-name Display the name of the compiler's companion library.
17 -print-file-name=<lib> Display the full path to library <lib>.
18 -print-prog-name=<prog> Display the full path to compiler component <prog>.
19 -print-multiarch Display the target's normalized GNU triplet, used as
20 a component in the library path.
21 -print-multi-directory Display the root directory for versions of libgcc.
22 -print-multi-lib Display the mapping between command line options and
23 multiple library search directories.
24 -print-multi-os-directory Display the relative path to OS libraries.
25 -print-sysroot Display the target libraries directory.
26 -print-sysroot-headers-suffix Display the sysroot suffix used to find headers.
27 -Wa,<options> Pass comma-separated <options> on to the assembler.
28 -Wp,<options> Pass comma-separated <options> on to the preprocessor.
29 -Wl,<options> Pass comma-separated <options> on to the linker.
30 -Xassembler <arg> Pass <arg> on to the assembler.
31 -Xpreprocessor <arg> Pass <arg> on to the preprocessor.
32 -Xlinker <arg> Pass <arg> on to the linker.
33 -save-temps Do not delete intermediate files.
34 -save-temps=<arg> Do not delete intermediate files.
35 -no-canonical-prefixes Do not canonicalize paths when building relative
36 prefixes to other gcc components.
37 -pipe Use pipes rather than intermediate files.
38 -time Time the execution of each subprocess.
39 -specs=<file> Override built-in specs with the contents of <file>.
40 -std=<standard> Assume that the input sources are for <standard>.
41 --sysroot=<directory> Use <directory> as the root directory for headers
42 and libraries.
43 -B <directory> Add <directory> to the compiler's search paths.
44 -v Display the programs invoked by the compiler.
45 -### Like -v but options quoted and commands not executed.
46 -E Preprocess only; do not compile, assemble or link.
47 -S Compile only; do not assemble or link.
48 -c Compile and assemble, but do not link.
49 -o <file> Place the output into <file>.
50 -pie Create a dynamically linked position independent
51 executable.
52 -shared Create a shared library.
53 -x <language> Specify the language of the following input files.
54 Permissible languages include: c c++ assembler none
55 'none' means revert to the default behavior of
56 guessing the language based on the file's extension.
57
58 Options starting with -g, -f, -m, -O, -W, or --param are automatically
59 passed on to the various sub-processes invoked by gcc. In order to pass
60 other options on to these processes the -W<letter> options must be used.
61
62 For bug reporting instructions, please see:
63 <http://bugzilla.redhat.com/bugzilla>.
64 [wit@fedora tmp]$
65 [wit@fedora tmp]$
二、源代码
1 // locale.c
2
3
4 #include <locale.h>
5 #include <stdio.h>
6 #include <time.h>
7
8
9 int main ()
10 {
11 time_t currtime;
12 struct tm *timer;
13 char buffer[80];
14
15 time( &currtime );
16 timer = localtime( &currtime );
17
18 printf("\n\nLocale is: %s\n", setlocale(LC_ALL, "en_GB.UTF-8"));
19 strftime(buffer,80,"%c", timer );
20 printf("Date is: %s\n", buffer);
21
22 printf("\n\nLocale is: %s\n", setlocale(LC_ALL, "zh_CN.UTF-8"));
23 strftime(buffer,80,"%c", timer );
24 printf("Date is: %s\n", buffer);
25 printf("\n\n");
26
27 return 0;
28
29 }
三、运行结果
1 [wit@fedora tmp]$ gcc -g -Wall -std=c2x -o locale locale.c && ./locale
2
3
4 Locale is: en_GB.UTF-8
5 Date is: Thu 18 Jan 2024 22:56:25 CST
6
7
8 Locale is: zh_CN.UTF-8
9 Date is: 2024年01月18日 星期四 22时56分25秒
10
11
12 [wit@fedora tmp]$
13 [wit@fedora tmp]$
四、参考资料
1、 C 库函数 - setlocale() -- https://www.runoob.com/cprogramming/c-function-setlocale.html
本文由 lnlidawei 原创、整理、转载,本文来自于【博客园】; 整理和转载的文章的版权归属于【原创作者】; 转载或引用时请【保留文章的来源信息】:https://www.cnblogs.com/lnlidawei/p/17973598