如何解决如何使用 Visual Studio Code 将库正确链接到我的 C++ 程序
我第一次尝试在 Visual Studio Code(不是 Visual Studio)中使用库。我在使用 tasks.json
和 c_cpp_properties.json
文件链接 SFML library 时遇到问题。它们包含用于构建我的 .cpp 和 .hpp 文件的库和编译器标志的路径。我的研究表明错误可能是由于链接器造成的,并且可能需要 -l
编译器标志。我发现很少有资源详细说明该标志的实际作用以及我在构建项目时应该如何实施它。
我的项目文件夹结构如下:
Test_Project (Root Folder)
﹂ build (main.exe compile location)
﹂ include (personal .hpp files)
﹂ src (main.cpp)
﹂ lib (only has SFML library)
﹂ SFML-2.5.1
﹂ bin (has a bunch of .dll files)
﹂ include (folder)
﹂ SFML (has library specific .hpp files)
﹂ lib (has a bunch of .lib files)
我的 tasks.json
文件如下。我已经评论了可能有用的部分。 ${workspaceFolder}
指的是根文件夹,也就是 Test_Project
。
{
"version": "2.0.0","tasks": [
{
"type": "cppbuild","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": [
"-std=c++17",//specify c++17
"-g",//give debug info
"-I${workspaceFolder}/include",//include personal .hpp files
"-I${workspaceFolder}/lib/SFML-2.5.1/include",//include library .hpp files
"-L${workspaceFolder}/lib/SFML-2.5.1/lib",//add .lib files from library
"${workspaceFolder}/src/*.cpp",//builds with all .cpp files
"-o",//-l flag should go before here???
"${workspaceFolder}/build/main.exe"
],"options": {
"cwd": "${fileDirname}"
},"problemmatcher": [
"$gcc"
],"group": {
"kind": "build","isDefault": true
},"detail": "compiler: \"C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\g++.exe\""
}
]
}
我的 c_cpp_properties.json
在下面。我真的不明白为什么我需要在此处和 tasks.json
中指定包含路径,但我阅读了 this Stack Overflow 帖子,该帖子将库中的包含也放在包含路径中。
{
"configurations": [
{
"name": "Win32","includePath": [
"${workspaceFolder}/**","${workspaceFolder}/lib/SFML-2.5.1/include" // <---
],"defines": [
"_DEBUG","UNICODE","_UNICODE"
],"compilerPath": "C:\\Program Files\\mingw-w64\\x86_64-8.1.0-posix-seh-rt_v6-rev0\\mingw64\\bin\\gcc.exe","cStandard": "gnu17","intelliSenseMode": "windows-gcc-x64"
}
],"version": 4
}
我尝试使用不同的编译器版本 (MinGW 7.3.0) 进行构建,因为 SFML 推荐它,但仍然出现错误。我的错误如下。
C:/.../Test_Project/src/main.cpp:15: undefined reference to `__imp__ZNK2sf6Window6isOpenEv'
C:/.../Test_Project/src/main.cpp:18: undefined reference to `__imp__ZN2sf6Window9pollEventERNS_5EventE'
C:/.../Test_Project/src/main.cpp:22: undefined reference to `__imp__ZN2sf6Window5closeEv'
C:/.../Test_Project/src/main.cpp:26: undefined reference to `__imp__ZN2sf5ColorC1Ehhhh'
etc...
我正在运行的测试代码是一个简单的 hello world 脚本,它应该显示一个红色圆圈。
#include <SFML/Graphics.hpp>
int main() {
auto dimension = sf::VideoMode(1280u,720u);
auto title = "hello world";
sf::RenderWindow window;
window.create(dimension,title);
sf::CircleShape circle;
circle.seTradius(100);
circle.setPosition(200,200);
circle.setFillColor(sf::Color::Red);
while (window.isopen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
window.clear();
window.draw(circle);
window.display();
}
}
我知道 Visual Studio 有方法可以轻松添加这些链接器依赖项,但我所遵循的教程并没有真正描述它们的工作原理,而且我更喜欢使用 VSCode。任何对 VSCode 的进一步阅读都会很棒,因为 VSCode 上的 C++ 主页并没有真正描述库实现。有关如何进行的任何信息也将很棒。谢谢! :)
解决方法
您需要使用 -L 标志指定搜索路径,然后使用 -l 指定库文件的名称。我建议查看编译器文档,而不是有关 vscode 的信息。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。