安装 FLTK 以与 CLion 和 CMake 一起使用时出现链接器错误

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

提问人:Benny Frog 提问时间:4/21/2023 最后编辑:Benny Frog 更新时间:4/22/2023 访问量:106

问:

我一直在尝试在我的系统上安装 FLTK。我遵循了文档中的所有步骤,并且运行良好。当我使用编译器编译以下代码时:(我复制了 FLTK 网站上的代码)

#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();
}

它工作得很好。 但是,当我使用 CMake 时,CMakeLists.txt 如下所示:

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})

它通过了 CMake 的内部检查,但链接器提供了一个错误:

====================[ 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.

我尝试使用不同的与 CMake 相关的 IDE(如 Visual Studio Code),但失败了,错误消息基本上都有以下几行:

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

我想按照 FLTK 网站上的说明编译和运行代码。

[修订版 1]日志记录信息 我刚刚将这一行添加到CMakeLists.txt(如上所述):

    message(${FLTK_LIBRARIES})

它产生以下内容:

    /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]

我应该采取哪些进一步的措施?

C++ macOS cmake、 fltk

评论

1赞 Tsyvarev 4/22/2023
该变量应包含与 fltk 相关的库和框架的所有绝对路径。您可以打印此变量的值(使用命令)并找到 .根据传递的参数,CMake 在 .但根据错误消息,该路径不存在......FLTK_LIBRARIESmessagefltk.framework-F/usr/local/usr/local/fltk.framework

答:

0赞 aardvarkk 4/21/2023 #1

我认为您的问题可能在于将 FLTK 链接到项目的方法。

在第一个有效的示例中,编译器可能正在编译 FLTK 工具包的源 .cpp 文件作为项目的一部分。这样,编译器就能够找到 等的定义。要么是这样,要么你有一个预编译的静态库,你要链接到它作为编译工具链的一部分。Fl_Window

在失败的第二个 (CMake) 示例中,我相信链接器无法找到您尝试用 指定的库。您是否能够注销变量的解析值并确认它指向预编译库?target_link_libraries(fltk_1 ${FLTK_LIBRARIES})${FLTK_LIBRARIES}

评论

0赞 Benny Frog 4/22/2023
我已经编辑了问题,以便您现在可以看到变量 ${FLTK_LIBRARIES} 的值。但是,我不确定这意味着什么。提前致谢。
0赞 Albrecht Schlosser 4/23/2023
${FLTK_LIBRARIES} 的内容是以“-framework”开头的一行。缺少很多分隔符(空格)。我在下面发布了每个项目在新行上的外观(希望它能在此评论中正确显示)。''' -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 ''' 对不起,我对 CLion 没有帮助。
0赞 Albrecht Schlosser 4/23/2023
注意:发布后,我注意到代码片段中的所有换行符都已转换为空格,但这没关系。
0赞 aardvarkk 4/25/2023
@BennyFrog 您是否检查过文件是否存在?/usr/local/fltk.framework
0赞 Benny Frog 4/29/2023
是的,它存在。@aardvarkk