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

Author Topic: Compiling example with cmake  (Read 7808 times)

0 Members and 1 Guest are viewing this topic.

Maxjen

  • Newbie
  • *
  • Posts: 7
    • View Profile
Compiling example with cmake
« on: June 25, 2012, 03:05:15 pm »
Hi,

I want to start a project with sfml and it should be as cross-plattform as possible, so I want to use git as version control and cmake as my buildsystem. And I want to bundle sfml with my application so I don't have to install it everytime. Right now I'm just trying to compile the window-example. My folder tree looks like this:

window
   |_ build
   |_ SFML
      |_ SFML-2.0-rc_linux
      |_ SFML-2.0-rc_windows
   |_ CMakeLists.txt
   |_ main.cpp

My CMakeLists.txt looks like this:
cmake_minimum_required (VERSION 2.6)
project (sfml-test)

if(UNIX)
  include_directories ("${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_linux/include")
  set (EXTRA_LIBS ${EXTRA_LIBS} "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_linux/lib/libsfml-system.so")
  set (EXTRA_LIBS ${EXTRA_LIBS} "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_linux/lib/libsfml-window.so")
  set (EXTRA_LIBS ${EXTRA_LIBS} GL GLU)
endif(UNIX)

if(WIN32)
  include_directories ("${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_windows/include")
  set (EXTRA_LIBS ${EXTRA_LIBS} "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_windows/lib/sfml-system.lib")
  set (EXTRA_LIBS ${EXTRA_LIBS} "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_windows/lib/sfml-window.lib")
  set (EXTRA_LIBS ${EXTRA_LIBS} Opengl32 glu32)
endif(WIN32)

add_executable (sfml-test main.cpp)
target_link_libraries (sfml-test  ${EXTRA_LIBS})

On linux I use KDevelop and everything works perfectly. On windows I first generate project files for Visual Studio 2010 and then I can compile it, but before I run it I have to copy sfml-system-2.dll and sfml-window-2.dll into build/Debug or build/Release. What I don't understand is why I only need the .so files on linux, but need both the .lib and the .dll on windows. And does anyone know how I can avoid having to copy the .dlls everytime? I have another problem with codeblocks on windows, but this one is more important right now.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Compiling example with cmake
« Reply #1 on: June 25, 2012, 03:39:24 pm »
Quote
What I don't understand is why I only need the .so files on linux, but need both the .lib and the .dll on windows.
There are plenty of explanations about this on the internet.
In short: the .lib is the import library, the file that the linker will read when linking your program; the .dll is the runtime library, the file that will be loaded by the OS when running your program.
On Linux, the .so file handles both roles.
What's important is that you need the .lib file only when you compile, and the .dll file only when you execute (so you don't have to distribute .lib files).

Quote
And does anyone know how I can avoid having to copy the .dlls everytime?
Why would you copy them everytime? They never change.
And if you don't want DLLs at all, link statically.

By the way, you should use the FindSFML.cmake script to abstract the way SFML is found and used by CMake.

Wait... why is this post in the C forum??
Laurent Gomila - SFML developer

Maxjen

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compiling example with cmake
« Reply #2 on: June 26, 2012, 01:45:50 am »
Quote
What's important is that you need the .lib file only when you compile, and the .dll file only when you execute (so you don't have to distribute .lib files).
Ok, that's good to know, thanks!

Quote
Why would you copy them everytime? They never change.
Right, but it's still one extra step and it would be nice it could be done automatically by cmake somehow. But ok, it's not really that important.

Quote
And if you don't want DLLs at all, link statically.
I might do that, thanks.

Quote
Wait... why is this post in the C forum??
I thought this was the right forum, because my problems were more related to the programming language and not directly to sfml, but I just noticed that this forum is not C/C++ but only C, sorry. Maybe this can be moved.

Quote
By the way, you should use the FindSFML.cmake script to abstract the way SFML is found and used by CMake.
Good idea, I thought that required SFML to be installed on the sytem, but it seems that is not the case. I updated my CMakeLists.txt and it works, thanks! I'll post later about my other problem.

My CMakeLists.txt now looks like this:
cmake_minimum_required (VERSION 2.6)
project (sfml-test)

set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")
include_directories ("${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_linux/include")

if (UNIX)
  set (SFML_DIR "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_linux/")
endif (UNIX)
if (WIN32)
  set (SFML_DIR "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_windows/")
endif (WIN32)

find_package (SFML 2 COMPONENTS system window)
find_package (OpenGL)
include_directories (${SFML_INCLUDE_DIR})
include_directories (${OPENGL_INCLUDE_DIR})

add_executable (sfml-test main.cpp)
target_link_libraries (sfml-test ${OPENGL_LIBRARIES} ${SFML_LIBRARIES})

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Compiling example with cmake
« Reply #3 on: June 26, 2012, 08:20:32 am »
Quote
Right, but it's still one extra step and it would be nice it could be done automatically by cmake somehow. But ok, it's not really that important.
You can tell CMake to add pre- or post-build steps that copy files automatically.

Quote
I thought this was the right forum, because my problems were more related to the programming language and not directly to sfml, but I just noticed that this forum is not C/C++ but only C, sorry. Maybe this can be moved.
Yeah :P

Quote
include_directories ("${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_linux/include")
...
include_directories (${SFML_INCLUDE_DIR})
Isn't it redundant?
Laurent Gomila - SFML developer

Maxjen

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compiling example with cmake
« Reply #4 on: June 26, 2012, 12:22:50 pm »
Quote
Isn't it redundant?
Ah, forgot to remove that.

My next problem is, that I can't compile it with Codeblocks. I generated the project files with the "CodeBlocks - MinGW Makefiles" generator, but when I try to compile it I get lots of undefined reference errors.

Part of my error message:
-------------- Build: all in sfml-test ---------------

Using makefile: Makefile
Linking CXX executable sfml-test.exe
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x93): undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xcd): undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKSsjRKNS_15ContextSettingsE'
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xfb): undefined reference to `_imp___ZN2sf6Window17setFramerateLimitEj'
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x108): undefined reference to `_imp___ZN2sf5ClockC1Ev'
 

I think my problem might be similiar to this one: http://en.sfml-dev.org/forums/index.php?topic=4010.0
You said there that you have to use the same compiler for sfml and your application. I downloaded the precompiled package. Does this mean I can't use sfml with codeblocks unless I compile sfml myself? If that is the case, isn't that extremly impractical? What if for example I wanted to use a proprietary library where I don't have the source code?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Compiling example with cmake
« Reply #5 on: June 26, 2012, 12:39:56 pm »
Don't worry, it looks like a simple mistake, not a compatibility problem.

You should try "window system" instead of "system window" -- as said in the tutorial, the order is important for gcc.
Laurent Gomila - SFML developer

Maxjen

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compiling example with cmake
« Reply #6 on: June 26, 2012, 01:29:56 pm »
Unfortunately  I still get the same error:
-------------- Build: all in sfml-test ---------------

Using makefile: Makefile
Scanning dependencies of target sfml-test
[100%] Building CXX object CMakeFiles/sfml-test.dir/main.cpp.obj
Linking CXX executable sfml-test.exe
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x93): undefined reference to `_imp___ZN2sf9VideoModeC1Ejjj'
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xcd): undefined reference to `_imp___ZN2sf6WindowC1ENS_9VideoModeERKSsjRKNS_15ContextSettingsE'
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0xfb): undefined reference to `_imp___ZN2sf6Window17setFramerateLimitEj'
CMakeFiles\sfml-test.dir/objects.a(main.cpp.obj):main.cpp:(.text+0x108): undefined reference to `_imp___ZN2sf5ClockC1Ev'
...

This is my current CMakeLists.txt
cmake_minimum_required (VERSION 2.6)
project (sfml-test)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake/Modules/")

if (UNIX)
  set (SFMLDIR "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_linux/")
endif (UNIX)
if (WIN32)
  set (SFMLDIR "${PROJECT_SOURCE_DIR}/SFML/SFML-2.0-rc_windows/")
endif (WIN32)

find_package (SFML 2 COMPONENTS window system)
find_package (OpenGL)
include_directories (${SFML_INCLUDE_DIR})
include_directories (${OPENGL_INCLUDE_DIR})

add_executable (sfml-test main.cpp)
target_link_libraries (sfml-test ${OPENGL_LIBRARIES} ${SFML_LIBRARIES})

In case it matters: I had to change the make program from "make.exe" to "mingw32-make.exe" so that it even starts compiling, but other than that I didn't change any settings.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Compiling example with cmake
« Reply #7 on: June 26, 2012, 01:35:14 pm »
Can you upload the generated Code::Blocks project file?
Laurent Gomila - SFML developer

Maxjen

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compiling example with cmake
« Reply #8 on: June 26, 2012, 01:56:01 pm »
Ok, it works now. I downloaded the SFML package for GCC SJLJ. Before I was using the same as for Visual Studio. But now I have the folders:
SFML-2.0-rc_linux
SFML-2.0-rc_windows_visualStudio
SFML-2.0-rc_windows_codeblocks

It's becoming a little bit cluttered and I have to rename one of them to SFML-2.0-rc_windows if I want to use codeblocks or visual studio. Is it not possible to somehow have only one folder for windows regardless of which compiler you use? Or at least to avoid the renaming?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Compiling example with cmake
« Reply #9 on: June 26, 2012, 02:04:39 pm »
The "environment" inludes the OS of course, but also the compiler, and also the version of the compiler. Therefore you can't just have Linux/Windows. Look at all the different SFML archives that exist for a single version.

To me, the solution is not to distribute SFML in precompiled form. It will become unmaintainable for you: look at all the versions that exist, and it's not even enough (there are problems with GCC 4.7, which will probably require an additional precompiled version). Users that want to compile your source code must take care of downloading the version of SFML that matches their environment.

Another solution is to distribute the sources of SFML, and compile it when your project is compiled. This way you always get the right compiled version of SFML.
Laurent Gomila - SFML developer

Maxjen

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compiling example with cmake
« Reply #10 on: June 26, 2012, 03:17:52 pm »
Thanks for your replies, they help a lot! I think I'll just leave it to the user then to download the right version of SFML. Just for understanding, say there is a closed-source library which only provides .lib files would this mean that I simply can't use it with gcc? So on windows you depend on library developers to support your compiler if it is not open-source.

And there is one more thing that still confuses me. On this tutorial you say that SFML provides its dependancies on windows. How is that possible if the libraries need to be compiled with the right compiler?

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: Compiling example with cmake
« Reply #11 on: June 26, 2012, 03:48:17 pm »
Quote
Just for understanding, say there is a closed-source library which only provides .lib files would this mean that I simply can't use it with gcc?
Absolutely. Each compiler has its own ABI (application binary interface) and its own format of import libraries.

Quote
So on windows you depend on library developers to support your compiler if it is not open-source.
Yes.

Quote
And there is one more thing that still confuses me. On this tutorial you say that SFML provides its dependancies on windows. How is that possible if the libraries need to be compiled with the right compiler?
It's much easier: all the dependencies are C libraries. Since C has a standard ABI, C libraries can be used with any other compiler than the one they were compiled with.
Laurent Gomila - SFML developer

Maxjen

  • Newbie
  • *
  • Posts: 7
    • View Profile
Re: Compiling example with cmake
« Reply #12 on: June 27, 2012, 11:10:46 am »
Ok, thanks. My curiosity is satisfied for now.

AndrewChan

  • Newbie
  • *
  • Posts: 1
    • View Profile
    • Email
Re: Compiling example with cmake
« Reply #13 on: October 13, 2023, 09:27:11 am »
Here is my copying dll

if (WIN32)
   file(GLOB sfml_dlls ${SFML_DIR}/../../../bin/*.dll)
   foreach( file_i  ${sfml_dlls})
      add_custom_command(TARGET ${PROJECT_NAME} POST_BUILD
         COMMAND ${CMAKE_COMMAND} -E copy_if_different
         ${file_i}
         $<TARGET_FILE_DIR:${PROJECT_NAME}>
      )
   endforeach( file_i )
endif(WIN32)
 
« Last Edit: October 13, 2023, 09:28:56 am by AndrewChan »