P
P
Prizm2021-04-28 17:50:54
CMake
Prizm, 2021-04-28 17:50:54

How to properly release sfml projects?

When I send an executable file with an sfml program to another person, his terminal gives an error, which is that dynamic libraries were not found. How can I configure the project through cmake (I use clion) so that the libraries are statically linked? I saw solutions in which people force the program to install separately sfml via the console at startup, if it is not there, and then start the program, but it seems to me that this is the wrong solution.

main.cpp
#include <SFML/Graphics.hpp>
#include <cstdint>

sf::Vector2i rotateRight(sf::Vector2i v) {
    return sf::Vector2i(v.y, -v.x);
}

sf::Vector2i rotateLeft(sf::Vector2i v) {
    return sf::Vector2i(-v.y, v.x);
}

class antField {
public:
    uint32_t W, H;
    sf::Vector2i antPos, antDir;
    uint8_t *cells;

    antField(uint32_t W_, uint32_t H_) {
        W = W_;
        H = H_;
        antPos = sf::Vector2i(W / 2, H / 2);
        antDir = sf::Vector2i(1, 0);
        cells = new uint8_t[W * H];
    }

    ~antField() {
        delete[] cells;
    }

    void step(sf::Image *img) {
        uint32_t *map = (uint32_t *) (img->getPixelsPtr());
        uint32_t i = antPos.x + W * antPos.y;
        uint8_t c = cells[i];
        bool angle = c != 2;

        map[i] = c == 0 ? 0xff0013FF :
                 c == 1 ? 0xff9C8903 :
                          0xff00EC95;
        antDir = angle ? rotateLeft(antDir) : rotateRight(antDir);
        cells[i] = (c + 1) % 3;
        antPos += antDir;
        (antPos.x += W) %= W;
        (antPos.y += H) %= H;
    }
};

int main() {
    sf::ContextSettings setting;
    setting.antialiasingLevel = 8;

    const uint16_t winW = 800, winH = 800;

    antField mainField(winW, winH);
    sf::Image img;
    img.create(winW, winH);
    sf::Texture tex;
    tex.create(winW, winH);
    sf::Sprite spr(tex);

    sf::RenderWindow window(sf::VideoMode(winW, winH), "ant", sf::Style::Default, setting);

    while (window.isOpen()) {
        sf::Event event;

        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed)
                window.close();
        }
        for (uint32_t i = 0; i < 1000; i++) {
            mainField.step(&img);
        }

        tex.loadFromImage(img);
        window.clear();
        window.draw(spr);
        window.display();
    }

    return 0;
}
CMakeList.txt
cmake_minimum_required(VERSION 3.10)
project(langtonAnt)

set(CMAKE_CXX_STANDARD 17)
set(SFML_DIR "C:/SFML-2.5.1")
find_package(SFML COMPONENTS graphics window system)
add_executable(langtonAnt main.cpp )
target_link_libraries(langtonAnt sfml-graphics sfml-audio)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
Prizm, 2021-05-02
@PrizmMARgh

1. you need to download the sfml sources from their github.
2. using cmake compile so that there are static libraries. Along the way, errors like "no library XXX" will occur - you will need to download the libXXX-dev package.
3. After compilation, you will need to add to the cmake file:
set(SFML_DIR "path to compiled sfml/SFML-compiled")
set(CMAKE_MODULE_PATH "path to compiled sfml/SFML-compiled" ${CMAKE_MODULE_PATH})
set(SFML_STATIC_LIBRARIES TRUE)

L
LoliDeveloper, 2021-04-28
@LoliDeveloper

Read the tutorials on their site for your compiler. There are separate dlls for static libraries

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question