3

Question: Can someone give me instructions on how to install FLTK for Microsoft Visual Studio 2015, so that I can use FLTK for C++?

Extra Information:

the Chapter 12 drill, in Bjarne Stroustrup's Programming: Principle and Pracice using C++ wants me to install FLTK.

I have Microsoft Visual Studio 2015.

I downloaded the files at www.stroustrup.com/Programming/FLTK.

Then I opened the fltk.dsw in C://Program Files (x86)/Microsoft Visual Studio 14.0/VC.

But the build failed.

I searched this website for any similar problems; but the last similar question is a person asking for help with VS2010, 4 years ago.

I'm sorry again, guys. but this issue seems beyond my grasp because this is my first time installing a library on C++

  • Tomorrow, I'm going to try to install a newer version of fltk. apparently the fltk version at www.stroustrup.com/Programming/FLTK is outdated...? I've already spent 3 hours trying to install this today. I need some rest. – johnny smithson Oct 24 '16 at 01:50
  • The stourstrup library is not the FLTK library: it just uses FLTK. Just go to the FLTK website, pick the download and download it. Note that there are 3 versions - only pick 1.3 – cup Oct 24 '16 at 04:56
  • yeah, the FLTK library is not the stroustrup library; I notice that the books makes that repetitively clear. now it makes more sense why the download from that website had an outdated version of FLTK. thank you. – johnny smithson Oct 24 '16 at 18:31

2 Answers2

4

Since the above answer was given, changes have occurred that have made it easier to install the FLTK library on a Microsoft Windows system. I have outlined below how I went about installing the FLTK library and getting a simple FLTK program to run.

For those of you working your way through Bjarne Stroustrup's Programming, Principles, and Practices Using C++, here is how I got the FLTK library installed and operational on a Windows 10.0.18363 Build 18363 a.k.a. Windows 10 1809 Pro using the VCPKG and MSYS2 versions of the FLTK library 1.3.5.

INSTALLATION

Using Microsoft VCPKG

The steps below assume that Visual Studio is already installed.

1.  Clone the VCPKG
    1.1  Open PowerShell as an Administrator.
    1.2  Enter a directory to clone the VCPKG--e.g. C:\ProgramFiles.
    1.3  Copy and paste the following into PowerShell and press enter to start downloading VCPKG or go to https://github.com/Microsoft/vcpkg:  git clone https://github.com/microsoft/vcpkg.git
        1.3.1  After VCPKG finishes downloading, there should be a vcpkg-master directory. Enter the following at the command line:
            1.3.1.1  cd vcpkg-master
            1.3.1.2  .\bootstrap-vcpkg.bat
            1.3.1.3  .\vcpkg integrate install
        1.3.2  The command in step 1.3.1.2 will compile the VCPKG library using the most recent Visual Studio it can find on your system.
        1.3.3  The command in step 1.3.1.3 will configure Visual Studio to locate all vcpkg header files and binaries on a per-user basis. There's no need for manual editing of VC++ Directories paths.  SEE https://learn.microsoft.com/en-us/cpp/build/vcpkg?view=vs-2019#installation for more info.
    1.4  This concludes the installation of VCPKG.  For more information, see the following:
        1.4.1  https://learn.microsoft.com/en-us/cpp/build/vcpkg?view=vs-2019#installation
        1.4.2  https://devblogs.microsoft.com/cppblog/vcpkg-a-tool-to-acquire-and-build-c-open-source-libraries-on-windows/
2. Install FLTK Using VCPKG
    2.1  In PowerShell, execute the following commands:
        2.1.1  .\vcpkg install fltk:x86-windows
        2.1.2  .\vcpkg install fltk:x64-windows
    2.2  The above two commands will install FLTK into vcpkg-master\packages.

3.  Modify the x.H file
    3.1  When trying to run the FLTK test program under Visual Studio 2019, I was getting the error message:  "… cannot open include file ‘x11/xlib.h’ no such file or directory …" that is documented in Resource #3 below.  So, I ended up modifying the x.H file as instructed in this resource.  Here's what to do. . .
    3.2  If you get this error, double-click on the error message to open up the file within Visual Studio.  If it does not open, I found my file in: ..\vcpkg-master\installed\x64-windows\include\FL\x.H
    3.3  Go to line 28, "...between # include “Enumerations.H” on line 27 and # ifdef WIN32..." add include "# define WIN32" as shown by the red arrow in the screen shot below:

x.H file

Using MSYS2

The steps below assume that MSYS2 is already installed.

1.  Install FLTK
    1.1  Open the MSYS2 MSYS terminal.
    1.2  For 64-bit systems, enter:  pacman -S mingw-w64-x86_64-fltk 
         For 32-bit systems, enter:  pacman -S mingw-w64-i686-fltk
    1.3  In the ../msys64/mingw64/bin directory, there should be a fltk-config file.  If the installation was successful, you should see the fltk version number after executing the following command:  fltk-config --version.

FLTK TEST PROGRAM

Using Microsoft Visual Studio Community 2019 Version 16.4.5

    1.  Create a new project
        1.1  Select File --> New --> Project or press CTRL+SHIFT+N
        1.2  Click on Windows Desktop Application
        1.3  Enter a project name and create your project
    2.  Create a header file
        2.1  Right-click on the Header Files folder and select Add --> New Item
        2.2  Name the header file
        2.3  In the new header file, under #include "resource.h" enter the following:

                #include <Fl/Fl.H>
                #include <FL/Fl_Box.H>
                #include <FL/Fl_Window.H>

        2.4  Save the file.
    3.  Open the source file
        3.1  Under #include "framework.h", enter a #include statement for the header file created in step 2 above.
        3.2  Locate the comment:  // TODO: Place code here.
        3.3  Copy and paste the following code below the comment in Step 3.2:

                Fl_Window window(200, 200, "Window Title");
                Fl_Box box(0, 0, 200, 200, "Hey, I mean, Hello, World!");
                window.show();
                return Fl::run();

        3.4  Press CTRL+F5 to run the program.  A window should display with the words: "Hey, I mean, Hello, World!".

Using MSYS2

    1.  Using a text editor or IDE of your choice, create a header file and copy and paste the following:

        #include <Fl/Fl.H>
        #include <FL/Fl_Box.H>
        #include <FL/Fl_Window.H>

    2.  Create a source file--e.g. fltk_test.cpp.  Copy and paste the following code into the source file:

            #include "name of your header file in Step 1"

            using namespace std;

            int main (int argc, char *argv[])
            {
                Fl_Window *window = new Fl_Window(340, 180);
                Fl_Box *box = new Fl_Box(20, 40, 300, 100, "Hello World");
                box->box(FL_UP_BOX);
                box->labelfont(FL_BOLD+FL_ITALIC);
                box->labelsize(36);
                box->labeltype(FL_SHADOW_LABEL);
                window->end();
                window->show(argc, argv);
                return Fl::run();
            }

    3.  Compile and build the source
        3.1  Open a MSYS MinGW 64 terminal
        3.2  Enter the following at the command line:  g++ -v -g <your source file name> -lfltk -o <your exe file name>.  For example,  g++ -v -g fltk_test.cpp -lfltk -o fltk_test.exe
        3.4  The linker flag -lfltk MUST come after the source file [THIS IS VERY IMPORTANT!].  If it does not, undefined reference errors will be generated.
    4.  Run the program:  .\fltk_test.exe.  A window should display with the words: "Hello, World!".

EXTRA - Makefile

Below is a sample Makefile using the file name: fltk_mingw_test

#
# DATE:     2020 FEB 27
# FILENAME: Makefile
# DESCRIPTION:  This is a Makefile for the MSYS2 version of FLTK 
#   1.3.5.  It will compile and build the Hello World program 
#   found at https://www.fltk.org/doc-1.3/basics.html#basics_standard_compiler
#
CXX      = $(shell fltk-config --cxx)
DEBUG    = -g
CXXFLAGS = $(shell fltk-config --use-gl --use-images --cxxflags ) -I.
LDFLAGS  = $(shell fltk-config --use-gl --use-images --ldflags )
LDSTATIC = $(shell fltk-config --use-gl --use-images --ldstaticflags )
LINK     = $(CXX)

TARGET = C:\<your path>\fltk_mingw_test.exe #File extension must be exe
OBJS =   fltk_mingw_test.o
SRCS =   fltk_mingw_test.cpp

.SUFFIXES: .o .cpp
%.o: %.cpp
    $(CXX) $(CXXFLAGS) $(DEBUG) -c $<

all: $(TARGET)
    $(LINK) -o $(TARGET) $(OBJS) $(LDSTATIC)

$(TARGET): $(OBJS)
fltk_mingw_test.o: fltk_mingw_test.cpp fltk_mingw_test.h

clean: $(TARGET) $(OBJS)
    rm -f *.o 2> /dev/null
    rm -f $(TARGET) 2> /dev/null

Please note: you need to put a tab character at the beginning of every recipe line! This is an obscurity that catches the unwary. If you prefer to prefix your recipes with a character other than tab, you can set the .RECIPEPREFIX variable to an alternate character (see Special Variables).

FOR MORE INFORMATION SEE: GNU Make Manual

RESOURCES

  1. vcpkg: a C++ package manager for Windows, Linux, and MacOS
  2. Vcpkg: a tool to acquire and build C++ open source libraries on Windows
  3. How to install and use fltk-1.3.4 in Visual Studio 2017 2.0 [complete guide] – preventing cross-contamination
  4. GNU make Manual
Allan Head
  • 41
  • 5
2

To install the FLTK to work with the VS 2015, you can follow this article: http://www.c-jump.com/bcc/common/Talk2/Cxx/FltkInstallVC/FltkInstallVC.html

Now the latest version of FLTK is 1.3.3 and you can find it from here http://www.fltk.org/software.php and select the ‘fltk-1.3.3-source.tar.gz’ to download. I already downloaded this one and followed the steps in the above guide document, finally it is successful and the test result is matched the expected one as below:

enter image description here

Sara Liu - MSFT
  • 6,007
  • 1
  • 21
  • 27
  • thank you so much, Sara. At first, it didn't work; but only because I wasn't doing exactly what the link suggested. but when I did exactly every instruction in the link, and it all worked out. I up-voted your helpful answer, but it won't show because I have less than 15 reputation, but thank you so much. – johnny smithson Oct 24 '16 at 18:22