【问题标题】:Stroustrup: For C++, how do I install FLTK library?Stroustrup:对于 C++,我如何安装 FLTK 库?
【发布时间】:2017-03-05 17:05:21
【问题描述】:

问题:谁能告诉我如何安装 FLTK for Microsoft Visual Studio 2015,以便我可以使用 FLTK for C++?

额外信息:

Bjarne Stroustrup 的 Programming: Principle and Pracice using C++ 中的第 12 章练习要我安装 FLTK。

我有 Microsoft Visual Studio 2015。

我在 www.stroustrup.com/Programming/FLTK 下载了文件。

然后我在 C://Program Files (x86)/Microsoft Visual Studio 14.0/VC 中打开了 fltk.dsw。

但是构建失败了。

我在这个网站上搜索过任何类似的问题;但最后一个类似的问题是一个人在 4 年前寻求 VS2010 的帮助。

再次抱歉,伙计们。但这个问题似乎超出了我的掌握,因为这是我第一次在 C++ 上安装库

【问题讨论】:

  • 明天,我将尝试安装更新版本的 fltk。显然 www.stroustrup.com/Programming/FLTK 上的 fltk 版本已经过时了......?我今天已经花了 3 个小时尝试安装它。我需要休息一下。
  • stourstrup 库不是 FLTK 库:它只是使用 FLTK。只需访问 FLTK 网站,选择下载并下载即可。请注意,有 3 个版本 - 只选择 1.3
  • 是的,FLTK 库不是 stroustrup 库;我注意到这些书反复明确地说明了这一点。现在更有意义的是,为什么从该网站下载的 FLTK 版本过时。谢谢。

标签: c++ visual-studio-2015 installation fltk


【解决方案1】:

自从给出上述答案以来,已经发生了一些变化,使得在 Microsoft Windows 系统上安装 FLTK 库变得更加容易。我在下面概述了我是如何安装 FLTK 库并运行一个简单的 FLTK 程序的。

对于那些正在学习 Bjarne Stroustrup 的《使用 C++ 的编程、原则和实践》的人,这里是我如何使用 VCPKG 和FLTK 库 1.3.5 的 MSYS2 版本。

安装

使用 Microsoft VCPKG

以下步骤假定已安装 Visual Studio。

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://docs.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://docs.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:

使用 MSYS2

以下步骤假设已经安装了 MSYS2。

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 测试程序

使用 Microsoft Visual Studio Community 2019 版本 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!".

使用 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: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

请注意:您需要在每个配方行的开头放置一个制表符!这是一个晦涩难懂的东西。如果您喜欢在食谱前加上制表符以外的字符,可以将 .RECIPEPREFIX 变量设置为备用字符(请参阅特殊变量)。

欲了解更多信息,请参阅:GNU Make Manual

资源

  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

【讨论】:

    【解决方案2】:

    要安装 FLTK 以使用 VS 2015,您可以关注这篇文章:http://www.c-jump.com/bcc/common/Talk2/Cxx/FltkInstallVC/FltkInstallVC.html

    现在 FLTK 的最新版本是 1.3.3,您可以从这里http://www.fltk.org/software.php 找到它并选择“fltk-1.3.3-source.tar.gz”进行下载。我已经下载了这个并按照上面的指导文档中的步骤,终于成功了,测试结果与预期的一致,如下所示:

    【讨论】:

    • 非常感谢,莎拉。起初,它不起作用。但这仅仅是因为我没有完全按照链接的建议进行操作。但是当我完全按照链接中的每一条指令进行操作时,一切都解决了。我对你的有用答案投了赞成票,但它不会显示,因为我的声望低于 15,但非常感谢。
    猜你喜欢
    • 2012-01-31
    • 1970-01-01
    • 1970-01-01
    • 2011-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多