jonsno
jonsno

Reputation: 299

Compiling SFML on linux (ubuntu)

In compiling a SFML app,

I was successfully able to compile and run my first app, but can't we shorten this process, I mean do I have to type out this every time I compile, and make application?

I have this question also about compiling c++ files in general. Every time I have to write g++ filename.cpp -o filename. How can I shorten this process? Thank you.

Upvotes: 3

Views: 7125

Answers (4)

Galik
Galik

Reputation: 48605

It is very common to use a Makefile on Linux. The Makefile is simpler if you name your main source file the same as you want your finished program to be called.

So if you rename your main.cpp file to sfml-app.cpp and then create a file called Makefile and copy this text into it:

# optional flags (if the compiler supports it)
CXXFLAGS += -std=c++11

# HIGHLY RECOMMENDED flags
CXXFLAGS += -Wall -Wextra -pedantic-errors

# required for SFML programs
LDLIBS := $(shell pkg-config sfml-all --libs)

# The rest will turn any source file ending in .cpp
# into a program of the same name

SOURCES := $(wildcard *.cpp)
PROGRAMS := $(patsubst %.cpp,%,$(SOURCES))

all: $(PROGRAMS)

clean:
    $(RM) $(PROGRAMS)

Type: make to build the programs and make clean to remove them.

Note: The indentation of the $(RM) $(PROGRAMS) command must be a TAB, not spaces.

If you want to compile another program in the same directory simply create another source file in the directory another-app.cpp and make will automatically turn it into a program.

This Makefile will turn any source file (ending in .cpp) into a program of the same name.

Note: When you want to build larger, multi-file programs you will need a different Makefile. If you are serious about programming then you should learn make.

Here you can learn all about make.

Upvotes: 6

Marty Mast
Marty Mast

Reputation: 26

Here's a bash script I like to use on Linux. It has four modes, compile, compile and run, compile for release with optimization, and compile for release with optimization and then run the game.

Save this as build.sh, then using Linux terminal type "chmod +x build.sh"

#######!/bin/bash

appRunning=1

while [ $appRunning == 1 ]; do
    echo Compile debug[1], Compile and run debug[2], Compile release[3], Compile and run release[4], Exit[5]
    read user

    if [[ $user == 1 ]]; then
        clear
        echo Compiling for debug...
        echo
        g++ -c src/*.cpp -std=c++14 -Werror -m64
        g++ *.o -o bin/debug/Game -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network
        read -n 1 -s
        clear

    elif [[ $user == 2 ]]; then
        clear
        echo Compiling and running for debug...
        echo
        g++ -c src/*.cpp -std=c++14 -Werror -m64
        g++ *.o -o bin/debug/Game -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network
        ./bin/debug/Game
        read -n 1 -s
        clear

    elif [[ $user == 3 ]]; then
        clear
        echo Compiling for release...
        echo
        g++ -c src/*.cpp -std=c++14 -m64 -O3 -fexpensive-optimizations -s
        g++ *.o -o bin/release/Game -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network
        read -n 1 -s
        clear

    elif [[ $user == 4 ]]; then
        clear
        echo Compiling and running for release...
        echo
        g++ -c src/*.cpp -std=c++14 -m64 -O3 -fexpensive-optimizations -s
        g++ *.o -o bin/release/Game -lsfml-graphics -lsfml-window -lsfml-system -lsfml-audio -lsfml-network
        ./bin/release/Game
        read -n 1 -s
        clear
    elif [[ $user == 5 ]]; then
        exit
    fi
done

It compiles all .cpp files located in a folder called "src" and outputs to bin/Game. Of course you can modify this to your own likings.

Upvotes: 0

Garf365
Garf365

Reputation: 3707

One solution is to use build automation tool. A lot of differents tools exists, each with strong and weak points.

Personnaly, for small project, I use Make. You write in a file, named Makefile, rules which descibe relation between files and how to realize each step. For example, the minimal Makefile for your project will be :

sfml-app: main.o
    g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system

main.o: main.c
    g++ -c main.cpp

This file is located at root of project. You just have to enter "make" command to launch build. If you want, you will find a lot of documentation on internet. see https://en.wikipedia.org/wiki/Make_%28software%29 I encourage you to read about makefile to use it correctly and to be able to use its power.

Also, someone can advise you to use another build automation tool: it's a personnaly choice! You will find a list of existing tool here (not exhaustive): https://en.wikipedia.org/wiki/List_of_build_automation_software

Upvotes: 2

Np3w
Np3w

Reputation: 81

You can write a .sh script that executes the commands:

g++ -c main.cpp
g++ main.o -o sfml-app -lsfml-graphics -lsfml-window -lsfml-system

Upvotes: 4

Related Questions