c – 使用Cmake和Visual Studio 2013的未解析的外部
作者:互联网
我一直遇到使用SFML的cmake和Visual Studio 2013的问题.当我尝试编译我的程序时,我正在为使用SFML的所有东西获得未解析的外部.我在多台机器上遇到过这个问题,在某些情况下还有一些其他的库.
我正在使用以下命令生成我的Visual Studio项目(在VisualStudioProject文件夹中):
Z:/Coding/cmake/cmake-3.1.1-win32-x86/bin/cmake.exe -G “Visual Studio 12” ..
这是我的CMakeLists.txt(它实际上是从我写的python脚本生成的,但我怀疑这很重要).我可以确认正确找到所有库.
cmake_minimum_required (VERSION 2.6)
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin)
set( CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/lib )
set( CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/lib )
project(Game)
include_directories( Z:/Coding/Libraries/SFML/SFML-2.2/include Z:/Coding/Game/src/win32 Z:/Coding/Game/src )
find_library( SFML-GRAPHICS NAMES sfml-graphics PATHS Z:/Coding/Libraries/SFML/SFML-2.2/lib NO_DEFAULT_PATH )
find_library( SFML-WINDOW NAMES sfml-window PATHS Z:/Coding/Libraries/SFML/SFML-2.2/lib NO_DEFAULT_PATH )
find_library( SFML-SYSTEM NAMES sfml-system PATHS Z:/Coding/Libraries/SFML/SFML-2.2/lib NO_DEFAULT_PATH )
add_executable( win32 "Z:/Coding/Game/apps/win32.cpp" )
target_link_libraries( win32 ${SFML-GRAPHICS} ${SFML-WINDOW} ${SFML-SYSTEM} )
这是我简单的SFML测试程序:
#include <string>
#include <iostream>
#include <SFML/Graphics.hpp>
using namespace std;
int main(int argc, char *argv[])
{
sf::RenderWindow window;
window.create(sf::VideoMode(1280, 1024), "Test");
while (true)
{
sf::Event ev;
while (window.pollEvent(ev))
{
if (ev.type = sf::Event::EventType::Closed)
{
window.close();
exit(1);
}
}
}
std::string hello = "Hello";
cout << hello << endl;
}
我正在为以下内容得到未解决的外部因素:
Error 1 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 Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 2 error LNK2019: unresolved external symbol
“__declspec(dllimport) public: __thiscall sf::String::~String(void)”
(__imp_??1String@sf@@QAE@XZ) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 3 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 Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 4 error LNK2019: unresolved external symbol
“__declspec(dllimport) public: void __thiscall
sf::Window::create(class sf::VideoMode,class sf::String const
&,unsigned int,struct sf::ContextSettings const &)”
(__imp_?create@Window@sf@@QAEXVVideoMode@2@ABVString@2@IABUContextSettings@2@@Z)
referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 5 error LNK2019: unresolved external symbol
“__declspec(dllimport) public: void __thiscall
sf::Window::close(void)” (__imp_?close@Window@sf@@QAEXXZ) referenced
in function _main Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 6 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 Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 7 error LNK2019: unresolved external symbol
“__declspec(dllimport) public: __thiscall
sf::RenderWindow::RenderWindow(void)”
(__imp_??0RenderWindow@sf@@QAE@XZ) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 8 error LNK2019: unresolved external symbol
“__declspec(dllimport) public: virtual __thiscall
sf::RenderWindow::~RenderWindow(void)”
(__imp_??1RenderWindow@sf@@UAE@XZ) referenced in function
_main Z:\Coding\Game\VisualStudioProject\win32.obj win32Error 9 error LNK1120: 8 unresolved
externals Z:\Coding\Game\bin\Debug\win32.exe win32
我以前在Windows上使用过相同类型的脚本,在Windows上使用早期的VS成功(至少我记得我有)所以我认为这是VS 2013的新功能,但我完全被难倒了.
提前致谢.
注意:这是一个简单的例子.这种样式的CMakeLists.txt是由脚本生成的,因为它使用的主要代码库非常大(最初是基于Linux的,所以为什么’/’在任何地方使用).
解决方法:
这看起来好像你没有成功地连接三个SFML库中的任何一个.
如果CMakeLists.txt中的find_library调用未能找到库,那么CMake将输出一个致命错误,我相信你已经提到了.
所以我最好的猜测是你试图链接64位版本的SFML库,而你的CMake命令指定32位版本.
要创建64位版本,只需运行:
cmake -G "Visual Studio 12 Win64" ..
您需要清空构建文件夹以将生成器从“Visual Studio 12”更改为“Visual Studio 12 Win64”.
有关VS发生器的更多信息,请参见the docs.
标签:sfml,c,visual-studio-2013,cmake,unresolved-external 来源: https://codeday.me/bug/20190725/1528763.html