Course list http://www.c-jump.com/bcc/
FLTK ( www.fltk.org ) is a is an Open Source, cross-platform C++ library to create desktop applications with Graphical User Interface. FLTK includes FLUID program to interactively design your GUI interface.
In this course we use FLTK (pronounced "fulltick") to build desktop GUI applications in order to test our C++ skills and knowledge.
In this lab we create a Windows directory to host our GUI (Graphical User Interface) programming labs and homework assignment projects.
Before you get started, decide where your working directory should reside on your harddrive. (Windows refers to directories as "folders".)
Keep it simple. For example:
C:\bcc
Caution:
Do not use spaces in your directory and file names. If you do, some things will break and cause a headache. When you need spacing, type underscores, for example, my_project.
Do not use a cloud drive, such as OneDrive, Google Drive, Dropbox, or Sync.com. Instead, choose a local harddrive to develop your C++ code. It's a great idea to backup your work on a cloud drive, but hosting a Visual Studio C++ project on a mapped cloud drive leads to nearly impossible to debug errors such as
improper file syncing in the background, which interferes with how Visual Studio locks and accesses files, causing missing files and lost work.
prohibited exclusive access to executable and binary files, causing "denied access" errors and corrupted builds.
danger of corrupting project configuration files and a subsequent loss of work.
obscure performance issues such as significantly slower compiler and linker reads and writes of hundreds of small files, commonly used during the compilation process.
Download c255labs.zip
Unzip c255labs.zip under directory of your choice. The result should be:
C:\bcc\c255labs
Important: delete c255labs.zip archive after extraction. You can always download it again when needed.
The extracted C:\bcc\c255labs directory will contain the following preconfigured subdirectories:
bin.........EXE programs that you build will be here
example.....Sample programs
external....External libraries: FLTK C++ headers, LIB files, and the FLUID program
JPEG........JPEG Image files shared between multiple projects
labs........Our lab projects and your homework
Start Visual Studio and open project
C:\bcc\c255labs\labs\c255_lab01_intro\c255_lab01_intro.sln
Click
View -> Solution Explorer
Open source file c255_lab01_main.cpp:
#include <Windows.h> // include Windows.h only if using WinMain
#include "FL/Fl.H"
#include "FL/Fl_Box.H"
#include "FL/Fl_Window.H"
// Use standard main to have console background:
// int main()
// Use WinMain if you don't want the console in the background:
int __stdcall WinMain(
__in HINSTANCE hInstance,
__in_opt HINSTANCE hPrevInstance,
__in LPSTR lpCmdLine,
__in int nShowCmd
)
{
Fl_Window window( 200, 200, "My window title" );
Fl_Box box( 0, 50, 200, 20, "Hello" );
window.show();
return Fl::run();
}
Compile the project:
Build -> Build Solution
(or simply press F7.)
When you compile your program,
All CPP files are compiled into OBJ files by the C++ compiler
All OBJ and LIB files are linked together and written into the EXE file by the linker.
When you run your EXE program, necessary DLLs are also loaded into memory, and program can call functions in our EXE and in DLL files.
C++ header files are plain text files that contain declarations of constant variables, C++ functions, and classes.
C++ function declarations are functions without a body. For example,
// myheader.h
// Function declaration:
bool load_data_from_file( char const* filename );
Once the header is included in your C++ program, you can call this function
// main.cpp
#include "myheader.h"
int main( int argc, char* argv[] )
{
bool success = false;
success = load_data_from_file( "C:/data/mydata.txt" );
if ( !success ) {
// could not load data from file
} else {
// everything is good
}
return 0;
}
There are a few possibilities where the function load_data_from_file() can be defined:
(a) you implement it in your own C++ file and compile
(b) it is written by someone else and exists in a binary form inside a DLL file (dynamic link library)
(c) it is written by someone else and exists in a binary form inside a LIB file (linker library)
What's the difference?
(a) -- no additional steps required
(b) and (c) -- you need to tell the linker where the LIB file is located. To do this, you set the path to the LIB file in "Additional Library Directories" and "Additional Dependencies" of your linker's configuration.
FLTK does not use DLLs, only LIB files. (However, if you want to add DLL files to your project, you can copy your DLLs to the directory where your EXE file is written. The reason is because when the program runs, it first searches the local directory (the place where the program is run from) for the required DLLs. If it can't find it, it looks in anything that your $PATH environment variable specifies.)
There are actually two places where our EXE files may be written:
C:\bcc\c255labs\bin\Debug
C:\bcc\c255labs\bin\Release
It depends whether you use "Debug" or "Release" configuration in your project. Most programmers test their code under both configurations.
A LIB file (or library) contains binary code used by the linker to link someone else's code with our executable file. LIB files may also contain indirect references to functions in DLLs, but we don't have to use those with FLTK.
This is a self-learning lab. There are no files to submit. Make sure to complete all of the above steps to build and test the project on your own computer.
NOTE: Reading the remaining parts of this handout is optional. The instructions for FLTK download and build, as well as FLTK C++ project configuration are provided as a reference and troubleshooting guide.
If you are experiencing problems building and running the c255_lab01_intro project, it may be worth a try to build FLTK library on your own computer as described below.
Download the source of the latest stable release version (e.g. fltk-N.N.N-source.tar.gz) from FLTK library website. FLTK is a cross-platform tool that works with Microsoft Visual Studio on Windows, as well as Linux and MAC OS.
Important: do not use C:\bcc\c255labs directory as a placeholder for FLTK download and build. Use a different location, for example, use your Windows Download folder or your own folder such as:
C:\Projects\fltk-1.4.4
Unzip the downloaded file. The process takes two steps: first, it unzips into a .tar file. Next, unzip the .tar file, which creates a subdirectory named fltk-N.N.N.
Download and install CMake GUI from
cmake.org
Open the website and scroll down to Binary distributions.
Choose Windows x64 Installer such as
cmake-4.1.0-rc4-windows-x86_64.msi
This will download the Windows Installer (.msi) (64-bit version).
Run CMake GUI installer.
Start CMake GUI application.
Caution: CMake GUI is a separate software. Using Visual Studio File -> Open -> CMake is the wrong choice!
When CMake GUI dialog opens:
Where is the source code: C:/Projects/fltk-1.4.4
Where to build the binaries: C:/Projects/fltk-1.4.4/build
Click the Configure button at the bottom left
Select Visual Studio 17 2022 (or similar, depending on your environment)
Choose x64
Click Finish
Wait while CMake configures the project.
When you see options appear (like FLTK_BUILD_TEST, FLTK_USE_GL, etc.), click Generate.
If you're happy with the default settings, you can save them.
Finally, click Open Project. This opens fltk.sln in Visual Studio.
NOTE: The location of fltk.sln is
C:/Projects/fltk-1.4.4/build/FLTK.sln
Close CMake GUI application.
You need to build FLTK Binaries in Visual Studio (Debug + Release x64).
fltk.sln ia a large solution that includes the source code for the FLTK demo apps and FLTK library itself. Be patient, it may take a while for all parts to load into Visual Studio development environment.
Since FLTK is open source software, once downloaded, the binaries must be built on your computer. You can use Visual Studio toolbar or menu
Build -> Configuration Manager...
to switch between active configurations - Release and Debug.
In Visual Studio, select Build -> Build Solution.
Important: Do this for both Debug x64 and Release x64 configurations.
Be patient while the surce code is being compiled. It may take several minutes to complete. If everything is okay, you should see the outputs similar to the examples provided below.
The results of the build are in
C:/Projects/fltk-1.4.4/build/bin
C:/Projects/fltk-1.4.4/build/lib
Here is sample Debug x64 output:
Build started at 4:58 PM...
...
3>Done building project "html.vcxproj" -- FAILED. <<<==== Ignore this error
...
117>Generating Code...
117>checkers.vcxproj -> C:\Projects\fltk-1.4.4\build\bin\test\Debug\checkers.exe
119>------ Build started: Project: ALL_BUILD, Configuration: Debug x64 ------
119>Building Custom Rule C:/igor/Projects/fltk-1.4.4/CMakeLists.txt
120>------ Skipped Build: Project: INSTALL, Configuration: Debug x64 ------
120>Project not selected to build for this solution configuration
========== Build: 116 succeeded, 1 failed, 0 up-to-date, 3 skipped ==========
========== Build completed at 5:04 PM and took 05:45.012 minutes ==========
Here is a sample Release x64 output:
1>------ Build started: Project: ZERO_CHECK, Configuration: Release x64 ------
1>1>Checking Build System
2>------ Skipped Build: Project: uninstall, Configuration: Release x64 ------
2>Project not selected to build for this solution configuration
3>------ Build started: Project: html, Configuration: Release x64 ------
4>------ Build started: Project: fltk_z, Configuration: Release x64 ------
5>------ Build started: Project: fltk_jpeg, Configuration: Release x64 ------
6>------ Build started: Project: fltk, Configuration: Release x64 ------
3>Converting Doxyfile to doxygen version 1.8.9
3>'C:\Projects\fltk-1.4.4\documentation\convert_doxyfile' is not recognized as an internal or external command,
3>operable program or batch file.
3>C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Microsoft\VC\v170\Microsoft.CppCommon.targets(254,5): error MSB8066
3>Done building project "html.vcxproj" -- FAILED. <<<==== Ignore this error
7>------ Skipped Build: Project: docs, Configuration: Release x64 ------
7>Project not selected to build for this solution configuration
5>Building Custom Rule C:/igor/Projects/fltk-1.4.4/jpeg/CMakeLists.txt
...
118>Generating Code...
117>checkers.vcxproj -> C:\Projects\fltk-1.4.4\build\bin\test\Release\checkers.exe
118>CubeView.vcxproj -> C:\Projects\fltk-1.4.4\build\bin\test\Release\CubeView.exe
119>------ Build started: Project: ALL_BUILD, Configuration: Release x64 ------
119>Building Custom Rule C:/igor/Projects/fltk-1.4.4/CMakeLists.txt
120>------ Skipped Build: Project: INSTALL, Configuration: Release x64 ------
120>Project not selected to build for this solution configuration
========== Build: 116 succeeded, 1 failed, 0 up-to-date, 3 skipped ==========
========== Build completed at 5:12 PM and took 02:14.811 minutes ==========
Exit Visual Studio.
Use two side-by-side Windows File Explorer panels to copy and paste the newly built FLTK library and source files:
From To
fltk-N.N.N-N\build\bin\Release\*.* ---> c255labs\external\fluid
fltk-N.N.N-N\FL\*.* ------------------> c255labs\external\x64_include\FL
fltk-N.N.N-N\GL\*.* ------------------> c255labs\external\x64_include\GL
fltk-N.N.N-N\build\lib\*.* -----------> c255labs\external\x64_lib
Our C:\bcc\c255labs\labs\c255_lab01_intro project is already pre-configured to build FLTK GUI applications. No changes are needed unless you are creating your own project from scratch.
For your reference, here is the list of the required project configuration options.
In Visual Studio, start by creating new project: File -> New ->Project.
Choose "Empty Project" -- C++ Windows Console
Specify project name
Specify directory location of your project, such as
C:\bcc\c255labs\myprojects
NOTE: Project options outlined here are designed to build correctly when your project directory location is a subdirectory of C:\bcc\c255labs.
Check option "Place solution and project in the same directory" -- checked
Add your C++ source file(s). In our programming projects the source files are located under src subdirectory and later also in fluid_project subdirectory.
Right-click project name in Solution Explorer and select Properties.
Configurations: All Configurations Platform: Active(x64)
General
OutputDirectory: $(SolutionDir)\..\..\bin\$(Configuration)\
C/C++
General
Additional Include Directories
..\..\external\x64_include;..\..\external\x64_include\build;%(AdditionalIncludeDirectories)
SDL checks: change it to -- No (/sdl-)
Linker
General
Additional Library Directories
..\..\external\x64_lib\$(Configuration);%(AdditionalLibraryDirectories)
Configurations: Debug
Linker
Input
Additional dependencies
fltkd.lib fltk_formsd.lib fltk_gld.lib fltk_imagesd.lib fltk_jpegd.lib fltk_pngd.lib fltk_zd.lib gdiplus.lib ws2_32.lib comctl32.lib
Configurations: Release
Linker
Input
Additional dependencies
fltk.lib fltk_forms.lib fltk_gl.lib fltk_images.lib fltk_jpeg.lib fltk_png.lib fltk_z.lib gdiplus.lib ws2_32.lib comctl32.lib
Configurations: All Configurations Platform: Active(x64)
Linker
System
change SubSystem to Windows (/SUBSYSTEM:WINDOWS)
This allows to use use WinMain() instead of main(). When building with WinMain(), console window disappears.
Be sure to test your program with both Debug and Release builds.
Have fun!