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



我正在尝试安装Howard Hinnant的日期/时区库,这样我就可以制作一个在不同时区显示时间的程序。我正在使用Howard的时区解析器安装指南,但鉴于我对C++和一般编程还很陌生,我意识到我可能做了一些错误的事情。

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

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

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

在";问题";我控制台上的窗口;但是,终端上出现以下文本:


C:UserskburcAppDataLocalTempccqDCN6j.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:UserskburcAppDataLocalTempccqDCN6j.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:WINDOWSSystem32WindowsPowerShellv1.0powershell.exe -Command & 'C:Program Filesmingw-w64x86_64-8.1.0-posix-seh-rt_v6-rev0mingw64bing++.exe' -g 'c:UserskburcVol 7Documents!Dell64docsProgrammingCPPKJB3programsCLClockv2CLClockv2.cpp' -o 'c:UserskburcVol 7Documents!Dell64docsProgrammingCPPKJB3programsCLClockv2CLClockv2.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之前,先从命令行学习编译。另一个选项是Visual Studio,它与MSVC一起预配置。

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

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

其次,我需要编辑我的构建命令,不仅添加tz.cpp,还添加-lole32(这要归功于denchathttps://github.com/HowardHinnant/date/issues/272用于指出这一点(。这可以在命令提示符下执行g++ CLClockv2.cpp tz.cpp -lole32 -o CLClockv2.exe或在Visual Studio代码中使用以下tasks.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的下载文件夹中。

最后,我需要创建一个名为windowsZones.xml的文件(可在https://github.com/unicode-org/cldr/blob/master/common/supplemental/windowsZones.xml(,并将其包含在下载文件夹中。

一旦我采取了所有这些步骤,我就能够让程序发挥作用。

再次感谢您的帮助!

Ken

最新更新