[英]Can't get ms _tzSet() example to compile
我在 Windows 10 pro 上使用 c++ builder 10.2 和 clang 編譯器。 誰能告訴我為什么這不能編譯?
// crt_tzset.cpp
// This program uses _tzset to set the global variables
// named _daylight, _timezone, and _tzname. Since TZ is
// not being explicitly set, it uses the system time.
#include <time.h>
#include <stdlib.h>
#include <stdio.h>
int main( void )
{
_tzset();
int daylight;
_get_daylight( &daylight );
printf( "_daylight = %d\n", daylight );
long timezone;
_get_timezone( &timezone );
printf( "_timezone = %ld\n", timezone );
size_t s;
char tzname[100];
_get_tzname( &s, tzname, sizeof(tzname), 0 );
printf( "_tzname[0] = %s\n", tzname );
exit( 0 );
}
我收到 3 個與 _get_daylight、_get_timezone 和 _get_tzname 相關的“未解決的外部”錯誤。
因為我沒有“c++builder”,所以我用 MinGW 嘗試了這個。
使用如下簡單的編譯和鏈接命令:
gcc -Wall -Werror -pedantic -O2 tz.c -o tz
我得到了同樣的錯誤:
C:\Users\###\AppData\Local\Temp\ccI8j8Mj.o:tz.c:(.text.startup+0x1f): undefined reference to `__imp__get_daylight'
C:\Users\###\AppData\Local\Temp\ccI8j8Mj.o:tz.c:(.text.startup+0x3a): undefined reference to `__imp__get_timezone'
C:\Users\###\AppData\Local\Temp\ccI8j8Mj.o:tz.c:(.text.startup+0x61): undefined reference to `__imp__get_tzname'
collect2.exe: error: ld returned 1 exit status
單個 grep 顯示庫libucrtbase.a
(以及其他)包含符號_get_daylight
。 將此庫添加到命令中:
gcc -Wall -Werror -pedantic -O2 tz.c -lucrtbase -o tz
這產生了一個可運行的程序。
其他庫都是不同版本的libmsvcr*.a
,我只嘗試了其中一個。 這也很成功。
編輯:
使用不太流行的“clang”,我什至不需要添加庫。
clang -Wall -Werror -pedantic -O3 tz.c -o tz-clang.exe
這編譯和鏈接沒有任何錯誤並且運行完美。
(clang 版本 7.0.1 (tags/RELEASE_701/final),目標:x86_64-pc-windows-msvc)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.