简体   繁体   中英

Can't get ms _tzSet() example to compile

I'm using c++ builder 10.2 with the clang compiler on Windows 10 pro. Can anyone tell me why this doesn't compile?

// 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 );
}

I get 3 'Unresolved external' errors relating to _get_daylight, _get_timezone and _get_tzname.

Since I don't have "c++builder", I tried this with MinGW.

With a straightforward compile and link command like this:

gcc -Wall -Werror -pedantic -O2 tz.c -o tz

I got the same errors:

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

A single grep revealed the library libucrtbase.a (among others) to contain the symbol _get_daylight . Adding this library to the command:

gcc -Wall -Werror -pedantic -O2 tz.c -lucrtbase -o tz

This produced a runnable program.

The other libraries are all libmsvcr*.a in different versions, I tried just one of them. This was successful, too.


Edit:

With a not-so-current "clang" I didn't even need to add the library.

clang -Wall -Werror -pedantic -O3 tz.c -o tz-clang.exe

This compiled and linked without any error and runs perfectly.

(clang version 7.0.1 (tags/RELEASE_701/final), Target: x86_64-pc-windows-msvc)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM