How to link against fltk-1.3.4-1 using CMake on OSX ?

705 views
Skip to first unread message

Duncan Gibson

unread,
Dec 12, 2016, 10:12:28 AM12/12/16
to fltkg...@googlegroups.com
Just to add to the flurry of CMake related articles here recently :-)

A while ago I posted something about switching to a Mac and being
surprised at what the XCode environment did and didn't offer, but
I didn't archive those discussions, so I don't remember details.

I've just downloaded the 30-day trial of CLion from JetBrains to
see how it compares (I already use PyCharm at work and like it)
but the CLion project compile function is built around CMake.

I've built fltk-1.3.4-1 both with and without CMake, but so far I
have only been able to get CMake to recognise the FL include so
that the sources compile, but I have not been able to get CMake
to link against libfltk.a even with the tips in README.CMake.txt

If I install fltk in my project directory the basic hierarchy is:

fltest:
fltest.h
fltest.cpp
include/
FL/
other/
lib/
libfltk.a
libother.a

If I build fltk-1.3.4-1 with CMake then the FL and libfltk.a
reside elsewhere and I set FLTK_DIR to /path/fltk-1.3.4-1/build

Can someone post a stripped down CMakeLists.txt file as an example
of how to build in either or both configurations?

D.

PS. I don't remember the exact error messages because I tried so many
combinations, the Mac is at home, and I only post from work...

this message and any attachments are intended for the use of the addressee or addressees only.
The unauthorised disclosure, use, dissemination or copying (either in whole or in part) of its
content is not permitted.
If you received this message in error, please notify the sender and delete it from your system.
Emails can be altered and their integrity cannot be guaranteed by the sender.

Please consider the environment before printing this email.

Albrecht Schlosser

unread,
Dec 12, 2016, 11:24:09 AM12/12/16
to fltkg...@googlegroups.com
Okay, I'll try. But note that I don't have MacOS.

First of all, if you want to use FLTK in your project(s) with CMake then
the best bet is to build FLTK with CMake as well because it provides
FLTKConfig.cmake so you can easily "import" the FLTK include and library
definitions.

Whether you install FLTK somewhere (with make install) or use it
directly from its build directory you should always set FLTK_DIR to
point at the directory where FLTKConfig.cmake resides. Unfortunately
this is not consistent if you use the build directory or if you install
FLTK (the directory layout appears to be platform dependent, at least in
FLTK 1.3.4 - I'll take a look into it for FLTK 1.4.0 and later).

That said: in the build directory FLTKConfig.cmake must always be used
in the root of the build directory, hence FLTK_DIR must point at the
build root. The other FLTKConfig.cmake you may find
(<build>/etc/FLTKConfig.cmake) is for use when FLTK gets installed.

If FLTK was installed, look for FLTKConfig.cmake in or "below" the
install directory.

In my Linux system this is <prefix>/share/fltk/
(/usr/local/fltk-1.3.4/share/fltk/), under Windows this is obviously
different (:-(),
it's <prefix>/CMake.

That said, I have a (not that) simple CMakeLists.txt that builds the
CubeView test program. To do that I copied the necessary source files
and config.h (!) from the FLTK source/test and build directories, resp..
config.h is only necessary since the CubeView code uses it, but it is
not installed because it is a private FLTK file. Since this is a demo I
simply copied it. Note that config.h is only necessary if FLTK was
installed; the build works w/o it if the build uses the build directory
directly.

Here's my build environment and CMakeLists.txt:

$ ls
CMakeLists.txt config.h CubeMain.cxx CubeView.cxx CubeView.h
CubeViewUI.fl
$ cat CMakeLists.txt
cmake_minimum_required(VERSION 2.6.3)

project(CubeView)

# change this to your fltk build directory
# set(FLTK_DIR ~/git/fltk-1.3/build/Debug/)
set(FLTK_DIR /usr/local/fltk-1.3.4/share/fltk/)

find_package(FLTK REQUIRED NO_MODULE)
include_directories(${FLTK_INCLUDE_DIRS})

#run fluid -c to generate CubeViewUI.cxx and CubeViewUI.h files
add_custom_command(
OUTPUT "CubeViewUI.cxx" "CubeViewUI.h"
COMMAND fluid -c ${CMAKE_CURRENT_SOURCE_DIR}/CubeViewUI.fl
)

set (DEBUG_FLTK 1)
if (DEBUG_FLTK)
message(STATUS "FLTK_INCLUDE_DIRS = '${FLTK_INCLUDE_DIRS}'")

message(STATUS "CMAKE_SOURCE_DIR = '${CMAKE_SOURCE_DIR}'")
message(STATUS "CMAKE_CURRENT_SOURCE_DIR = '${CMAKE_CURRENT_SOURCE_DIR}'")

message(STATUS "CMAKE_BINARY_DIR = '${CMAKE_BINARY_DIR}'")
message(STATUS "CMAKE_CURRENT_BINARY_DIR = '${CMAKE_CURRENT_BINARY_DIR}'")

endif ()

include_directories(${CMAKE_CURRENT_BINARY_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})

add_executable(CubeView WIN32 CubeMain.cxx CubeView.cxx CubeViewUI.cxx)

target_link_libraries(CubeView fltk fltk_gl)

# End of CMakeLists.txt

$ mkdir build
$ cd build/
$ cmake ..
-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- FLTK_INCLUDE_DIRS = '/usr/local/fltk-1.3.4/include'
-- CMAKE_SOURCE_DIR = '~/testprogramme/fltk-cubeview'
-- CMAKE_CURRENT_SOURCE_DIR = '~/testprogramme/fltk-cubeview'
-- CMAKE_BINARY_DIR = '~/testprogramme/fltk-cubeview/build'
-- CMAKE_CURRENT_BINARY_DIR = '~/testprogramme/fltk-cubeview/build'
-- Configuring done
-- Generating done
-- Build files have been written to: ~/testprogramme/fltk-cubeview/build
$ make
[ 25%] Generating CubeViewUI.cxx, CubeViewUI.h
Scanning dependencies of target CubeView
[ 50%] Building CXX object CMakeFiles/CubeView.dir/CubeMain.cxx.o
[ 75%] Building CXX object CMakeFiles/CubeView.dir/CubeView.cxx.o
[100%] Building CXX object CMakeFiles/CubeView.dir/CubeViewUI.cxx.o
Linking CXX executable CubeView
[100%] Built target CubeView
$

That's it. HTH

Duncan Gibson

unread,
Dec 13, 2016, 3:25:23 AM12/13/16
to fltkg...@googlegroups.com
Thanks, Albrecht, for the CMakeLists.txt for building CubeView.

I took your file and tried to build CubeView on MacOSX but was
not successful. I didn't think to check whether it build as
part of the main fltk-1.3.4-1 build using CMake. Silly me :-(

I was hoping that CMake would insulate me from having to work
out all of the OSX internals that I don't really want to learn.

Anyway, when building CubeView on its own in /tmp/cube/build
I get the following...

$ cmake ..
-- The C compiler identification is AppleClang 8.0.0.8000042
-- The CXX compiler identification is AppleClang 8.0.0.8000042
-- Check for working C
compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- Check for working C
compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/cc
-- works -- Detecting C compiler ABI info -- Detecting C compiler ABI
info - done -- Detecting C compile features -- Detecting C compile
features - done -- Check for working CXX
compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Check for working CXX
compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- works -- Detecting CXX compiler ABI info -- Detecting CXX compiler
ABI info - done -- Detecting CXX compile features -- Detecting CXX
compile features - done -- FLTK_INCLUDE_DIRS =
'/Users/duncan/fltk-1.3.4-1/build;/Users/duncan/fltk-1.3.4-1' --
CMAKE_SOURCE_DIR = '/tmp/cube' -- CMAKE_CURRENT_SOURCE_DIR =
'/tmp/cube' -- CMAKE_BINARY_DIR = '/tmp/cube/build'
-- CMAKE_CURRENT_BINARY_DIR = '/tmp/cube/build'
-- Configuring done
-- Generating done
-- Build files have been written to: /tmp/cube/build


and when it comes to the actual build, compiling works, but
the linking phase fails...

$ make
[ 20%] Generating CubeViewUI.cxx, CubeViewUI.h
Scanning dependencies of target CubeView
[ 40%] Building CXX object CMakeFiles/CubeView.dir/CubeMain.cxx.o
[ 60%] Building CXX object CMakeFiles/CubeView.dir/CubeView.cxx.o
[ 80%] Building CXX object CMakeFiles/CubeView.dir/CubeViewUI.cxx.o
[100%] Linking CXX executable CubeView.app/Contents/MacOS/CubeView
Undefined symbols for architecture x86_64:
"_CFAbsoluteTimeGetCurrent", referenced from:
Fl::add_timeout(double, void (*)(void*), void*) in
libfltk.a(Fl_cocoa.mm.o)
Fl::repeat_timeout(double, void (*)(void*), void*) in
libfltk.a(Fl_cocoa.mm.o)
"_CFAttributedStringCreate", referenced from:
Fl_Font_Descriptor::Fl_Font_Descriptor(char const*, int) in
libfltk.a(fl_font.cxx.o)
Fl_Quartz_Graphics_Driver::text_extents(char const*, int, int&,
int&, int&, int&) in libfltk.a(fl_font.cxx.o)

[...]

"_objc_msgSend_stret", referenced from:
Fl_X::calc_mac_os_version() in libfltk.a(Fl_cocoa.mm.o)
fl_open_display() in libfltk.a(Fl_cocoa.mm.o)
-[FLWindow convertBaseToScreen:] in libfltk.a(Fl_cocoa.mm.o)
-[FLWindow setSubwindowFrame] in libfltk.a(Fl_cocoa.mm.o)
-[FLWindowDelegate windowDidMove:] in libfltk.a(Fl_cocoa.mm.o)
update_e_xy_and_e_xy_root(NSWindow*) in libfltk.a(Fl_cocoa.mm.o)
-[FLWindowDelegate windowDidResize:] in libfltk.a(Fl_cocoa.mm.o)
...
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see
invocation) make[2]: *** [CubeView.app/Contents/MacOS/CubeView] Error
1 make[1]: *** [CMakeFiles/CubeView.dir/all] Error 2
make: *** [all] Error 2

Albrecht Schlosser

unread,
Dec 13, 2016, 7:27:26 AM12/13/16
to fltkg...@googlegroups.com
On 13.12.2016 09:25 Duncan Gibson wrote:
> Thanks, Albrecht, for the CMakeLists.txt for building CubeView.

Welcome.

> I took your file and tried to build CubeView on MacOSX but was
> not successful. I didn't think to check whether it build as
> part of the main fltk-1.3.4-1 build using CMake. Silly me :-(

Did it build with the normal FLTK build?

> I was hoping that CMake would insulate me from having to work
> out all of the OSX internals that I don't really want to learn.

That's what it should do.

> Anyway, when building CubeView on its own in /tmp/cube/build
> I get the following...
>
> ...
>
> and when it comes to the actual build, compiling works, but
> the linking phase fails...
>
> $ make
> [ 20%] Generating CubeViewUI.cxx, CubeViewUI.h
> Scanning dependencies of target CubeView
> [ 40%] Building CXX object CMakeFiles/CubeView.dir/CubeMain.cxx.o
> [ 60%] Building CXX object CMakeFiles/CubeView.dir/CubeView.cxx.o
> [ 80%] Building CXX object CMakeFiles/CubeView.dir/CubeViewUI.cxx.o
> [100%] Linking CXX executable CubeView.app/Contents/MacOS/CubeView
> Undefined symbols for architecture x86_64:
> "_CFAbsoluteTimeGetCurrent", referenced from:
> Fl::add_timeout(double, void (*)(void*), void*) in
> ...
> ld: symbol(s) not found for architecture x86_64
> clang: error: linker command failed with exit code 1 (use -v to see
> invocation) make[2]: *** [CubeView.app/Contents/MacOS/CubeView] Error
> 1 make[1]: *** [CMakeFiles/CubeView.dir/all] Error 2
> make: *** [all] Error 2

That's the point where I can't help you with MacOS. Maybe Manolo can do.

But I have another "generic" hint that may help, at least to find out
what's happening. If the CubeView example builds in the normal FLTK
build you can delete the CubeView executable (usually in bin/examples
folder) and then run make with

$ make VERBOSE=ON

to see the entire build command line. This may help to find out which
Framework or whatever is missing if you compare it with your CubeView
build (also with VERBOSE=ON).

That said, building CubeView is not a trivial example because it uses
fluid to generate source and header files and uses GL which can be
another source of trouble. Building the hello example program would be
less complicated. However I see in your build log that fluid worked and
the undefined symbols don't look GL specific, so this is likely not the
source of the problem.

Michael Surette

unread,
Dec 13, 2016, 9:36:00 AM12/13/16
to fltkg...@googlegroups.com
On Tue, Dec 13, 2016 at 3:25 AM, Duncan Gibson wrote:

> and when it comes to the actual build, compiling works, but
> the linking phase fails...
>
> $ make
> [ 20%] Generating CubeViewUI.cxx, CubeViewUI.h
> Scanning dependencies of target CubeView
> [ 40%] Building CXX object CMakeFiles/CubeView.dir/CubeMain.cxx.o
> [ 60%] Building CXX object CMakeFiles/CubeView.dir/CubeView.cxx.o
> [ 80%] Building CXX object CMakeFiles/CubeView.dir/CubeViewUI.cxx.o
> [100%] Linking CXX executable CubeView.app/Contents/MacOS/CubeView
> Undefined symbols for architecture x86_64:
> "_CFAbsoluteTimeGetCurrent", referenced from:
> Fl::add_timeout(double, void (*)(void*), void*) in
> libfltk.a(Fl_cocoa.mm.o)
> Fl::repeat_timeout(double, void (*)(void*), void*) in
> libfltk.a(Fl_cocoa.mm.o)
> "_CFAttributedStringCreate", referenced from:
> Fl_Font_Descriptor::Fl_Font_Descriptor(char const*, int) in
> libfltk.a(fl_font.cxx.o)
> Fl_Quartz_Graphics_Driver::text_extents(char const*, int, int&,
> int&, int&, int&) in libfltk.a(fl_font.cxx.o)

Perhaps this link will help.

https://groups.google.com/forum/#!topic/fltkgeneral/VE1hXN_cg24

Mike

Duncan Gibson

unread,
Dec 13, 2016, 10:09:35 AM12/13/16
to fltkg...@googlegroups.com
Michael wrote:
> Perhaps this link will help.
> https://groups.google.com/forum/#!topic/fltkgeneral/VE1hXN_cg24

Yes, and no.

I had already seen the "-framework Cocoa" in the fltk-config output
and had tried some ad hoc macro combinations along the lines of:

dont_remember(mytarget -Llib -lfltk -framework Cocoa)

but I (a) couldn't get it to work, and (b) wanted a generic solution.

What would be best would be to embed fltk-config in CMakeLists.txt
and let fltk-config provide the values needed to compile / link etc.

D.

Albrecht Schlosser

unread,
Dec 13, 2016, 1:18:39 PM12/13/16
to fltkg...@googlegroups.com
On 13.12.2016 16:06 Duncan Gibson wrote:
> Michael wrote:
>> Perhaps this link will help.
>> https://groups.google.com/forum/#!topic/fltkgeneral/VE1hXN_cg24
>
> Yes, and no.
>
> I had already seen the "-framework Cocoa" in the fltk-config output
> and had tried some ad hoc macro combinations along the lines of:
>
> dont_remember(mytarget -Llib -lfltk -framework Cocoa)
>
> but I (a) couldn't get it to work, and (b) wanted a generic solution.

In file CMake/setup.cmake we have:

if(APPLE)
# ...
if(OPTION_APPLE_X11)
# ...
elseif(OPTION_APPLE_SDL)
# ...
else()
set(__APPLE_QUARTZ__ 1)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework
Cocoa")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}
-framework Cocoa")
endif(OPTION_APPLE_X11)
endif(APPLE)

So I guess what you'd need is:

if(APPLE)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework
Cocoa")
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS}
-framework Cocoa")
endif(APPLE)

You may omit if(APPLE)/endif(APPLE) if your target is only MacOS.

That said, I don't know why this is not exported by the use of the CMake
script (if it isn't) or how you would import it, sorry. But these four
lines above should help if it's the only missing framework.

Basically

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa")

should suffice (i.e. you don't need CMAKE_SHARED_LINKER_FLAGS if your
project doesn't build shared libs).

> What would be best would be to embed fltk-config in CMakeLists.txt
> and let fltk-config provide the values needed to compile / link etc.

I wouldn't recommend to do that. Either use CMake or fltk-config, but
don't mix both. YMMV.

Duncan Gibson

unread,
Jan 3, 2017, 7:22:13 AM1/3/17
to fltkg...@googlegroups.com
Sorry, but I've been away from my work system and unable to reply...
Midway through the 30-day CLion evaluation period, the JetBrains
team asked for some feedback, and I suggested the creation of a
basic architecture aware CMake template. They provided the links:

https://groups.google.com/forum/#!topic/fltkgeneral/P-6pXV34TWQ and
https://www.jetbrains.com/help/clion/2016.3/quick-cmake-tutorial.html

and these, along with the FLTK README.CMake file were sufficient to
create the working CMakeLists.txt file that had eluded me before.
What follows is what I came up with, but is basically what you said:

CMAKE_MINIMUM_REQUIRED(VERSION 3.0.0)

PROJECT(fltest)

set(CPPFILES
CheckVisitor.cpp
...
fltest.cpp
)

set(FLTK_DIR /Users/duncan/fltk-1.3.4-1/build)
find_package(FLTK REQUIRED NO_MODULE)
include_directories(${FLTK_INCLUDE_DIRS}
/Users/duncan/src/iitas-esa/include)
link_directories(/Users/duncan/src/iitas-esa/lib)

set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -framework Cocoa")

add_executable(fltest MACOSX_BUNDLE ${CPPFILES})

target_link_libraries(fltest tas_arm_support tas_arm Step fltk)



Now that I have something working, I can look at the conditional
stuff for building on Linux and Windows.

Thanks for the help
D.




This message and any attachments are intended for the use of the addressee or addressees only.

Fenghe Xu

unread,
Jun 10, 2019, 10:28:53 AM6/10/19
to fltk.general
Reply all
Reply to author
Forward
0 new messages