main-compilingwithfltk

Compiling programs with FlTk and VS2005

A tutorial for CS559 students
written October 2007, by Mike Gleicher

There is another set of tutorials that walk you through setting up a project to use FlTk with Visual Studio for CS559. This one is a little different in that I will try to explain why you are doing each of the required steps, as well as giving you some alternatives. By understanding this, you should be more able to deal with things when they break.

Overview: What needs to happen

In order to use an external library (like fltk), a number of things need to happen:

  1. Your program (the C++) must include the header files that tell what functions are available in the library. This means that you must tell the compiler where to find the header files.
  2. Your program must be compiled in a way that is compatible with the library. This means you need to adjust some settings that you might not otherwise care about in order to be compatible with the library.
  3. Your program must be linked with the library. This means you need to tell the linker that you need the library, and where it can find it.
  4. Your program (which now includes the library) might need to load other components at runtime. This means that you need to tell the operating system where to find these components.

Notice that each of these steps require different things to be done, and will cause different problems if things go wrong.

Part 1 - Finding Header Files

In order for C++ to use some class, function, global variable, macro, or anything else, it must be declared. To get the declarations for a library into your program, you include a header file. So, in your .cpp files (or your own header files) you will have things like

#include

If you don’t, the compiler will give you errors in your program. Be aware that FlTk has many header files, and you will need to include the ones that your program uses.

Once you’ve told the compiler what file it needs, you must also tell it what directory to find the files in. In the CSL environment, the fltk include files are in:

`` S:\fltk\include ``

However, if you are working on your own computer, you probably have FlTk in a different place.

There are two different ways to tell Visual Studio where to find the FlTk include files:

  1. You can add the FlTk include directory to your Visual Studio default settings.
  2. You can set the path inside of the Visual Studio Project.

Method #1 has the advantage that you do it once for Visual Studio, and then all your projects know about FlTk. It also has the advantage that you set it for each computer you use, and the projects don’t need to be changed if the computers are different. The downside is that all your projects will search FlTk whether you need it or not, and your projects don’t specify that they need FlTk includes (they assume you have Visual Studio set up in a particular way).

The advantage of Method #2 is that it makes no assumptions about how visual studio is set up, but you do need to set things for each project and be careful to make things work across computers. To make #2 work across computers, rather than putting the hard path into your project, you set the include directory to refer to an environment variable. For example, I set the include search path to include

`` $(FLTKHOME)/include ``

and I set the environment variable to be S:\fltk at UW and C:\Tools\FlTk on my laptop.

Part 2 - Using the right compilation settings

In order for your program to work correctly with FlTk you need to make sure you use the same compiler settings as was used to build FlTk. Generally, this is easy because the defaults are OK - just be sure to use the right version of the FlTk library. (if you’re building your project with debugging, use the debug build of the library)

However: there is one setting that is not set correctly by default. The FlTk libraries are built using the “multi-threaded dll” versions of the C runtime. What this means is that the functions in the standard C/C++ library

When you program in C or C++, you make use of a basic library for things you might think of as built-in (file I/O, memory allocation, …). There are several versions of this library.

Page last modified on October 08, 2007, at 03:58 PM