繁体   English   中英

链接 Howard Hinnant 的日期/时区库时出错

[英]Error linking Howard Hinnant's date/time zone library

我正在尝试安装Howard Hinnant 的日期/时区库,以便我可以制作一个显示不同时区时间的程序。 我正在使用 Howard 的时区解析器安装指南,但鉴于我对 C++ 和一般编程仍然是新手,我认识到我可能做错了一些事情。

到目前为止,我已经从 GitHub 下载了源材料,并且我相信我在 Visual Studio Code 中成功编译了tz.cpp (同时链接到源材料中提供的 header 文件)。 我没有选择自定义构建。

然后我尝试运行以下示例程序(在安装指南中提供):

#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';
}

在我的控制台上的“问题”window 下没有出现错误; 但是,终端上会出现以下文本:


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.

[为格式化道歉]

任何关于我可能做错或失败的指示将不胜感激。 如有必要,很高兴提供更多信息。 谢谢!

编辑:这是我的launch.json文件:

  "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"
        }
    ]
}

这是我的tasks.json文件:

{
    // 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"
        }
    ]
}

程序编译正确,但没有链接:

In function `main':
  undefined reference to `date::current_zone()'

In function `date::sys_info date::time_zone::get_info<...>(...) const':
  undefined reference to `date::time_zone::get_info_impl(...) const'

error: ld returned 1 exit status

因此,该问题与运行程序无关,因为该程序尚不存在。

命令行是:

g++.exe -g CLClockv2.cpp -o CLClockv2.exe

其中不包含任何链接标志 ( -l ),这意味着 GCC 不会链接库并在找不到所需符号时产生您在上面看到的错误。 通常,您还需要一个搜索路径标志 ( -L )。


我建议您在尝试手动配置像 VS Code 这样的 IDE 之前学习从命令行编译。 另一个选项是预配置了 MSVC 的 Visual Studio。

更新:我能够让程序按需要运行,部分归功于 Acorn 的反馈。 事实证明,我需要做一些改变。

首先,我需要在程序中包含 tz.cpp。

其次,我需要编辑我的构建命令,不仅要添加 tz.cpp,还要添加 -lole32(感谢https://github.com/HowardHinnant/date/issues/272的 denchat 指出这一点)。 这可以在命令提示符中作为g++ CLClockv2.cpp tz.cpp -lole32 -o CLClockv2.exe或在 Visual Studio Code 中使用以下任务完成。json 文件:

{
    "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
            }
        }
    ]
}

但是,然后我发现我需要从https://www.iana.org/time-zones下载时区数据库的副本(“仅数据”版本对我有用),然后将其解压缩到下载文件夹中命名为 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.

一旦我采取了所有这些步骤,我就能让程序运行起来。

再次感谢你的帮助!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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