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

Author Topic: Linking SFML libraries to main application with MinGW  (Read 6006 times)

0 Members and 1 Guest are viewing this topic.

Anonymous

  • Guest
Linking SFML libraries to main application with MinGW
« on: July 10, 2014, 05:19:06 pm »
Hi,

I have been developing an audio library based in C. I have then been developing an application which uses this library based in C++, and this C++ application uses the SFML libraries to display information from my library onto the screen. I was previously using Visual C++ to create my c library code and cpp application code, however I realised that all of it was being passed through a c++ compiler, and I want my library to be purely C code so I am taking to doing everything outside of the VC++ IDE.

I have learnt how to create a .o file from my C code. I then use
Code: [Select]
extern "C" { #include analyser.h } in my main.cpp application, and use the command
Code: [Select]
g++ -o myAnalyser main.cpp analyser.o to link my c++ application with the c library.

The next step I need to do is to get my main.cpp application linking with all the SFML files, so that the program will run correctly. All of my settings were managed by VC++, the imports I have in my main.cpp file are: #include <SFML/Graphics.hpp> #include <SFML/Audio.hpp> #include <SFML/System.hpp>, and I have also had to include the libsndfile-1.dll and openal32.dll files in the directory of which my application was built to run.

If anyone would be able to help me compiling my cpp code with MinGW with the relevant compiler flags, instead of having to rely on the properties I have set in VC++ that would be great.

Many thanks,
Mark.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Linking SFML libraries to main application with MinGW
« Reply #1 on: July 10, 2014, 05:33:12 pm »
In my opinion, the first step should be to pick/adopt a cross platform build system to manage your build process.
Personally I recommend SCons for this.
Some people prefer CMake, qmake or plain old classical make (they are wrong of course, but I respect their right to choose ;) ).
Whatever you pick, the important thing is to pick something and then learn your tool properly - it will make your life a lot easier going forward when you want to juggle multiple compilers and multiple operating systems.
Even if you only ever want to use one compiler on one OS, using an existing well-known and feature full build system will help you compared to whatever ad-hoc thing you can create yourself.
« Last Edit: July 10, 2014, 05:37:04 pm by Jesper Juhl »

Anonymous

  • Guest
Re: Linking SFML libraries to main application with MinGW
« Reply #2 on: July 11, 2014, 10:37:00 am »
Hi,

To get started I am trying to compile a simple SFML app in bash with MinGW (g++):

Code: [Select]
#include <SFML/Graphics.hpp>

int main()
{
    sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }

    return 0;
}

I have been following this tutorial: SFML and Linux
My code compiled with
Code: [Select]
g++ -c main.cpp -I/c/SFML-2.1/include but then when I try to link the compiled file to the libraries I get an error. I use the command:
Code: [Select]
g++ main.o -o sfml-app -L/c/SFML-2.1/lib -lsfml-graphics -lsfml-window -lsfml-system but I get the following result:


I think this is because I am not on linux, I am on windows. And so I am not supposed to link to .so files, the files I want to link to in SFML-2.1/lib are Object File Library files. If anyone would be able to help me create an executable for this simple SFML code on MinGW that would be very helpful.

Thanks,
Mark

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Linking SFML libraries to main application with MinGW
« Reply #3 on: July 11, 2014, 10:55:24 am »
What MinGW version are you using? From where did you get SFML?
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Anonymous

  • Guest
Re: Linking SFML libraries to main application with MinGW
« Reply #4 on: July 11, 2014, 11:10:06 am »
I am using 4.8.1 and I am using SFML 2.1 for Visual C++ 2010, which might be the problem. Looking at the downloads page I guess that I need the SFML GCC 4.7 MinGW for Windows? I was using visual c++ but I am trying to switch to compiling without an IDE.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Linking SFML libraries to main application with MinGW
« Reply #5 on: July 11, 2014, 11:13:00 am »
SFML needs to be compiled with the same compiler, you're using to compile your application and since SFML doesn't provide packages for GCC 4.8.1, you'll have to rebuild SFML on your own.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Anonymous

  • Guest
Re: Linking SFML libraries to main application with MinGW
« Reply #6 on: July 11, 2014, 11:53:13 am »
Okay, I've downloaded the sources, how will I go about compiling with gcc 4.8.1, are there any tutorials because I am not sure what to do?
Thanks for the help

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Linking SFML libraries to main application with MinGW
« Reply #7 on: July 11, 2014, 11:55:00 am »
If you had spent 5 seconds on http://www.sfml-dev.org/ looking for it you'd have found: http://www.sfml-dev.org/tutorials/2.1/compile-with-cmake.php

SFML is extremely well documented.

Anonymous

  • Guest
Re: Linking SFML libraries to main application with MinGW
« Reply #8 on: July 11, 2014, 12:14:33 pm »
I have been looking on the tutorials because they are very useful. I just didn't realise that CMake was what I needed for this task.

Anonymous

  • Guest
Re: Linking SFML libraries to main application with MinGW
« Reply #9 on: July 11, 2014, 02:56:46 pm »
I have gone through with CMake and installed both the static and dynamic libraries for debug and release modes in the directory C:/Program Files (x86)/SFML

I have now tried to follow the tutorial SFML and Linux, replacing <sfml-install-path> with /c/"Program Files (x86)"/SFML (not sure if I need the brackets but I used them due to the spacing).

Commands used to try and create an executable program that links in to the SFML files:

g++ -c main.cpp -I<sfml-install-path>/include
g++ main.o -o sfml-app -L<sfml-install-path>/lib -lsfml-graphics -lsfml-window -lsfml-system
export LD_LIBRARY_PATH=<sfml-install-path>/lib && ./sfml-app

I manage to generate a .o and then an executable file, however at runtime I get the message "The program can't start because sfml-graphics-2.dll is missing from your computer. Try reinstalling the program to fix this problem." The sfml-graphics-2.dll, along with the other release and debug dll files are found in /c/Program Files (x86)/SFML/bin, so I am unsure where I am supposed to link these, and what I need to do to get my program running.

Thanks for the continued help, I feel like I'm almost there.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10846
    • View Profile
    • development blog
    • Email
Re: Linking SFML libraries to main application with MinGW
« Reply #10 on: July 11, 2014, 03:08:59 pm »
From the tutorials about Code::Blocks and VS:

Quote
Compile it, and if you linked to the dynamic version of SFML, don't forget to copy the SFML DLLs (they are in <sfml-install-path/bin>) to the directory where your compiled executable is.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Anonymous

  • Guest
Re: Linking SFML libraries to main application with MinGW
« Reply #11 on: July 11, 2014, 03:12:48 pm »
Thanks very much, I knew I was almost there! Working with SFML really is a dream with the amount of help in the documentations, tutorials and on the forums.