【发布时间】:2021-08-16 18:10:42
【问题描述】:
我尝试编译从 sfml 教程复制的这个程序
#include <SFML/Graphics.hpp>
int main()
{
// create the window
sf::RenderWindow window(sf::VideoMode(800, 600), "My window");
// run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
// clear the window with black color
window.clear(sf::Color::Black);
// draw everything here...
// window.draw(...);
// end the current frame
window.display();
}
return 0;
}
使用以下tasks.json:
{
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
"label": "g++",
"command": "g++",
"args": [
"-g",
"${file}",
"-o",
"${fileDirname}\\${fileBasenameNoExtension}.exe",
"-ID:\\Personal\\SFML\\include",
"-LD:\\Personal\\SFML\\lib",
"-lsfml-graphics",
"-lsfml-window",
"-lsfml-system",
"-lsfml-network"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "C:\\MinGW\\bin\\g++.exe"
}
]
}
但每次我这样做时,它都会为每行包含 sfml 内容的行显示“未定义的引用”错误。
g++ -g "D:\Prog 2\SC2\sc.cpp" -o "D:\Prog 2\SC2\sc.exe" -ID:\Personal\SFML\include -LD:\Personal\SFML\lib -lsfml-graphics -lsfml-window -lsfml-system -lsfml-network
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: C:\Users\pwucn\AppData\Local\Temp\ccERIoYa.o: in function `main':
D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf6StringC1EPKcRKSt6locale'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf12RenderWindowC1ENS_9VideoModeERKNS_6StringEjRKNS_15ContextSettingsE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:9: undefined reference to `_imp___ZNK2sf6Window6isOpenEv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:13: undefined reference to `_imp___ZN2sf6Window9pollEventERNS_5EventE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:17: undefined reference to `_imp___ZN2sf6Window5closeEv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:21: undefined reference to `_imp___ZN2sf5Color5BlackE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:21: undefined reference to `_imp___ZN2sf12RenderTarget5clearERKNS_5ColorE'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:27: undefined reference to `_imp___ZN2sf6Window7displayEv'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
c:/mingw/bin/../lib/gcc/mingw32/9.2.0/../../../../mingw32/bin/ld.exe: D:/Prog 2/SC2/sc.cpp:6: undefined reference to `_imp___ZN2sf12RenderWindowD1Ev'
collect2.exe: error: ld returned 1 exit status
即使在我按照另一篇文章的建议将 dll 文件包含到项目中之后,这种情况仍然会发生。我在这里做错了吗?
【问题讨论】:
-
您可能需要添加错误消息的确切文本。您使用的
sfml二进制文件也是为 mingw 或 msvc 编译的吗? -
究竟哪些符号未定义?请edit您的问题显示实际错误,完整且完整地复制粘贴(作为文本!)。
-
看起来您正在尝试将 msvc 二进制文件与 mingw 链接。相关:https://stackoverflow.com/questions/48661676/sfml-undefined-reference-to-imp
-
如果您使用 mingw,我的建议是使用 msys2 来提供 mingw 并将其包管理器用于第三方库。 https://www.msys2.org/ 和 https://packages.msys2.org/package/mingw-w64-x86_64-sfml 要在 msys2 中安装 64 位二进制文件,请在 msys2 shell 中键入
pacman -S mingw-w64-x86_64-sfml。
标签: c++ visual-studio-code sfml