Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot build project with CMake (add_library cannot create ALIAS target "fltk" because target "fltk::fltk" is imported but not globally visible) #970

Closed
sprkrd opened this issue May 7, 2024 · 7 comments
Assignees
Labels
CMake CMake bug or needs CMake support fixed The issue or PR was fixed.

Comments

@sprkrd
Copy link

sprkrd commented May 7, 2024

Describe the bug

When setting up a CMake project that uses FLTK, the following error messages pop up during the CMake configuration process:

CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk" because target "fltk::fltk"
  is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:105 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk-shared" because target
  "fltk::fltk-shared" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:106 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:94 (add_executable):
  add_executable cannot create ALIAS target "fluid" because target
  "fltk::fluid" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:108 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:94 (add_executable):
  add_executable cannot create ALIAS target "fltk-options" because target
  "fltk::options" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:111 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk_forms" because target
  "fltk::forms" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:115 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk_forms-shared" because target
  "fltk::forms-shared" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:116 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk_gl" because target "fltk::gl"
  is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:115 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk_gl-shared" because target
  "fltk::gl-shared" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:116 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk_images" because target
  "fltk::images" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:115 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)


CMake Error at /home/asuarez/.local/share/fltk/FLTKConfig.cmake:96 (add_library):
  add_library cannot create ALIAS target "fltk_images-shared" because target
  "fltk::images-shared" is imported but not globally visible.
Call Stack (most recent call first):
  /home/asuarez/.local/share/fltk/FLTKConfig.cmake:116 (_fltk_make_alias)
  CMakeLists.txt:16 (find_package)

I have to disable (comment out) lines 87 to 119 from the FLTKConfig.cmake file in order for CMake to succeed.

To Reproduce

I've followed the instructions from README.CMake.txt to build FLTK and set up the project. Here you'll find a FLTK project that used to work with the pre-release of FLTK v1.4 (like a year ago) and no longer works (unless I comment out lines 87 to 119 from FLTKConfig.cmake). I've adapted the project to the new changes described in README.CMake.txt (e.g. linking the executables to fltk::fltk-shared instead of fltk_SHARED).

Expected behavior

Successful configuration and compilation of the project.

Screenshots

N/A

FLTK Version

  • Version: 1.4.0
  • Installed from source using CMake.
  • Downloaded and self-built from: FLTK Git repo: https://github.com/fltk/fltk
  • If from Git, branch: master
  • If from Git, commit: 2af1da4

FLTK Configure / Build Options

Procedure for building:

mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:=$HOME/.local -DFLTK_BUILD_SHARED_LIBS:=ON -DCMAKE_BUILD_TYPE:=RELEASE
make -j6 install

The example project is built like so:

(create and cd to build)
cmake .. -DFLTK_DIR:=$HOME/.local
make -j6

Operating System / Platform:

Please be as precise as possible, e.g. "Linux: Ubuntu 20.04"

  • OS: Ubuntu 20.04
  • CMake version: 3.16.3

Linux/Unix Runtime, if applicable:

N/A

Additional context

N/A

@Albrecht-S
Copy link
Member

@sprkrd Thanks for the report, but I can't reproduce the error messages here. Please make sure that you have built and installed a fresh copy of FLTK and that the install directory is empty before you install FLTK.

I tested to build your project directly from the FLTK build and from the installation folder. Both worked w/o issues.

BTW (this may be helpful to avoid other issues): current docs (in README.CMake.txt) recommend to set FLTK_DIR as a cache variable. This prevents false find results in case the CMake procedure would find some older instances of FLTK:

# change this to your fltk build directory
set(FLTK_DIR "/path/to/fltk"
    CACHE FILEPATH "FLTK installation or build directory")
find_package(FLTK CONFIG REQUIRED)

I strongly recommend to do this. One advantage is that you can inspect the cache variable with tools like cmake-gui. You could also output FLTK_DIR after the find_package() command.

Sidenote: NO_MODULE is an alias name of CONFIG and is not required in your project. It shouldn't do any harm though.

... unless I comment out lines 87 to 119 from FLTKConfig.cmake ...

Although I can't reproduce this I remember that I had similar issues in the past but I couldn't find out yet what caused this. If you build from scratch and it doesn't work, then an easier workaround is to change only line 35 in CMake/FLTKConfig.cmake.in:

  set(FLTK_CREATE_COMPATIBILITY_ALIASES FALSE)  ## changed from TRUE to FALSE

The generated aliases are only useful if you still use old FLTK target names like fltk_shared but you are using the new namespaced target name fltk::fltk-shared, hence your project doesn't need the compatibility aliases.

That said, I wonder what caused this error in your build. It would be very helpful if you could provide CMake logs and more info about your build process (if it still fails w/o editing CMake/FLTKConfig.cmake.in). Thank you.

Could it be possible that CMake finds another (older) instance of FLTK on your system?

@Albrecht-S Albrecht-S self-assigned this May 7, 2024
@Albrecht-S Albrecht-S added the CMake CMake bug or needs CMake support label May 7, 2024
@sprkrd
Copy link
Author

sprkrd commented May 9, 2024

@Albrecht-S Thank you for your rapid response!

Actually, I have a strong preference against setting the FLTK_DIR path in the project's CMakeLists.txt file, because I find it better that this file be agnostic as to where FLTK is installed - i.e. I don't want it to reflect FLTK's location in my particular computer. However, I've tried setting it directly in CMakeLists.txt and the problem persists. I'm pretty sure CMake is locating the correct FLTK installation. I output FLTK version to stdout whenever I execute cmake, and the reported version is 1.4 (as it should be). Like I said, the project compiles just fine when I comment out lines 87 to 119 of FLTKConfig.cmake, and it identifies the correct FLTK version (1.4, rather than the one provided by my package manager, which is much older, 1.3.something). Plus, I use features exclusive to 1.4 that are not available in the system-wide installation of FLTK.

I believe that the problem lies with CMake version. The version I use in this computer is 1.16, which should be fine according to README.CMake.txt. However, I've seen similar issues with alias exports with versions of CMake older than 1.23 in other unrelated libraries. Out of curiosity, what version of CMake are you using?

Maybe the issue could just be solved by upgrading the CMake minimum requirement? I'll come back when I have a chance to try in a environment with a newer CMake.

EDITED: Sorry! I mean't CMake 3.16, not 1.16.

@Albrecht-S
Copy link
Member

Actually, I have a strong preference against setting the FLTK_DIR path in the project's CMakeLists.txt file, because I find it better that this file be agnostic as to where FLTK is installed

That's fine, as long as you set it as a cache variable on the commandline in the first CMake invocation. Our original recommendation to set it as a simple (non-cache) CMake variable is not recommended. If it's set as a cache variable you can inspect it after the build which is very helpful if there are any doubts (as in this case).

Hint: If you don't need the system installation of FLTK I recommend to remove it just to be sure it doesn't affect the build. There are well-known issues when mixing header and binary files of self-built and system versions. However, that is only relevant later, at runtime.

I believe that the problem lies with CMake version. The version I use in this computer is 1.16 3.16, which should be fine according to README.CMake.txt ... what version of CMake are you using?

[Typo 1.16 fixed in citation, must be 3.16] For my regular builds I'm using 3.25.1 (Debian Bookworm default), and that's what I used in my tests of your project. However, I have 6 older and newer versions present as well: from 3.15.0 (currently our minimum required version) up to 3.28.1 on my Debian system and even 3.29.2 on my Windows system where I have also several installed versions I can select. It is not impossible that a particular CMake version has a certain kind of bug that's fixed later. I know of some issues that have or have not been fixed in later versions.

That said, I can test your project with CMake 3.15.0 and 3.18.0, the oldest versions I have presently available. Let's see if I can reproduce your issue, but I'd appreciate if you could test with other CMake versions as well. TIA.

Maybe the issue could just be solved by upgrading the CMake minimum requirement?

I would like to stay conservative (i.e. keep 3.15) as long as possible (in FLTK 1.4.x). FLTK 1.5 will probably require a higher version. If I can reproduce the issue with one of my older versions there are chances I can work around the issue on the FLTK side.

@sprkrd
Copy link
Author

sprkrd commented May 9, 2024

Oh, I wish I could do away with the old system FLTK installation, but unfortunately there are several packages that rely on it (ROS packages to be precise), and removing FLTK would wreak havoc on my system. No problem on my personal computers, where I can afford to install newer software.

So, I've tested the newest version of CMake (v3.29) and it works without problem in all of my projects. Therefore, it'd seem there's some quirk in older versions of CMake involving aliases.

@Albrecht-S
Copy link
Member

... removing FLTK would wreak havoc on my system ...

OK, usually it shouldn't be necessary, it should work with two parallel versions, but we heard from users that mixed versions - with unexpected failures at runtime.

I've tested the newest version of CMake (v3.29) and it works without problem in all of my projects. Therefore, it'd seem there's some quirk in older versions of CMake involving aliases.

Thanks for testing and feedback. I've been busy with other FLTK stuff in the meantime, but with this information I'll definitely test your (and maybe other) project(s) with older CMake versions to see if I can confirm - and hopefully fix - this.

I'll leave this issue open until I could test...

@Albrecht-S
Copy link
Member

Albrecht-S commented May 9, 2024

Progress report:

  • CMake 3.15.0: fails with the error messages posted by @sprkrd
  • CMake 3.16.3: fails as posted by @sprkrd
  • CMake 3.17.5: fails (tested: May 10, 2024)
  • CMake 3.18.0: works.

I'll investigate this further and try to resolve the issue. At least we know now that

  • this is a CMake bug (or incompatibility in older CMake versions), and
  • it has been fixed in CMake 3.17 or CMake 3.18.

A very easy fix would be not to create the aliases if the CMake version is lower than 3.18.0, but I'll investigate further. It's good that I can reproduce the issue.

Albrecht-S pushed a commit that referenced this issue May 13, 2024
... (add_library cannot create ALIAS target "fltk" because target
"fltk::fltk" is imported but not globally visible)

It turned out that this was due to a CMake feature that was changed in
CMake 3.18. This commit makes imported FLTK targets globally visible
for CMake versions less than 3.18.
@Albrecht-S Albrecht-S added the fixed The issue or PR was fixed. label May 13, 2024
@Albrecht-S
Copy link
Member

Commit 6c06e95 fixes this issue by promoting all imported FLTK targets to global visibility for CMake versions less than 3.18. CMake 3.18 and later versions can create alias targets for imported targets with "local visibility" and don't need this extra step.

FTR: I decided to keep the minimum CMake version at 3.15 and to fix this issue for versions < 3.18 only. This extra step will be removed when we require CMake 3.18 or higher in a later FLTK version.

@sprkrd Please confirm if this fixes the issue for you and close this issue. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CMake CMake bug or needs CMake support fixed The issue or PR was fixed.
Projects
None yet
Development

No branches or pull requests

2 participants