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

Author Topic: Creating C++ SFML projects using CMake and Make, Cross Compiling  (Read 3035 times)

0 Members and 1 Guest are viewing this topic.

reportados123

  • Newbie
  • *
  • Posts: 18
    • View Profile
Hi, I'm new to CMake and Make and want to cross compile between Linux and Windows all in one CMakeLists.txt.

I've looked up online on a few tutorials, got some basics down but there's only one problem, on my windows computer I don't have environment path variables setup and that MinGW is on a USB drive. This is how my current CMakeLists.txt looks like.

cmake_minimum_required(VERSION 2.8)

set(PROJECTNAME "Pong")
project(${PROJECTNAME})

aux_source_directory(src/ Sources)
add_executable(bin/${PROJECTNAME} ${Sources})
target_link_libraries(bin/${PROJECTNAME} sfml-graphics sfml-window sfml-system sfml-audio)
include_directories(D:/SFML/include)

So basically what happens is, is that I have to specify my C and CPP compiler which I've already done by doing "cmake -G "MinGW Makefiles" -DCMAKE_C_COMPILER=D:/MinGW/bin/gcc.exe -DCMAKE_CXX_COMPILER=D:/MinGW/bin/g++.exe" But now I have to somehow specify the lib folder too, which I don't know how. I looked at add_library but it was confusing.

Also, this is geared towards the Windows platform. How can I make it so that it has an if statement and sees whether the system is Linux or Windows?

Thanks.

TL:DR: How do you add SFML lib folder to CMakeLists.txt file? How do you check whether the system is Linux or Windows?

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: Creating C++ SFML projects using CMake and Make, Cross Compiling
« Reply #1 on: January 18, 2013, 04:40:00 am »
I'm not an expert on CMake scripts and it can sometimes be quite complex... Whenever I need to do something, I take a look at existing projects that use CMake, especially Thor and/or SFGUI (notice the examples directory for binary building).

For CMake variables you should take a look at the CMake documentation, I know it's freaking big, but it contains all one would want to know. ;)
Specific OS variables can be found here.

For MinGW the advised way is to temporarily change the environment variable - I'm not sure if there's another way. Keep in mind that you don't have to actually change the PATH variable on the system, but you just need to run a small batch file that adds the MinGW root directory to PATH for the current command line session.
Then you'll have to call everything that needs information about the compiler via that command line with the changed PATH.
In short:
  • Create a new batch file containing: @set "PATH=P:\ath\to\MinGW\bin;%PATH%"
  • Start a new cmd process.
  • Call the created batch file.
  • Call cmake with the wanted settings (you can also call cmake-gui).
  • Call mingw32-make install in the correct directory.
  • Done!

I hope that helps! :)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

reportados123

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Creating C++ SFML projects using CMake and Make, Cross Compiling
« Reply #2 on: January 18, 2013, 01:29:45 pm »
Hi, thanks, MinGW works but I'm still getting errors saying SFML/Graphics.hpp no such file or directory.

Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
Re: Creating C++ SFML projects using CMake and Make, Cross Compiling
« Reply #3 on: January 18, 2013, 01:38:51 pm »
I think the include_directories() has to be put before add_executable(), at least that's what I've been doing and it worked.
You also probably want to use find_package() to search for SFML in a better way.

As I already said, you should probably take a look at existing projects. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

reportados123

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Creating C++ SFML projects using CMake and Make, Cross Compiling
« Reply #4 on: January 18, 2013, 09:32:59 pm »
SFML/Graphics.hpp error is gone but I'm getting cannot find "-lsfml-graphics", etc because I didn't include the SFML/lib folder. I tried using include_directories but it doesn't work. I've tried using find_package but I needed sfml.cmake and stuff.

Is there any way to include the lib folder? I've looked at existing projects using cmake but they were all very confusing. All I need right now is how to include the lib folder.

Thanks.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11008
    • View Profile
    • development blog
    • Email
AW: Creating C++ SFML projects using CMake and Make, Cross Compiling
« Reply #5 on: January 18, 2013, 10:09:50 pm »
The correct way is to use find_package(). The FindSFML.cmake script comes with SFML itself: SFML/cmake/Modules
You can either copy the script to your CMake shared dir with the scripts for better known libs, or simply set CMAKE_MODULES_PATH (or similar).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

reportados123

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Creating C++ SFML projects using CMake and Make, Cross Compiling
« Reply #6 on: January 19, 2013, 01:49:18 am »
Thanks, I got it working now.