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

Author Topic: Need help building my project with CMake  (Read 3782 times)

0 Members and 1 Guest are viewing this topic.

gabrieljt

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Need help building my project with CMake
« on: January 27, 2014, 01:52:56 pm »
Hello there,

I've been reading the SFML Game Development Book, which is great by the way; just finished chapter 6.

I'm typing all the code by myself using Sublime Text because I learn more this way instead of only copying/reading the source from GitHub (but I was also able to build that code using CMake).
Thus, I am building the chapters I've rewrote using a simple Makefile.

But the number of files are increasing, and chapter 7 looks like a tough one :)
I have little experience with Makefiles and I would like to use CMake to build my projects.

I've found this tutorial https://github.com/SFML/SFML/wiki/Tutorial%3A-Build-your-SFML-project-with-CMake, but I didn't fully understand it.

SFML is installed in a custom path (/home/myusername/development/SFML-2.1).
My projects have 3 directories: ./Include (for .hpp's), ./Source (for .cpp's) and ./Media (for resources).
I include headers in my projects using #include <headerFile.hpp>, and in the Makefiles I use the arg -I./Include for compiling. (Note: I am not using the ./Include/Book directory, only ./Include).

I've created a directory /usr/share/cmake-2.8/Modules/FindSFML and pasted the FindSFML.cmake there.
I've also created the config.h.in and COPYING files in the ./Source directory of my project, and added the #include "config.h" in the Main.cpp.

When i try to use CMake-GUI, i get the following error:

"[...]By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SFML", but
  CMake did not find one.
[...]"

I am aware that I need to inform CMake where is the FindSFML directory I've created, but I don't know how :/

I also need help adapting the tutorial to my project structure, like informing the compiler about my ./Include and ./Source dirs.

Thanks for the assistance, any help and hints are appreciated.

-Gabriel
« Last Edit: January 27, 2014, 01:59:59 pm by gabrieljt »

gabrieljt

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Need help building my project with CMake
« Reply #1 on: January 27, 2014, 03:57:58 pm »
I've done a better search on the forums and found this topic http://en.sfml-dev.org/forums/index.php?topic=8356.msg56044#msg56044 which allowed me to make some progress.

This is my CMakeLists.txt:

Quote
#Change this if you need to target a specific CMake version
cmake_minimum_required(VERSION 2.6)
project(sfml-game)

list(APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -Wall -g")

# Enable debug symbols by default
if(CMAKE_BUILD_TYPE STREQUAL "")
      set(CMAKE_BUILD_TYPE Debug)
      endif()
# (you can also set it on the command line: -D CMAKE_BUILD_TYPE=Release)

# Set version information in a config.h file
set(sfml-game_VERSION_MAJOR 1)
set(sfml-game_VERSION_MINOR 0)
configure_file(
  "${PROJECT_SOURCE_DIR}/Source/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h"
      )
include_directories("${PROJECT_SOURCE_DIR}/Include/")
include_directories("${PROJECT_BINARY_DIR}")

# Define sources and executable
set(EXECUTABLE_NAME "sfml-game")
add_executable(${EXECUTABLE_NAME} Source/Main.cpp)

# Detect and add SFML
#set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-2.8/Modules/" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
      include_directories(${SFML_INCLUDE_DIR})
        target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
        endif()


# Install target
install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)


# CPack packaging
include(InstallRequiredSystemLibraries)
#set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")
set(CPACK_PACKAGE_VERSION_MAJOR "${sfml-game_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${sfml-game_VERSION_MINOR}")
include(CPack)

The
Quote
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-2.8/Modules/" ${CMAKE_MODULE_PATH})
command allows CMake to find SFML dir. I've defined an Env Var SFML_ROOT and moved the FindSFML.cmake to the Modules dir.

The
Quote
list(APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -Wall -g")
command is needed for using C++11 features.

The
Quote
include_directories("${PROJECT_SOURCE_DIR}/Include/")
allows CMake to find my header files.

The Makefile has been generated in the build dir, and the problem arises when compiling:

Quote
[100%] Building CXX object CMakeFiles/sfml-game.dir/Source/Main.cpp.o
In file included from /home/.../6_gui-lesson-cmake/Source/Main.cpp:1:0:
/home/.../build/6_gui-lesson/config.h:1:13: warning: ISO C99 requires whitespace after the macro name [enabled by default]
/home/.../build/6_gui-lesson/config.h:2:13: warning: ISO C99 requires whitespace after the macro name [enabled by default]
/home/.../build/6_gui-lesson/config.h:2:0: warning: "sfml" redefined [enabled by default]
/home/.../build/6_gui-lesson/config.h:1:0: note: this is the location of the previous definition
Linking CXX executable sfml-game
/home/.../6_gui-lesson-cmake/Source/Main.cpp:12: error: undefined reference to 'Application::Application()'
/home/.../6_gui-lesson-cmake/Source/Main.cpp:13: error: undefined reference to 'Application::run()'
collect2: error: ld returned 1 exit status
make[2]: *** [sfml-game] Error 1
make[1]: *** [CMakeFiles/sfml-game.dir/all] Error 2
make: *** [all] Error 2

I've noticed only the Main.cpp.o was created.
I believe I have to create all the object files, so the linking can be done.
How can one achieve this in the CMakeLists.txt?

Thanks again!

-Gabriel

gabrieljt

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Need help building my project with CMake
« Reply #2 on: January 27, 2014, 04:14:36 pm »
It has been done!

the following commands did the trick:

Quote
# Define sources and executable
file(GLOB sfml-game_SRC
    "Include/*.hpp"
    "Source/*.cpp"
)

set(EXECUTABLE_NAME "sfml-game")
#add_executable(${EXECUTABLE_NAME} Source/Main.cpp)
add_executable(${EXECUTABLE_NAME} ${sfml-game_SRC})

The sfml-game executable has been successfully created.
However, when i try to run it from the build directory, the Media/ folder is not there and the game fails to find the resources.
So i had to move the executable back to my source directory after the compilation in order to run it, which worked flawlessly.

Based on all this, does anyone have suggestions/hints on how to improve this build system?

Here is my current CMakeLists.txt:

Quote
#Change this if you need to target a specific CMake version
cmake_minimum_required(VERSION 2.6)
project(sfml-game)

list(APPEND CMAKE_CXX_FLAGS "-std=c++0x ${CMAKE_CXX_FLAGS} -Wall -g")

# Enable debug symbols by default
if(CMAKE_BUILD_TYPE STREQUAL "")
      set(CMAKE_BUILD_TYPE Debug)
      endif()
# (you can also set it on the command line: -D CMAKE_BUILD_TYPE=Release)

# Set version information in a config.h file
set(sfml-game_VERSION_MAJOR 1)
set(sfml-game_VERSION_MINOR 0)
configure_file(
  "${PROJECT_SOURCE_DIR}/Source/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h"
      )
include_directories("${PROJECT_SOURCE_DIR}/Include/")
include_directories("${PROJECT_BINARY_DIR}")

# Define sources and executable
file(GLOB sfml-game_SRC
    "Include/*.hpp"
    "Source/*.cpp"
)

set(EXECUTABLE_NAME "sfml-game")
#add_executable(${EXECUTABLE_NAME} Source/Main.cpp)
add_executable(${EXECUTABLE_NAME} ${sfml-game_SRC})

# Detect and add SFML
#set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake-2.8/Modules/" ${CMAKE_MODULE_PATH})
#Find any version 2.X of SFML
#See the FindSFML.cmake file for additional details and instructions
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
    include_directories(${SFML_INCLUDE_DIR})
    target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
endif()

# Install target
install(TARGETS ${EXECUTABLE_NAME} DESTINATION bin)

# CPack packaging
include(InstallRequiredSystemLibraries)
#set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")
set(CPACK_PACKAGE_VERSION_MAJOR "${sfml-game_VERSION_MAJOR}")
set(CPACK_PACKAGE_VERSION_MINOR "${sfml-game_VERSION_MINOR}")
include(CPack)

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Need help building my project with CMake
« Reply #3 on: January 27, 2014, 05:38:53 pm »
You need to add an Install() for the Media directory and make sure to call make INSTALL, when building.

You could take a look at some of my rather simple CMake scripts on GitHub. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

gabrieljt

  • Newbie
  • *
  • Posts: 18
    • View Profile
    • Email
Re: Need help building my project with CMake
« Reply #4 on: January 27, 2014, 08:54:20 pm »
Nice stuff there, congrats :)

The solution was quite simple:

Quote
file(COPY Media DESTINATION .)

But I think it works only with CMake 2.8.

Yeay, now I can build my projects using CMake o/

Thanks :D

To Chapter 7!