Error linking FLTK-Cairo program

13 views
Skip to first unread message

david allen

unread,
Nov 25, 2025, 2:08:26 PM (2 days ago) Nov 25
to fltk.general
My application will display its graphic output via CairoWindow. The user can produce a pdf via FLTK's native file browser and Cairo's backend capabilities.

I wrote a prototype cmake project with all .cpp files in a single directory. Works perfectly.

Looking ahead to project expansion, I restructured the project by allocating the .cpp files between two directories. One directory contains file with `main()` the other contains all the other files. This version will not link. I use Linux Mint and a recent version of FLTK.

I'm hoping someone can take a look at this. I know it is a cmake problem rather than a FLTK problem, but I don't know where else to turn.

Unzip the attached zip file. To see the working version:
```
cd trial/1
cmake -B build
cmake --build build
build/dual-output/trial-01
```
To see my problem:
```
cd ../2
cmake -B build
cmake --build build
```

trial.zip

Albrecht Schlosser

unread,
Nov 26, 2025, 6:39:29 AM (yesterday) Nov 26
to fltkg...@googlegroups.com
On 11/25/25 20:08 david allen wrote:
Looking ahead to project expansion, I restructured the project by allocating the .cpp files between two directories. One directory contains file with `main()` the other contains all the other files. This version will not link. I use Linux Mint and a recent version of FLTK.

In my tests it does not even *compile* which is important. Please post real error messages. Even if you posted a full (way too large) build environment, the error messages may be different on your system than anywhere else. Maybe your project works on someone's build environment.


I'm hoping someone can take a look at this. I know it is a cmake problem rather than a FLTK problem, but I don't know where else to turn.

Indeed, it's a CMake problem. cairo-code/CMakeLists.txt is missing the include directories for FLTK which can be added easily. For me your demo /2/ compiles and links fine with this modification:
target_include_directories(cairo-code PUBLIC 
  .
  ${FLTK_INCLUDE_DIRS}  # <<< added this line <<<
  )

... although in the "modern CMake" version you would replace these lines altogether with a single line:
target_link_libraries(cairo-code PUBLIC fltk::fltk)

Note: I tested both versions successfully.

BTW: in
  find_package(FLTK    REQUIRED NO_MODULE CONFIG)

the keyword NO_MODULE is redundant, it's a synonym for CONFIG.

david allen

unread,
Nov 26, 2025, 11:31:12 AM (yesterday) Nov 26
to fltk.general
Thanks so much for considering my problem. I apologize for not including the error message.
I implemented your suggestions, but the error message remained the same:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/david/trial/2/build
david@gram:~/trial/2$ cmake --build build
[ 12%] Building CXX object app/CMakeFiles/trial-02.dir/dual.cpp.o
[ 25%] Linking CXX executable trial-02
/usr/bin/ld: cannot find -l../cairo-code: No such file or directory
collect2: error: ld returned 1 exit status
gmake[2]: *** [app/CMakeFiles/trial-02.dir/build.make:126: app/trial-02] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:116: app/CMakeFiles/trial-02.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2

I also tried configuring with Ninga. The error message was more verbose but the same conclusion.

Albrecht Schlosser

unread,
Nov 26, 2025, 12:10:23 PM (yesterday) Nov 26
to fltkg...@googlegroups.com
On 11/26/25 17:31 david allen wrote:
Thanks so much for considering my problem. I apologize for not including the error message.
I implemented your suggestions, 

Which suggestions? There were two related to the problem and one unrelated ("BTW: ...").

Please be more specific, otherwise helping you becomes tedious (and people trying to help may lose their patience).


but the error message remained the same:
-- Configuring done
-- Generating done
-- Build files have been written to: /home/david/trial/2/build
david@gram:~/trial/2$ cmake --build build
[ 12%] Building CXX object app/CMakeFiles/trial-02.dir/dual.cpp.o
[ 25%] Linking CXX executable trial-02
/usr/bin/ld: cannot find -l../cairo-code: No such file or directory
collect2: error: ld returned 1 exit status
gmake[2]: *** [app/CMakeFiles/trial-02.dir/build.make:126: app/trial-02] Error 1
gmake[1]: *** [CMakeFiles/Makefile2:116: app/CMakeFiles/trial-02.dir/all] Error 2
gmake: *** [Makefile:136: all] Error 2

OK, some things to do:

(this is general advice, but please read my post until the end. I think I got the solution, see below)

- make sure that you cleared the entire build directory before you start (maybe you did, but you didn't say so)
- rerun cmake -B ... (configure pass). Save the log for later requests.
- rerun the build by doing:
  $ VERBOSE=1 cmake --build build
- again, save the log.
- Now check the log and/or whether the library was built. In my case it's:
  $ ls -l build/cairo-code/*.a
  -rw-r--r-- 1 albrecht albrecht 39192 Nov 26 17:40 build/cairo-code/libcairo-code.a

This file should exist. If not, please post both logs you saved earlier (recommended in a zip file).


I also tried configuring with Ninga. The error message was more verbose but the same conclusion.

"the same conclusion" is not very helpful. Even if this seems to be "the same" for you someone else (like me) might read more out of it.

That all said ... I'm sorry, I forgot to post yet another tiny modification :-(

See comment in full file shown below:
$ cat 2/app/CMakeLists.txt

add_executable(trial-02 
  dual.cpp
  )

target_include_directories(trial-02 PUBLIC
  ${FLTK_INCLUDE_DIRS}
  ${CAIRO_INCLUDE_DIRS}
  ../cairo-code
  )

target_link_libraries(trial-02 PUBLIC
  cairo-code                           # <<< needs to be changed as shown here !!!
  Threads::Threads
  fltk
  ${CAIRO_LIBRARIES}
  )

# set install directories
install(TARGETS trial-02 RUNTIME DESTINATION ~/.local/bin)

[end of file]

This missing change explains the error message you posted above because you used syntax for a directory (../cairo-code) whereas this statement requires libraries. In many cases you can use target names like `cairo-code` which is the library target you built in the subdirectory.

While we're at it: you may also want to replace `fltk` with `fltk::fltk` which is the "modern CMake" notation of imported targets (libraries), just as in `Threads::Threads` [1], whereas `${CAIRO_LIBRARIES}` may be anything that can be interpreted as one or more (i.e. a list of) libraries.

HTH
Albrecht

[1] if the imported library supports this (FLTK does, if it is used in CONFIG mode)

david allen

unread,
2:42 PM (9 hours ago) 2:42 PM
to fltk.general
This solved the problem:
target_link_libraries(trial-02 PUBLIC
  cairo-code               # <<< needs to be changed as shown here !!!
  Threads::Threads
  fltk
  ${CAIRO_LIBRARIES}
  )
I have been obtuse, but I have learned a lot about cmake and how to better make requests. Thank you very much.
Reply all
Reply to author
Forward
0 new messages