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

xvw2m8pv  于 2023-04-30  发布在  其他
关注(0)|答案(1)|浏览(319)

我一直在尝试在我的系统上安装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]

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

xesrikrc

xesrikrc1#

我认为您的问题可能是围绕将FLTK链接到项目中的方法。
在您的第一个有效的示例中,编译器可能正在编译源代码。FLTK工具包的cpp文件作为项目的一部分。这样,编译器就能够定位Fl_Window等的定义。或者,你有一个预编译的静态库,你链接到你的编译工具链的一部分。
在第二个失败的(CMake)示例中,我相信链接器无法找到您试图用target_link_libraries(fltk_1 ${FLTK_LIBRARIES})指定的库。您是否能够注销变量${FLTK_LIBRARIES}的解析值并确认它指向预编译库?

相关问题