c++visual-studio-codec++-chronozone

Error linking Howard Hinnant's date/time zone library


I am trying to install Howard Hinnant's date/time zone library so that I can make a program that displays the time in different time zones. I am using Howard's installation guide for the time zone parser, but given that I am still new to C++ and to programming in general, I recognize I may be doing some things incorrectly.

So far, I have downloaded the source material from GitHub, and I believe I successfully compiled tz.cpp within Visual Studio Code (while linking to the header files provided within the source material). I did not opt to customize the build.

I then tried to run the following sample program (provided in the installation guide):

#include "date/tz.h"
#include <iostream>

int
main()
{
    using namespace date;
    using namespace std::chrono;
    auto t = make_zoned(current_zone(), system_clock::now());
    std::cout << t << '\n';
}

No errors show up under the "problems" window on my console; however, the following text appears on the terminal:


C:\Users\kburc\AppData\Local\Temp\ccqDCN6j.o: In function `main':
c:/Users/kburc/Vol 7/Documents/!Dell64docs/Programming/CPP/KJB3programs/CLClockv2/CLClockv2.cpp:26: undefined reference to `date::current_zone()'
C:\Users\kburc\AppData\Local\Temp\ccqDCN6j.o: In function `date::sys_info date::time_zone::get_info<std::chrono::duration<long long, std::ratio<1ll, 1000000000ll> > >(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long long, std::ratio<1ll, 1000000000ll> > >) const':
c:/Users/kburc/Vol 7/Documents/!Dell64docs/Programming/CPP/KJB3programs/CLClockv2/date/tz.h:896: undefined reference to `date::time_zone::get_info_impl(std::chrono::time_point<std::chrono::_V2::system_clock, std::chrono::duration<long long, std::ratio<1ll, 1ll> > >) const'       
collect2.exe: error: ld returned 1 exit status
The terminal process "C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -Command & 'C:\Program Files\mingw-w64\x86_64-8.1.0-posix-seh-rt_v6-rev0\mingw64\bin\g++.exe' -g 'c:\Users\kburc\Vol 7\Documents\!Dell64docs\Programming\CPP\KJB3programs\CLClockv2\CLClockv2.cpp' -o 'c:\Users\kburc\Vol 7\Documents\!Dell64docs\Programming\CPP\KJB3programs\CLClockv2\CLClockv2.exe'" terminated with exit code: 1.

Terminal will be reused by tasks, press any key to close it.

[Apologies for the formatting]

Any pointers on what I might have done incorrectly or failed to do would be greatly appreciated. Happy to provide more information if necessary. Thank you!

Edit: here is my launch.json file:

  "version": "0.2.0",
    "configurations": [
        {
            "name": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

And here is my tasks.json file:

{
    // Use IntelliSense to learn about possible attributes.
    // Hover to view descriptions of existing attributes.
    // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
    "version": "0.2.0",
    "configurations": [

        {
            "name": "g++.exe - Build and debug active file",
            "type": "cppdbg",
            "request": "launch",
            "program": "${fileDirname}\\${fileBasenameNoExtension}.exe",
            "args": [
                "-g",
"${workspaceFolder}\\*.cpp",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "stopAtEntry": false,
            "cwd": "${workspaceFolder}",
            "environment": [],
            "externalConsole": false,
            "MIMode": "gdb",
            "miDebuggerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gdb.exe",
            "setupCommands": [
                {
                    "description": "Enable pretty-printing for gdb",
                    "text": "-enable-pretty-printing",
                    "ignoreFailures": true
                }
            ],
            "preLaunchTask": "C/C++: g++.exe build active file"
        }
    ]
}

Solution

  • Update: I was able to get the program to run as desired, thanks in part to Acorn's feedback. It turned out that there were a couple of changes I needed to make.

    First, I needed to include tz.cpp within the program.

    Second, I needed to edit my build command to add in not just tz.cpp but -lole32 as well (credit to denchat at https://github.com/HowardHinnant/date/issues/272 for pointing this out). This could be done either within the command prompt as g++ CLClockv2.cpp tz.cpp -lole32 -o CLClockv2.exe or within Visual Studio Code using the following tasks.json file:

    {
        "version": "2.0.0",
        "tasks": [
            {
                "type": "shell",
                "label": "C/C++: g++.exe build active file",
                "command": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe",
                "args": [
                    "-g",
    "${workspaceFolder}\\*.cpp",
    "-lole32",
    "-o",
    "${fileDirname}\\${fileBasenameNoExtension}.exe"
                ],
                "options": {
                    "cwd": "${workspaceFolder}"
                },
                "problemMatcher": [
                    "$gcc"
                ],
                "group": {
                    "kind": "build",
                    "isDefault": true
                }
            }
        ]
    }
    

    However, I then found that I needed to download a copy of the time zone database (the "Data only" version worked for me) from https://www.iana.org/time-zones and then unzip it into a downloads folder named tzdata.

    Finally, I then needed to create a file called windowsZones.xml (available at https://github.com/unicode-org/cldr/blob/master/common/supplemental/windowsZones.xml ) and include it into the downloads folder.

    Once I took all these steps, I was able to get the program to work.

    Thanks again for your help!

    Ken