0
votes

I'm trying to re-learn C++ and I'm working with the Eclipse CDT on Windows with MinGW and I'd like to include an external library without modifying the MinGW installation. Currently, I'm working with SDL2, but this is a more generalized question.

I've successfully compiled and run my test program using the following steps:

  1. Download the SDL2-devel-2.0.4.mingw.tar.gz (here) and extract it
  2. Copy files from SDL2-2.0.4\ to my MinGW install:
    • \include\SDL2 to %MinGW%\include\
    • \lib\libSDL2.a to %MinGW%\lib\
    • \lib\libSDL2.dll.a to %MinGW%\lib\
    • \lib\libSDL2.la to %MinGW%\lib\
    • \lib\libSDL2main.a to %MinGW%\lib\
    • \bin\SDL2.dll to %MinGW%\bin\
  3. Add the libraries (in order) to the linker via Project->Properties : C/C++ Build->Settings : ("Tool Settings" tab) MinGW C++ Linker->Libraries : "Libraries (-l)" section
    • SDL2main
    • SDL2
  4. #include <SDL2\SDL.h> in my cpp/h file
  5. Build the program
  6. Copy SDL2.dll to the output directory with my .exe
  7. Run

On this particular machine, I'm using the i686-w64-mingw32 build of the SDL2-2.0.4, but I'd like to point the linker to this directly without copying to the MinGW installation in step 2 above. Then I could simply include these files in my repo without expecting a correctly modified MinGW install, or changing the pointer to the x86_64-w64-mingw32 build on a different machine, etc.

On a slightly different note (and probably a separate question), is there a method of automating step 6? I can't seem to find it, and manually copying the dlls on every clean build is rather annoying...

1

1 Answers

1
votes

You are almost there, simply add include directories to your externally unzipped location of SDL2 and add Library search path for the linking stage.

  1. Project->Properties : C/C++ Build->Settings : ("Tool Settings" tab) MinGW C++ Compiler->Includes : "Include paths (-I)" section

  2. Project->Properties : C/C++ Build->Settings : ("Tool Settings" tab) MinGW C++ Linker->Libraries : "Library search path (-L)" section

As for your other question, ensure that you have SDL2.dll on your PATH and then you won't have to copy it. If you really want it copied, you could add it to the post build steps: Project->Properties : C/C++ Build->Settings : ("Build Steps" tab) Post-build steps : Command