链接到SFML的未解决的外部错误(SFML 2.2,Visual Studio Community 2013)

Unresolved External errors linking to SFML (SFML 2.2, Visual Studio Community 2013)

本文关键字:SFML Visual Studio 2013 Community 未解决 外部 错误 链接      更新时间:2023-10-16

试图将一个新的C++项目与SFML链接起来,我做了以下操作:

  • 创建了一个空项目
  • C/C++>>常规:附加包含目录:添加了SFML包含目录并从项目默认值继承
  • 链接器>>附加库目录:添加了SFML lib目录并从项目默认值继承
  • 添加了其他依赖项:winmm.lib、openal32.lib、sndfile.lib、opengl32.lib、gdi32.lib、glew.lib、freetype.lib、jpeg.lib、sfml-system-s-d.lib、sfml-audio-s-d.lib、sfml-window-s-d.libsfml-graphics-s-d.lib,以及从项目默认值继承
  • 所有其他项目设置都是默认设置

我从中复制了示例main()http://www.sfml-dev.org/tutorials/2.0/start-vc.php。

当我尝试构建程序时,我得到15个LNK2019未解决的外部错误:

1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::String::String(char const *,class std::locale const &)" (__imp_??0String@sf@@QAE@PBDABVlocale@std@@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::String::~String(void)" (__imp_??1String@sf@@QAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::VideoMode::VideoMode(unsigned int,unsigned int,unsigned int)" (__imp_??0VideoMode@sf@@QAE@III@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Window::close(void)" (__imp_?close@Window@sf@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall sf::Window::isOpen(void)const " (__imp_?isOpen@Window@sf@@QBE_NXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: bool __thiscall sf::Window::pollEvent(class sf::Event &)" (__imp_?pollEvent@Window@sf@@QAE_NAAVEvent@2@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Window::display(void)" (__imp_?display@Window@sf@@QAEXXZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::Color::Color(unsigned char,unsigned char,unsigned char,unsigned char)" (__imp_??0Color@sf@@QAE@EEEE@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::RenderTarget::clear(class sf::Color const &)" (__imp_?clear@RenderTarget@sf@@QAEXABVColor@2@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::RenderTarget::draw(class sf::Drawable const &,class sf::RenderStates const &)" (__imp_?draw@RenderTarget@sf@@QAEXABVDrawable@2@ABVRenderStates@2@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::RenderWindow::RenderWindow(class sf::VideoMode,class sf::String const &,unsigned int,struct sf::ContextSettings const &)" (__imp_??0RenderWindow@sf@@QAE@VVideoMode@1@ABVString@1@IABUContextSettings@1@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall sf::RenderWindow::~RenderWindow(void)" (__imp_??1RenderWindow@sf@@UAE@XZ) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall sf::Shape::setFillColor(class sf::Color const &)" (__imp_?setFillColor@Shape@sf@@QAEXABVColor@2@@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall sf::CircleShape::CircleShape(float,unsigned int)" (__imp_??0CircleShape@sf@@QAE@MI@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall sf::CircleShape::~CircleShape(void)" (__imp_??1CircleShape@sf@@UAE@XZ) referenced in function _main

2 LNK2001未解决的外部:

1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class sf::Color const sf::Color::Green" (__imp_?Green@Color@sf@@2V12@B)
1>main.obj : error LNK2001: unresolved external symbol "__declspec(dllimport) public: static class sf::RenderStates const sf::RenderStates::Default" (__imp_?Default@RenderStates@sf@@2V12@B)

和LNK1120:

1>C:Users<user name>DocumentsDLLTestDebugDLLTest.exe : fatal error LNK1120: 17 unresolved externals

未生成任何程序。

我觉得我错过了一些显而易见的东西,这更可能是关于链接到DLL而不是SFML。我做错了什么?

解决方案:这是特定于SFML的:我链接到了静态版本的SFML DLL(名称中带有-s的DLL)。我切换到了动态版本(例如sfml-system-d.lib),并构建了项目,尽管要运行它,我需要将sfml DLL与可执行文件放在同一目录中。

您正在链接SFML库的静态调试版本。你真的想构建一个静态版本吗?如果是,则必须定义SFML_STATIC。否则,请删除库名称中的-s选项。

我认为dllspec(dllimport)意味着您正在尝试动态链接。需要定义SFML_STATIC,或者将dll放在工作目录中,并在其他库中更改名称。