I have a CMake C++ project that uses some third party library (GameNetworkingSockets).
I'm targeting linux and windows, but I'm working on my linux machine on a daily basis, so I'm not very used to building applications with msvc.
Today my application builds and works well on linux, but fails to link this library when building on windows with MSVC 2019 :
error LNK2019: unresolved external symbol __imp_SteamNetworkingUtils
I found a lot of other questions and answers to this kind of error, but nothing I found was useful to me.
EDIT : One of the question/answers I read was this one and even if it was very interesting, it did not help me solve my problem because it is too generic.
I want to link statically to this library, and I managed that in my CMake project by using in my CMakeLists.txt
file :
find_package(GameNetworkingSockets 1.2.0 REQUIRED)
target_include_directories(my_project PRIVATE ${GameNetworkingSockets_INCLUDE_DIR})
target_link_libraries(my_project ${GameNetworkingSockets_LIBRARY})
And I made a FindGameNetworkingSockets.cmake
module file in which I'm telling CMake to look for libGameNetworkingSockets_s.a
or GameNetworkingSockets_s.lib
for the library that has to be built beforehand, and to look for steam/isteamnetworkingutils.h
to find the right header include directory :
# Try to find GameNetworkingSockets lib
# Check for SDK_ROOT
if( NOT SDK_ROOT )
message(FATAL_ERROR "You have to set SDK_ROOT before calling find_package().")
endif()
# Set search paths
set( _GameNetworkingSockets_HEADER_SEARCH_DIR "${SDK_ROOT}/include/GameNetworkingSockets" )
set( _GameNetworkingSockets_LIB_SEARCH_DIR "${SDK_ROOT}/lib" )
# Search for the headers
FIND_PATH(GameNetworkingSockets_INCLUDE_DIR "steam/isteamnetworkingutils.h" PATHS "${_GameNetworkingSockets_HEADER_SEARCH_DIR}")
# Search for the library
FIND_LIBRARY(GameNetworkingSockets_LIBRARY NAMES libGameNetworkingSockets_s.a GameNetworkingSockets_s.lib PATHS ${_GameNetworkingSockets_LIB_SEARCH_DIR} NO_DEFAULT_PATH)
INCLUDE(FindPackageHandleStandardArgs)
FIND_PACKAGE_HANDLE_STANDARD_ARGS(GameNetworkingSockets DEFAULT_MSG GameNetworkingSockets_LIBRARY GameNetworkingSockets_INCLUDE_DIR)
message(STATUS "GameNetworkingSockets library was found : ${GameNetworkingSockets_LIBRARY}")
message(STATUS "GameNetworkingSockets header was found in : ${GameNetworkingSockets_INCLUDE_DIR}")
When running CMake, both within linux or windows, I can see the library and its header directory are found :
linux
-- GameNetworkingSockets library was found : /sdk/debug/lib/libGameNetworkingSockets_s.a
-- GameNetworkingSockets header was found in : /sdk/debug/include/GameNetworkingSockets
windows
-- GameNetworkingSockets library was found : C:/sdk/debug/lib/GameNetworkingSockets_s.lib
-- GameNetworkingSockets header was found in : C:/sdk/debug/include/GameNetworkingSockets
Then, when building on linux (using Ninja generator) :
[...]
[245/245 4.5/sec] Linking CXX executable my_project
And looking through the executable with nm my_project | grep SteamNetworkingUtils
I can see lots of symbols related to the library, even if the names are mangled :
0000000000c39ee0 T SteamNetworkingUtils
000000000143c278 b _ZGVZ20SteamNetworkingUtilsE7s_utils
0000000000c537a0 t _ZN21ISteamNetworkingUtils20SetConfigValueStructERK28SteamNetworkingConfigValue_t27ESteamNetworkingConfigScopel
0000000000c34cd0 T _ZN21ISteamNetworkingUtilsD1Ev
0000000000c34cd0 t _ZN21ISteamNetworkingUtilsD2Ev
0000000000c39d30 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils13GetTimeSecureEv
0000000000c39790 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils14GetConfigValueE27ESteamNetworkingConfigValue27ESteamNetworkingConfigScopelP30ESteamNetworkingConfigDataTypePvPm
0000000000c394c0 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils14SetConfigValueE27ESteamNetworkingConfigValue27ESteamNetworkingConfigScopel30ESteamNetworkingConfigDataTypePKv
0000000000c393c0 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils15AllocateMessageEi
0000000000c393e0 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils17GetLocalTimestampEv
0000000000c39a10 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils18GetConfigValueInfoE27ESteamNetworkingConfigValuePPKcP30ESteamNetworkingConfigDataTypeP27ESteamNetworkingConfigScopePS1_
0000000000c39bc0 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils19GetFirstConfigValueEv
0000000000c39400 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils22SetDebugOutputFunctionE38ESteamNetworkingSocketsDebugOutputTypePFvS1_PKcE
0000000000c39c30 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils30SteamNetworkingIPAddr_ToStringERK21SteamNetworkingIPAddrPcmb
0000000000c39ca0 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils32SteamNetworkingIdentity_ToStringERK23SteamNetworkingIdentityPcm
0000000000c39c70 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils33SteamNetworkingIPAddr_ParseStringEP21SteamNetworkingIPAddrPKc
0000000000c39cd0 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils35SteamNetworkingIdentity_ParseStringEP23SteamNetworkingIdentityPKc
0000000000c39d10 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtils8GetAppIDEv
0000000000c39390 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtilsD0Ev
0000000000c39370 t _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtilsD1Ev
0000000000c39370 T _ZN25SteamNetworkingSocketsLib21CSteamNetworkingUtilsD2Ev
0000000000c35520 t _ZN25SteamNetworkingSocketsLib23CSteamNetworkingSocketsC1EPNS_21CSteamNetworkingUtilsE
0000000000c35520 T _ZN25SteamNetworkingSocketsLib23CSteamNetworkingSocketsC2EPNS_21CSteamNetworkingUtilsE
0000000001302548 D _ZTVN25SteamNetworkingSocketsLib21CSteamNetworkingUtilsE
000000000130e418 d _ZZ20SteamNetworkingUtilsE7s_utils
However, when building under windows with MSVC 2019, still ninja generator (sorry, my windows OS is in french so these are french error messages) :
FAILED: my_project.exe
cmd.exe /C "cd . && "C:\Program Files\CMake\bin\cmake.exe" -E vs_link_exe --intdir=CMakeFiles\my_project.dir --rc=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\rc.exe --mt=C:\PROGRA~2\WI3CF2~1\10\bin\100190~1.0\x64\mt.exe --manifests -- C:\PROGRA~2\MICROS~2\2019\PROFES~1\VC\Tools\MSVC\1428~1.299\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\my_project.rsp /out:my_project.exe /implib:my_project.lib /pdb:my_project.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console && cd ."
LINK Pass 1: command "C:\PROGRA~2\MICROS~2\2019\PROFES~1\VC\Tools\MSVC\1428~1.299\bin\Hostx64\x64\link.exe /nologo @CMakeFiles\my_project.rsp /out:my_project.exe /implib:my_project.lib /pdb:my_project.pdb /version:0.0 /machine:x64 /debug /INCREMENTAL /subsystem:console /MANIFEST /MANIFESTFILE:CMakeFiles\my_project.dir/intermediate.manifest CMakeFiles\my_project.dir/manifest.res" failed (exit code 1120) with the following output:
servernetworkmanager.cpp.obj : error LNK2019: symbole externe non résolu __imp_SteamNetworkingUtils référencé dans la fonction "public: bool __cdecl ServerNetworkManager::InitSteamDatagramConnectionSockets(void)" (?InitSteamDatagramConnectionSockets@ServerNetworkManager@@QEAA_NXZ)
servernetworkmanager.cpp.obj : error LNK2019: symbole externe non résolu __imp_SteamNetworkingSockets référencé dans la fonction "public: bool __cdecl ServerNetworkManager::InitSteamDatagramConnectionSockets(void)" (?InitSteamDatagramConnectionSockets@ServerNetworkManager@@QEAA_NXZ)
servernetworkmanager.cpp.obj : error LNK2019: symbole externe non résolu __imp_GameNetworkingSockets_Init référencé dans la fonction "public: bool __cdecl ServerNetworkManager::InitSteamDatagramConnectionSockets(void)" (?InitSteamDatagramConnectionSockets@ServerNetworkManager@@QEAA_NXZ)
servernetworkmanager.cpp.obj : error LNK2019: symbole externe non résolu __imp_GameNetworkingSockets_Kill référencé dans la fonction "public: void __cdecl ServerNetworkManager::ShutdownSteamDatagramConnectionSockets(void)" (?ShutdownSteamDatagramConnectionSockets@ServerNetworkManager@@QEAAXXZ)
my_project.exe : fatal error LNK1120: 4 externes non résolus
That's where I'm stuck, I don't know where to go from here. I build everything from CLI, but I tried to open the CMake project in Visual Studio, and I had the same errors. I don't know how to get more information from the MSVC linker. I don't even know if the error is in my project or in the way I'm building my project's dependencies (GameNetworkingSocket, its own dependencies, and other things I'm using, all built and installed in my sdk
folder). Any help appreciated ^^