【发布时间】:2020-08-17 18:27:28
【问题描述】:
我正在尝试安装Howard Hinnant's date/time zone library,以便我可以制作一个显示不同时区时间的程序。我正在使用 Howard 的 installation guide 进行时区解析器,但考虑到我对 C++ 和一般编程仍然很陌生,我意识到我可能做错了一些事情。
到目前为止,我已经从 GitHub 下载了源材料,并且我相信我在 Visual Studio Code 中成功编译了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:\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"
}
]
}
【问题讨论】:
-
看来你需要链接一个库。你的编译/链接命令行是什么?
-
听起来确实可能是问题所在。我很尴尬地说我不确定我的编译/链接命令行是什么。当我在 Visual Studio Code 中调试程序时,我只需按 F5 并使用 launch.json 文件(我已将其作为问题的编辑包含在内)。
-
其实sounds likeHoward 只是在他的编译命令行中包含
src/tz.cpp而不是链接库。 -
launch.json不包含构建配置。 -
tasks.json是我们需要的文件,可能是配置错误的文件。默认情况下,除非您修改 tasks.json,否则 Visual Stduio Code 将仅编译活动文件。此处的说明:https://code.visualstudio.com/docs/cpp/config-mingw#_modifying-tasksjson 解释所需的更改。
标签: c++ visual-studio-code chrono zone