FLTK logo

Article #599: Beginners Guide to fltk-config

FLTK matrix user chat room
(using Element browser app)   FLTK gitter user chat room   GitHub FLTK Project   FLTK News RSS Feed  
  FLTK Apps      FLTK Library      Forums      Links     Login 
 Home  |  Articles & FAQs  |  Bugs & Features  |  Documentation  |  Download  |  Screenshots  ]
 

Return to Articles | Show Comments | Submit Comment ]

Article #599: Beginners Guide to fltk-config

Created at 12:28 May 16, 2006 by ianmacarthur

When you build and install fltk, it also generates a configuration script "fltk-config" tailored to your machine, that will help with many day to day building tasks. The script can be executed in most "posix-like" shell environments, so that includes the unix/linux systems, Mac OSX and even Windows with the Msys or cygwin shells installed.

So What Can it Do?

  • Build your code: If your code is a single, self-contained file, it is enough to type:

                fltk-config --compile myfile.cxx

And your code will be compiled and linked into an executable called "myfile".  How easy is that?

Since fltk-config is tailored to your machine when the fltk lib is built, it knows where all the libraries are, and which ones to link with.

So now you can just click on your program and off you go... Well, except on the Mac. On the Mac, gui programs will not take focus unless they are part of a bundle executable, OR they have a suitable resource fork attached. But how to do that?

                fltk-config --post myfile

will attach a minimal resource fork to your app, and you are up and running.

Now, suppose your application wants to use OpenGL, or load image files, the default settings of fltk-config will not include all the necessary libraries for that. So you tell it to add them like this:

                fltk-config --use-gl --compile myGLapp.cxx
                fltk-config --use-images --compile myImageApp.cxx

Again, on the Mac, you'll have to run --post on the app to get going, but otherwise that's it!

And it works the *Same Way* on all the supported platforms, so if your code is well written, you can compile it and be up and running in no time at all.

  • BUT: What if your code is more than one file?

In that case, a simple Makefile is probably in order. The problem (well OK, one of the problems) with Makefiles is they tend to be host specific, which is partly why so many open source projects depend on the autoconf tools.

But we can use fltk-config to attain a high degree of portability with less complexity, like this:

################ template makefile ##############
# We don't know what compiler to use to build fltk on this machine - but fltk-config does...
CC  = $(shell fltk-config --cc)
CXX = $(shell fltk-config --cxx)

# Set the flags for compiler: fltk-config knows the basic settings, then we can add our own...
CFLAGS   = $(shell fltk-config --cflags) -Wall -O3 -I/other/include/paths...
CXXFLAGS = $(shell fltk-config --cxxflags) -Wall -O3 -I/other/include/paths...

# We don't know what libraries to link with: fltk-config does...
LINKFLTK = $(shell fltk-config --ldstaticflags)
LINKFLTK_GL = $(shell fltk-config --use-gl --ldstaticflags)
LINKFLTK_IMG = $(shell fltk-config --use-images --ldstaticflags)

# Possible steps to run after linking...
STRIP      = strip
POSTBUILD  = fltk-config --post # Required on OSX, does nothing on other platforms, so safe to call


# Define what your target application is called
all: MyApp

# Define how to build the various object files...

file1.o: file1.c file1.h  # a "plain" C file
        $(CC) -c $< $(CCFLAGS)

file2.o: file2.cxx file2.h file1.h  # a C++ file
        $(CXX) -c $< $(CXXFLAGS)

# Now define how to link the final app - let's assume it needs image and OpenGL support
MyApp:  file2.o file1.o
        $(CXX) -o $@ file2.o file1.o $(LINKFLTK_IMG) $(LINKFLTK_GL)
        $(STRIP) $@
        $(POSTBUILD) $@  # only required on OSX, but call it anyway for portability


############### end #################

Now, assuming your code is well written, so that it compiles on different platforms, this one Makefile can build your code on any supported platform - unix, linux, Windows, OSX...

Very convenient!

Listing ]


Comments

Submit Comment ]

From Luiji, 13:08 Aug 04, 2010 (score=3)

Is there any chance of a tutorial on getting FLTK to work with Autoconf/Automake?
Reply ]

From shaiay, 11:48 May 23, 2006 (score=3)

slightly OT:

when using automake to generate makefiles, does anybody know how I can add a rule to invoke fltk-config --post after the compilation?


Reply ]

 
 

Comments are owned by the poster. All other content is copyright 1998-2024 by Bill Spitzak and others. This project is hosted by The FLTK Team. Please report site problems to 'erco@seriss.com'.