Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Compile project with cmake on windows  (Read 5079 times)

0 Members and 1 Guest are viewing this topic.

Souren

  • Newbie
  • *
  • Posts: 7
    • View Profile
Compile project with cmake on windows
« on: April 22, 2020, 09:45:48 am »
Hello,

I am able to compile my SFML project on mac and windows with cmake but I'm having a lot of trouble on windows.

On mac I just follow the instructions on this page: https://www.sfml-dev.org/tutorials/2.5/start-osx.php#installing-sfml and then run:
cmake -S . -B build
cmake --build build -- -j
 

And everything goes smoothly.

On windows I run
λ cmake -DSFML_DIR="C:/Users/souren/Documents/SFML-2.5.1/lib/cmake/SFML" -DBOOST_ROOT="C:\Program Files\boost_1_55_0"-S . -B build
-- Building for: Visual Studio 16 2019
-- Selecting Windows SDK version 10.0.18362.0 to target Windows 10.0.18363.
-- The CXX compiler identification is MSVC 19.24.28316.0
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.24.28314/bin/Hostx64/x64/cl.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SFML 2.5.1 in C:/Users/souren/Documents/SFML-2.5.1/lib/cmake/SFML
-- Found Boost: C:/Program Files/boost_1_55_0 (found suitable version "1.55.0", minimum required is "1.55")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/souren/Documents/code/ungroup_game/build
cmake --build build
 

And I get a ton of "fatal error C1083: Cannot open include file: 'SFML/Network.hpp'" errors.

Why can't my files include SFML? Did I do something wrong when linking?

CMakeLists.txt
cmake_minimum_required(VERSION 3.11..14)

# set the project name, version and language:w
project(Ungroup VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# find sfml
find_package(SFML 2.5 COMPONENTS audio graphics window system REQUIRED)

# find boost
find_package(Boost 1.55 COMPONENTS REQUIRED)

add_subdirectory(src)
add_subdirectory(extern/catch)
add_subdirectory(extern/cxxopts)
add_subdirectory(tests)
 

src/client/CMakeLists.txt
Code: [Select]
target_link_libraries(ug-client PRIVATE client-lib common-lib cxxopts)

You can see the code in more detail here: https://github.com/SourenP/ungroup_game/tree/c7614a15f1ca38b7a09ce2e24e86d843e68ea6f2

Additional Details:
- I have a 64bit machine running windows 10
- I'm using the sfml download "GCC 7.3.0 MinGW (SEH) - 64-bit"

Souren

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compile project with cmake on windows
« Reply #1 on: April 22, 2020, 09:48:15 am »
Any chance the issue is caused by using the download instead of compiling SFML on my machine and using that?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Re: Compile project with cmake on windows
« Reply #2 on: April 22, 2020, 10:03:24 am »
You should have posted the most relevant part of your CMake files:

target_link_libraries(common-lib PUBLIC sfml-system sfml-graphics sfml-network Boost::boost)

What if you link SFML directly to your final projects (client and server)? This should not be required, but it can help to figure out what happens.

You should also include the "network" component in your find_package call. And the link order should be reversed (sfml-system last).

Quote
I'm using the sfml download "GCC 7.3.0 MinGW (SEH) - 64-bit"
-- Building for: Visual Studio 16 2019
There's definitely something wrong here... ;)

Also note that -std=c++11 is redundant with set(CMAKE_CXX_STANDARD 11), and -pthreads can be handled in a generic way with CMake's find_package(Threads).
« Last Edit: April 22, 2020, 10:07:50 am by Laurent »
Laurent Gomila - SFML developer

Souren

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compile project with cmake on windows
« Reply #3 on: April 22, 2020, 10:06:26 pm »
Thanks for the quick response, I really appreciate it!  :)

Good point about the
Code: [Select]
-- Building for: Visual Studio 16 2019
I switched to using MinGW and downloaded the 32bit SFML library and made some progress by running
Code: [Select]
λ cmake -DSFML_DIR="C:/Users/souren/Documents/SFML-2.5.1_mingw_32/lib/cmake/SFML" -DBOOST_ROOT="C:\Program Files\boost_1_55_0" -G "MinGW Makefiles" -S . -B build
-- The CXX compiler identification is GNU 7.3.0
-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/bin/g++.exe
-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/bin/g++.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SFML 2.5.1 in C:/Users/souren/Documents/SFML-2.5.1_mingw_32/lib/cmake/SFML
-- Found Boost: C:/Program Files/boost_1_55_0 (found suitable version "1.55.0", minimum required is "1.55")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/souren/Documents/code/ungroup_game/build

The common lib build just fine but then the client and server projects dont :(
Code: [Select]
λ cmake --build build
Scanning dependencies of target common-lib
[  1%] Building CXX object src/common/CMakeFiles/common-lib.dir/util/network_util.cpp.obj
[  2%] Building CXX object src/common/CMakeFiles/common-lib.dir/util/StateDef.cpp.obj
[  4%] Building CXX object src/common/CMakeFiles/common-lib.dir/util/InputDef.cpp.obj
[  5%] Building CXX object src/common/CMakeFiles/common-lib.dir/util/InputUtil.cpp.obj
[  7%] Building CXX object src/common/CMakeFiles/common-lib.dir/util/game_settings.cpp.obj
[  8%] Building CXX object src/common/CMakeFiles/common-lib.dir/objects/CircleGameObject.cpp.obj
[ 10%] Building CXX object src/common/CMakeFiles/common-lib.dir/objects/Player.cpp.obj
[ 11%] Building CXX object src/common/CMakeFiles/common-lib.dir/objects/Mine.cpp.obj
[ 13%] Building CXX object src/common/CMakeFiles/common-lib.dir/objects/CircleRigidBody.cpp.obj
[ 14%] Building CXX object src/common/CMakeFiles/common-lib.dir/objects/Group.cpp.obj
[ 16%] Building CXX object src/common/CMakeFiles/common-lib.dir/events/EventController.cpp.obj
[ 17%] Building CXX object src/common/CMakeFiles/common-lib.dir/physics/PhysicsController.cpp.obj
[ 19%] Building CXX object src/common/CMakeFiles/common-lib.dir/physics/CollisionUtil.cpp.obj
[ 20%] Building CXX object src/common/CMakeFiles/common-lib.dir/physics/VectorUtil.cpp.obj
[ 22%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/GameObjectStore.cpp.obj
[ 23%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/GroupController.cpp.obj
[ 25%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/PlayerController.cpp.obj
[ 26%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/MineController.cpp.obj
[ 27%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/GameObjectController.cpp.obj
[ 29%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/GameController.cpp.obj
[ 30%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/ResourceController.cpp.obj
[ 32%] Building CXX object src/common/CMakeFiles/common-lib.dir/systems/LevelController.cpp.obj
[ 33%] Building CXX object src/common/CMakeFiles/common-lib.dir/factories/IdFactory.cpp.obj
[ 35%] Building CXX object src/common/CMakeFiles/common-lib.dir/bots/Bot.cpp.obj
[ 36%] Building CXX object src/common/CMakeFiles/common-lib.dir/metrics/TemporalMetric.cpp.obj
[ 38%] Building CXX object src/common/CMakeFiles/common-lib.dir/models/WinCondition.cpp.obj
[ 39%] Linking CXX static library libcommon-lib.a
[ 39%] Built target common-lib
Scanning dependencies of target client-lib
[ 41%] Building CXX object src/client/CMakeFiles/client-lib.dir/systems/NetworkingClient.cpp.obj
In file included from C:\Users\souren\Documents\code\ungroup_game\src\client\systems\NetworkingClient.cpp:1:0:
C:\Users\souren\Documents\code\ungroup_game\src\client\systems\NetworkingClient.hpp:14:10: fatal error: SFML/Network.hpp: No such file or directory
 #include <SFML/Network.hpp>
          ^~~~~~~~~~~~~~~~~~
compilation terminated.
mingw32-make.exe[2]: *** [src\client\CMakeFiles\client-lib.dir\build.make:79: src/client/CMakeFiles/client-lib.dir/systems/NetworkingClient.cpp.obj] Error 1
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:292: src/client/CMakeFiles/client-lib.dir/all] Error 2
mingw32-make.exe: *** [Makefile:100: all] Error 2

I find this weird because I include SFML/Network in the common lib's files too but that compiles just fine.
Not sure whats causing the issue to happen when I'm trying to build executables for server and client.

Here is a list of all my CMakeLists.txts (I tried including sfml directly in client and server)

CmakeLists.txt
Code: [Select]
cmake_minimum_required(VERSION 3.11..14)

# set the project name, version and language:w
project(Ungroup VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# find sfml
find_package(SFML 2.5.1 COMPONENTS system window graphics network audio REQUIRED)

# find boost
find_package(Boost 1.55 COMPONENTS REQUIRED)

add_subdirectory(src)
add_subdirectory(extern/catch)
add_subdirectory(extern/cxxopts)
add_subdirectory(tests)

src/CmakeLists.txt
Code: [Select]
add_subdirectory(common)
add_subdirectory(client)
add_subdirectory(server)

src/client/CmakeLists.txt
Code: [Select]
add_executable (ug-client
    main.cpp
)

add_library(client-lib
    systems/NetworkingClient.cpp
    [...]
)

target_link_libraries(ug-client PUBLIC sfml-window sfml-graphics sfml-network sfml-systems client-lib common-lib cxxopts)

src/server/CmakeLists.txt
Code: [Select]
add_executable (ug-server
    main.cpp
)

add_library(server-lib
    systems/NetworkingServer.cpp
    systems/ServerGameController.cpp
)

target_link_libraries(ug-server server-lib common-lib sfml-window sfml-graphic sfml-network cxxopts)

Souren

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compile project with cmake on windows
« Reply #4 on: April 23, 2020, 01:59:17 am »

Souren

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compile project with cmake on windows
« Reply #5 on: April 23, 2020, 04:27:26 am »
I made a little test file to see if I could link the SFML stuff directly, instead of the two tier system in my previous post that makes a common-lib and then creates the executables client and server with it.
Everything compiled and ran smoothly, so the issue might be with not linking sfml properly in my other project... still not sure how to fix it but it's a good lead.

Here are the details of the test env:

main.cpp
cmake_minimum_required(VERSION 3.11..14)

# set the project name, version and language:w
project(Ungroup VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# find sfml
find_package(SFML 2.5.1 COMPONENTS system window graphics network audio REQUIRED)

# find boost
find_package(Boost 1.55 COMPONENTS REQUIRED)

add_executable (main
    main.cpp
)

target_link_libraries(main PUBLIC sfml-window sfml-graphics sfml-network sfml-system)
 

CmakeLists.txt
cmake_minimum_required(VERSION 3.11..14)

# set the project name, version and language:w
project(Ungroup VERSION 1.0 LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD_REQUIRED True)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11 -pthread")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -Wall -g -fsanitize=address -fno-omit-frame-pointer")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set (CMAKE_MODULE_PATH "${CMAKE_MODULE_PATH};${CMAKE_CURRENT_SOURCE_DIR}/cmake")

# find sfml
find_package(SFML 2.5.1 COMPONENTS system window graphics network audio REQUIRED)

add_executable (main
    main.cpp
)

target_link_libraries(main PUBLIC sfml-window sfml-graphics sfml-network sfml-system)
 

Build & Run
λ  cmake -DSFML_DIR="C:/Users/souren/Documents/SFML-2.5.1_mingw_32/lib/cmake/SFML" -G "MinGW Makefiles" -S . -B build\
-- The CXX compiler identification is GNU 7.3.0
-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/bin/g++.exe
-- Check for working CXX compiler: C:/Program Files (x86)/mingw-w64/i686-7.3.0-posix-dwarf-rt_v5-rev0/mingw32/bin/g++.exe - works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SFML 2.5.1 in C:/Users/souren/Documents/SFML-2.5.1_mingw_32/lib/cmake/SFML
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/souren/Documents/code/sfml_test_network_link/build

λ cmake --build build
Scanning dependencies of target main
[ 50%] Building CXX object CMakeFiles/main.dir/main.cpp.obj
[100%] Linking CXX executable main.exe
[100%] Built target main

λ .\build\main.exe
Hello SFML world
 

Souren

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compile project with cmake on windows
« Reply #6 on: April 23, 2020, 08:38:34 am »
I was able to get this working by linking sfml to the client and server libraries before, in turn, linking them to the executables

src/client/CMakeLists.txt
- target_link_libraries(ug-client PRIVATE client-lib common-lib cxxopts)
+ target_link_libraries(client-lib common-lib sfml-window sfml-graphics sfml-network sfml-system cxxopts)
+ target_link_libraries(ug-client client-lib)
 

Thanks again Laurent :)

Finally got it to build on windows

 

anything