0

I am trying to link Valves GameNetworkingSockets with my c++ cmake project, and am having errors (first failed to compile due to missing defines when I was able to include the headers, later errors were due to unrecognized targets...). I am using the CPM module for package installing. Below is my cmake:

# Basic settings

cmake_minimum_required (VERSION 3.14)
project (Valhalla)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)
include(get_cpm)

if (NOT CMAKE_BUILD_TYPE)
    SET(CMAKE_BUILD_TYPE Release)
endif()

############
# LIBRARIES
############



CPMAddPackage(
    NAME gamenetworkingsockets
    GITHUB_REPOSITORY ValveSoftware/GameNetworkingSockets
    #VERSION 3.11.3
    GIT_TAG v1.4.1
    DOWNLOAD_ONLY TRUE
    OPTIONS
    CONFIG REQUIRED
    "USE_STEAMWEBRTC ON"
    "Protobuf_USE_STATIC_LIBS ON"
)

##add_library(lua STATIC ${Lua_sources})
#add_library(steamnet STATIC ${gamenetworkingsockets_sources})
add_library(gamenetworkingsockets INTERFACE)
target_compile_definitions(gamenetworkingsockets INTERFACE STEAMNETWORKINGSOCKETS_STATIC_LINK)
target_include_directories(gamenetworkingsockets SYSTEM INTERFACE ${gamenetworkingsockets_SOURCE_DIR}/include)
#target_include_directories(gamenetworkingsockets PUBLIC ${gamenetworkingsockets_SOURCE_DIR}/include)
#target_include_directories(gamenetworkingsockets PUBLIC ${gamenetworkingsockets_BINARY_DIR})
#target_compile_definitions(gamenetworkingsockets PUBLIC STEAMNETWORKINGSOCKETS_STATIC_LINK)
#target_include_directories(gamenetworkingsockets SYSTEM INTERFACE ${gamenetworkingsockets_SOURCE_DIR}/include)
#target_include_directories(gamenetworkingsockets SYSTEM INTERFACE ${gamenetworkingsockets_BINARY_DIR}/include)

#target_include_directories(gamenetworkingsockets PRIVATE ${gamenetworkingsockets_INCLUDE_DIR})



#
# Linking
#

add_executable(Valhalla
    "src/Main.cpp"
)



target_include_directories(Valhalla PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include)

target_compile_features(Valhalla PRIVATE cxx_std_20)

target_link_libraries(Valhalla
    #gamenetworkingsockets
    #GameNetworkingSockets::shared
    #steamnet::steamnet_s
    #GameNetworkingSockets::GameNetworkingSockets_s
    #GameNetworkingSockets::static
    #GameNetworkingSockets::GameNetworkingSockets_s

)

set_target_properties(Valhalla PROPERTIES
    VS_DEBUGGER_WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/run/
    CXX_EXTENSIONS OFF
    CXX_STANDARD_REQUIRED ON
)



if (MSVC)
    target_compile_options(Valhalla PRIVATE /bigobj "/diagnostics:caret")
endif()
if(MSVC AND CMAKE_BUILD_TYPE MATCHES Release)
    set_target_properties(Valhalla PROPERTIES WIN32_EXECUTABLE ON)
endif()

The Cmake above has been stripped to a mostly minimal portion for reproducibility. As you can see, there are many commented-out commands as I have tried to brute force ways of making this work to no avail. I found this Linking with GameNetworkingSockets library on Windows gives "unresolved external" error which doesn't help.

#include <steam/steamnetworkingsockets.h>

int main(int argc, char **argv) {
    SteamNetworkingIdentity identity;
    
    identity.ParseString("str:peer-server");

    return 0;
}

I have no clue how to interface vcpkg with Cmake from MSVC, and I really do not know how to fix this. Any clues as to how I can get everything to link and eventually compile correctly?

crazicrafter1
  • 309
  • 5
  • 18

1 Answers1

0

So I found out how to use vcpkg.

Install vcpkg

git clone https://github.com/microsoft/vcpkg
.\vcpkg\bootstrap-vcpkg.bat

Update it with

git pull

Make libraries installed with vcpkg recognizable by msvc

.\vcpkg\vcpkg integrate install

Install GameNetworkingSockets and required Protobuf with

.\vcpkg\vcpkg install GameNetworkingSockets --triplet=x64-windows
.\vcpkg\vcpkg install protobuf --triplet=x64-windows

cmakelists.txt in msvc

# Project
cmake_minimum_required (VERSION 3.14)
project (MyProject)

# Libraries
find_package(protobuf CONFIG REQUIRED)
find_package(GameNetworkingSockets CONFIG REQUIRED)

# Linking
add_executable(${PROJECT_NAME}
    "src/Main.cpp"
)

target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_20)
    
target_link_libraries(${PROJECT_NAME}
    PRIVATE GameNetworkingSockets::static GameNetworkingSockets::GameNetworkingSockets
    PRIVATE protobuf::libprotobuf
)

Try this simple script

// main.cpp
#include <steam/steamnetworkingsockets.h>

int main(void) {
    SteamNetworkingIdentity identity;
    
    identity.ParseString("str:peer-server");

    return 0;
}

The above should compile fine if everything worked

crazicrafter1
  • 309
  • 5
  • 18