• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

C++ How to compile sfml project using cmake on linux

lorli

Coder
How can I compile my sfml project using cmake? Using simple g++ command I would do this
Bash:
g++ -o test file1.cpp file2.cpp -lsfml-graphics -lsfml-window -lsfml-system
so far I have this in my cmake file:
Code:
cmake_minimum_required(VERSION 3.25.2)

project(life)

set (SOURCES file1.cpp file2.cpp)

add_executable(${PROJECT_NAME} ${SOURCES})
I installed sfml using
Bash:
sudo pacman -S sfml
I compiled it using cmake before, but I forgot how and my projects was deleted by accident. How can I do it?
 
Solution
I did it. If someone need solution you just have to add
Code:
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-window sfml-system)
after you use
Code:
add_executable
. Very easy but for some reason I couldn't find the solution yesterday

lorli

Coder
Hello, @lorli.

So from what I understand, you want to compile SFML for use in your projects, yeah? I don't use SFML, but there is documentation here on the SFML website that should be of use to you. Take your time whilst reading through and make sure to follow all of the steps.

Let me know if that's what you needed.
Well no. I'm not really good at knowing what is happening at compile time, but I know that when I'm linking SFML libraries I have to use
Code:
-lsfml-graphics -lsfml-window -lsfml-system
when using g++ command. And I want to create makefile using cmake that will do it for me.
 

BorkedSystem32

King Coder
Well no. I'm not really good at knowing what is happening at compile time, but I know that when I'm linking SFML libraries I have to use
Code:
-lsfml-graphics -lsfml-window -lsfml-system
when using g++ command. And I want to create makefile using cmake that will do it for me.
Ah, well I'm sorry but I don't use cmake so I unfortunately cannot help you there. Keep looking through the link I gave you though - there might be something useful in there.

Best of luck!
 

cbreemer

King Coder
I used to use the standard Unix make (not cmake) but it's probably been 20 years if not more.
At a minimum, your cmake file needs to have the compile and/or link commands to get from source to executable, as shown here in an old makefile of mine:

Makefile:
dyncall:    dyncall.o
    cc -Wl,+s,+k -o dyncall dyncall.o  -lpthread -ldld -lsql \
        -Wl,-u,sqlxpp -Wl,+ee,sqlxpp \
        -Wl,-u,sqlx -Wl,+ee,sqlx
        
dyncall.o:    dyncall.c
    cc -Ae +Z +DAportable -c dyncall.c

But how that would look for SFML I have no idea. There could be many components with many different commands to build them. In my experience, most packages you download in source format come with a makefile, compile script, or similar.
 

lorli

Coder
I did it. If someone need solution you just have to add
Code:
target_link_libraries(${PROJECT_NAME} sfml-graphics sfml-window sfml-system)
after you use
Code:
add_executable
. Very easy but for some reason I couldn't find the solution yesterday
 
Solution
Top