Linker error with installing FLTK to use with CLion as well as CMake

158 Views Asked by At

I have been trying to install FLTK on my system. I followed all steps in the documentation and works perfectly. When I use a compiler to compile the following code: (I duplicated the one on the FLTK website)

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Box.H>

int main(int argc, char **argv) {
    Fl_Window *window = new Fl_Window(340,180);
    Fl_Box *box = new Fl_Box(20,40,300,100,"Hello, World!");
    box->box(FL_UP_BOX);
    box->labelfont(FL_BOLD+FL_ITALIC);
    box->labelsize(36);
    box->labeltype(FL_SHADOW_LABEL);
    window->end();
    window->show(argc, argv);
    return Fl::run();
}

It works perfectly. However, when I used CMake, with CMakeLists.txt as follows:

cmake_minimum_required(VERSION 3.25)
project(fltk_1)

set(CMAKE_CXX_STANDARD 17)

add_executable(fltk_1 main.cpp)
find_package(FLTK REQUIRED COMPONENTS FLTK Fl_Window Fl_Box)

include_directories(${FLTK_INCLUDE_DIR})
# Note: a target should be already defined using 'add_executable' or 'add_library'
target_link_libraries(fltk_1 ${FLTK_LIBRARIES})

It passed CMake's internal inspection all right, but the linker provided an error:

====================[ Build | fltk_1 | Debug ]==================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/oliviawang/CLionProjects/fltk_1/cmake-build-debug --target fltk_1 -j 3
[1/1] Linking CXX executable fltk_1
FAILED: fltk_1 
: && /Library/Developer/CommandLineTools/usr/bin/c++ -Werror -g -isysroot /Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk -Wl,-search_paths_first -Wl,-headerpad_max_install_names  CMakeFiles/fltk_1.dir/main.cpp.o -o fltk_1 -F/usr/local -framework Carbon -framework Cocoa -framework ApplicationServices -lz  /usr/local/lib/libfltk_images.a  /usr/local/lib/libfltk_forms.a  /usr/local/lib/libfltk_gl.a  -Xlinker -framework -Xlinker OpenGL  -Xlinker -framework -Xlinker fltk && :
ld: framework not found fltk
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

I tried to use different CMake-related IDEs(Like Visual Studio Code), but failed, with the error message basically all having these lines:

ld: framework not found fltk
clang: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

I want to get the code to compile and run as instructed on the FLTK website.

[REVISION 1] Logging info I have just added this line to CMakeLists.txt(mentioned above):

    message(${FLTK_LIBRARIES})

and it yields the following contents:

    /Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_MAKE_PROGRAM=/Applications/CLion.app/Contents/bin/ninja/mac/ninja -DCMAKE_CXX_FLAGS=-Werror -G Ninja -S /Users/oliviawang/CLionProjects/fltk_1 -B /Users/oliviawang/CLionProjects/fltk_1/cmake-build-debug
-framework Carbon -framework Cocoa -framework ApplicationServices -lz/usr/local/lib/libfltk_images.a/usr/local/lib/libfltk_forms.a/usr/local/lib/libfltk_gl.a/Library/Developer/CommandLineTools/SDKs/MacOSX12.3.sdk/System/Library/Frameworks/OpenGL.framework/usr/local/fltk.framework
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/oliviawang/CLionProjects/fltk_1/cmake-build-debug

[Finished]

What further measures should I take?

1

There are 1 best solutions below

5
aardvarkk On

I think your issue is likely around the method in which you're linking FLTK into your project.

In your first example that works, the compiler is likely compiling the source .cpp files for the FLTK toolkit as part of your project. That way, the compiler is able to locate the definitions for Fl_Window, etc. Either that, or you have a precompiled static library that you're linking to as part of your compilation toolchain.

In your second (CMake) example that fails, I believe the linker is unable to find the library you're trying to specify with target_link_libraries(fltk_1 ${FLTK_LIBRARIES}). Are you able to log out the resolved value of the variable ${FLTK_LIBRARIES} and confirm that it points to a precompiled library?