1

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.

NoComprende
  • 731
  • 4
  • 14
  • 1
    Unresolved external indicates a linker error, not an compile error - correct?. If you look it up, you will find that __get_daylight is included in the Universal C runtime. That means you probably don't link against it. Now it depends on your project/IDE how you do the linking. – Wolfgang May 26 '20 at 09:00
  • I don't doubt your word Wolfgang but I can see no such option in the project manager within the IDE. I've never come across this before and I've no idea where this C runtime library exists. Hopefully I don't have to download Visual ******* C. – NoComprende May 26 '20 at 09:24
  • If you look up the documentation of `_tzset()` and its companions, there should be at least two things: which *header file* to include to make them known to the compiler, and which *library* to link with. -- Your project manager (I don't know c++builder) certainly has some way to let you define the libraries to link to. You need to look for "libraries", not "tzset". – the busybee May 26 '20 at 10:53
  • @the busybee I've been looking but can only find the #nclude . – NoComprende May 26 '20 at 12:19
  • I'm none the wiser after reading this https://stackoverflow.com/questions/2766233/what-is-the-c-runtime-library. – NoComprende May 26 '20 at 12:36
  • Did you ask the guys at the c++builder forum? They do have a forum, don't they? This looks to me as an IDE problem, not a C language problem. – the busybee May 26 '20 at 15:46
  • @the busybee It's unobtainable more often than not. – NoComprende May 26 '20 at 17:59
  • Since this function is in the CRT, you might need to link to that. – the busybee May 26 '20 at 20:24

1 Answers1

2

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 busybee
  • 10,755
  • 3
  • 13
  • 30
  • Thanks for your time busybee but TBH I've no experience of command line. I run everything from within the IDE. I can't even find what version of clang I'm using (it's not mentioned in the About section) although going by this page http://docwiki.embarcadero.com/RADStudio/Rio/en/Clang-enhanced_C%2B%2B_Compilers it may be version 5. If I even knew what the library file was called I could search my hard drive and then add it in the IDE. – NoComprende May 27 '20 at 14:17
  • 1
    "*If I even knew what the library file was called I could search my hard drive and then add it in the IDE*" - That is what grep tools are good for. They allow you to search the contents of files looking for keywords to find. So, you could try grepping the libs provided with the IDE looking for `get_daylight`, `get_timezone`, `get_tzname`, etc – Remy Lebeau May 28 '20 at 00:15
  • @Remy Lebeau Thanks Remy. I downloaded grepwin and searched for '_get_daylight' in the C:\Program Files (x86)\Embarcadero\Studio\19.0 folder. It returned 1 file MSVCR100.DLL in the bin/subversion sub-directory. I added that to the project and then got the same unresolved externals but this time the names were preceeded by double underscores. I tried searching for '__get_daylight' but this time nothing was found. – NoComprende May 28 '20 at 16:03
  • 1
    That won't work. A `.DLL` is the wrong file to add, you need a `.LIB` or `.A` file instead, depending on which platform you are compiling for. MSVCR is the Microsoft Visual C Runtime library, which is not what you want to add anyway. The `/bin` folder contains files that belong to the IDE itself, it does not contain files you can add to your own projects. Stay out of that folder. MSVCR is a dependancy for the IDE's SubVersion integration. The `/lib` folder contains files you can add to your project. Did you try grepping for `get_daylight` (no leading `_`s), like I suggested earlier? – Remy Lebeau May 28 '20 at 19:00
  • 1
    @NoComprende Are you sure that your IDE uses Clang as the compiler? Where is it installed? Which version is it? What did your research on the project's options for libraries reveal? – the busybee May 28 '20 at 19:23
  • @Remy Lebeau I tried 'get_daylight' first and got the unresolved externals with 1 leading underscore. I then tried searching for '_get_daylight' and you know the rest. – NoComprende May 28 '20 at 19:32
  • @busybee I searched for All sizes & include system folders, subfolders, hidden items and binary files. I've definitely got 'Use Borland Classic compiler' unchecked in the project options which I'm fairly sure means the clang compiler is used. I'm currently searching for clang files anywhere on c:/. Will report back tomorrow. Thanks to you and Remy for your help. – NoComprende May 28 '20 at 19:45
  • 1
    My point was, regardless of what the linker says, did you find the `get_daylight` **substring** *anywhere* in the files of the `/lib` folder? If not, then it is possible that maybe Embarcadero forgot to ship the relevant lib file (it happens), who knows. The real question is, why are you trying to use these vendor-specific functions in the first place, and not something more standardized instead? – Remy Lebeau May 28 '20 at 19:53
  • @Remy Lebeau That DLL file was the only file it was found in and that included the lib folder. I've tried all sorts Remy and had similar results. It's related to this thread https://stackoverflow.com/questions/61989232/struct-tm-tm-isdst-disagrees-with-bst. – NoComprende May 28 '20 at 20:49
  • 1
    Does c++builder show a build console while building? Most IDEs do. You can watch what commands are executed to compile and link. This should show the compiler used. -- Anyway, what about a try outside c++builder? It seems to be too difficult to debug your setting while at least *two* beasts (IDE and compiler system) are involved. Please consider compiling and linking your program from the command line. – the busybee May 29 '20 at 07:11
  • @the busybee bcc32c is the only thing I see that seems relevant to the compiler used. I copied and pasted all the build messages into notepad and searched for clang but nothing was found. Embarcadero website mentions 'Clang enhanced C++ compilers' so it's maybe not a pure clang compiler. Command line compilation is something I intend to look at but I've currently never done it. I'm seriously thinking of moving to Linux. Thanks. – NoComprende May 29 '20 at 12:09
  • Leaving the Microsoft land is a valuable experience in any case. ;-) Did that 20 years ago at home, and never regret it. At work I have to use it, though, and it's often causing troubles. – the busybee May 29 '20 at 20:09