Objective-C++使用 cmake 构建库时文件未链接到 Cocoa

发布于 2024-12-11 02:03:00 字数 6947 浏览 1 评论 0原文

我的 API 中有一个目标 c++ 文件。该库构建得很好,但是当我去编译使用该库的应用程序时,我的所有 Objective-C 调用都会出现链接器错误:

Undefined symbols:
  "_objc_msgSend", referenced from:
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
     (maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
  "_NSPasteboardTypeString", referenced from:
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
  "_OBJC_CLASS_$_NSArray", referenced from:
      objc-class-ref-to-NSArray in libAgui.a(OSXClipboard.mm.o)
  "_OBJC_CLASS_$_NSString", referenced from:
      objc-class-ref-to-NSString in libAgui.a(OSXClipboard.mm.o)
  "_objc_msgSend_fixup", referenced from:
      l_objc_msgSend_fixup_alloc in libAgui.a(OSXClipboard.mm.o)
      l_objc_msgSend_fixup_release in libAgui.a(OSXClipboard.mm.o)
     (maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
  "_OBJC_CLASS_$_NSAutoreleasePool", referenced from:
      objc-class-ref-to-NSAutoreleasePool in libAgui.a(OSXClipboard.mm.o)
  "_NSStringPboardType", referenced from:
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
  "_OBJC_CLASS_$_NSPasteboard", referenced from:
      objc-class-ref-to-NSPasteboard in libAgui.a(OSXClipboard.mm.o)
ld: symbol(s) not found

我的目标是不必每次使用 gui lib 构建应用程序时都链接到 cocoa。

这是 cmake 文件:

cmake_minimum_required(VERSION 2.6)

project(agui)

OPTION(WANT_SHARED "Build agui and the backend as a shared library" OFF) 

OPTION(WANT_ALLEGRO5_BACKEND "Build the Allegro 5 backend" ON) 

if(APPLE AND NOT IPHONE)
    set(MACOSX 1)
endif(APPLE AND NOT IPHONE)

if(MACOSX)
ADD_DEFINITIONS("-x objective-c++")
endif(MACOSX)

if(MACOSX)
find_library(COCOA_LIBRARY Cocoa)
endif(MACOSX)

set(CLIPBOARD_SOURCE)

if(WIN32)
set(CLIPBOARD_SOURCE src/Agui/Clipboard/WinClipboard.cpp)
endif(WIN32)

if(MACOSX)
set(CLIPBOARD_SOURCE src/Agui/Clipboard/OSXClipboard.mm)
endif(MACOSX)


set(AGUI_SOURCES
    ${CLIPBOARD_SOURCE}
    src/Agui/Clipboard/Clipboard.cpp
    src/Agui/ActionEvent.cpp
    src/Agui/ActionListener.cpp
    src/Agui/BaseTypes.cpp
    src/Agui/BlinkingEvent.cpp
    src/Agui/BorderLayout.cpp
    src/Agui/Color.cpp
    src/Agui/Dimension.cpp
    src/Agui/EmptyWidget.cpp
    src/Agui/EventArgs.cpp
    src/Agui/FlowLayout.cpp
    src/Agui/FocusListener.cpp
    src/Agui/FocusManager.cpp
    src/Agui/Font.cpp
    src/Agui/FontLoader.cpp
    src/Agui/Graphics.cpp
    src/Agui/GridLayout.cpp
    src/Agui/Gui.cpp
    src/Agui/Image.cpp
    src/Agui/ImageLoader.cpp
    src/Agui/Input.cpp
    src/Agui/KeyboardListener.cpp
    src/Agui/Layout.cpp
    src/Agui/MouseListener.cpp
    src/Agui/Point.cpp
    src/Agui/Rectangle.cpp
    src/Agui/ResizableText.cpp
    src/Agui/ResizableBorderLayout.cpp
    src/Agui/SelectionListener.cpp
    src/Agui/TopContainer.cpp
    src/Agui/Widget.cpp
    src/Agui/WidgetListener.cpp
    src/Agui/Widgets/Button/Button.cpp
    src/Agui/Widgets/Button/ButtonListener.cpp
    src/Agui/Widgets/CheckBox/CheckBox.cpp
    src/Agui/Widgets/CheckBox/CheckBoxListener.cpp
    src/Agui/Widgets/DropDown/DropDown.cpp
    src/Agui/Widgets/DropDown/DropDownListener.cpp
    src/Agui/Widgets/Frame/Frame.cpp
    src/Agui/Widgets/Frame/FrameListener.cpp
    src/Agui/Widgets/Label/Label.cpp
    src/Agui/Widgets/Label/LabelListener.cpp
    src/Agui/Widgets/ListBox/ListBox.cpp
    src/Agui/Widgets/ListBox/ListBoxListener.cpp
    src/Agui/Widgets/RadioButton/RadioButton.cpp
    src/Agui/Widgets/RadioButton/RadioButtonListener.cpp
    src/Agui/Widgets/RadioButton/RadioButtonGroup.cpp
    src/Agui/Widgets/ScrollBar/HScrollBar.cpp
    src/Agui/Widgets/ScrollBar/HScrollBarListener.cpp
    src/Agui/Widgets/ScrollBar/VScrollBar.cpp
    src/Agui/Widgets/ScrollBar/VScrollBarListener.cpp
    src/Agui/Widgets/ScrollPane/ScrollPane.cpp
    src/Agui/Widgets/Slider/Slider.cpp
    src/Agui/Widgets/Slider/SliderListener.cpp
    src/Agui/Widgets/Tab/Tab.cpp
    src/Agui/Widgets/Tab/TabbedPane.cpp
    src/Agui/Widgets/Tab/TabbedPaneListener.cpp
    src/Agui/Widgets/TextBox/TextBox.cpp
    src/Agui/Widgets/TextBox/TextBoxListener.cpp
    src/Agui/Widgets/TextBox/ExtendedTextBox.cpp
    src/Agui/Widgets/TextField/TextField.cpp
    src/Agui/Widgets/TextField/TextFieldListener.cpp
    )

set(ALLEGRO5_BACKEND_SOURCES
    src/Agui/Backends/Allegro5/Allegro5Font.cpp
    src/Agui/Backends/Allegro5/Allegro5FontLoader.cpp
    src/Agui/Backends/Allegro5/Allegro5Graphics.cpp
    src/Agui/Backends/Allegro5/Allegro5Image.cpp
    src/Agui/Backends/Allegro5/Allegro5ImageLoader.cpp
    src/Agui/Backends/Allegro5/Allegro5Input.cpp
    )

    include_directories (./include) 
if(WANT_SHARED)
  add_library(agui SHARED ${AGUI_SOURCES})

  if(MACOSX)
  target_link_libraries (agui ${COCOA_LIBRARY})
  endif(MACOSX)

  set_target_properties(agui PROPERTIES DEFINE_SYMBOL "AGUI_BUILD")
  if(WANT_ALLEGRO5_BACKEND)
    add_library(agui_allegro5 SHARED ${ALLEGRO5_BACKEND_SOURCES})
  set_target_properties(agui_allegro5 PROPERTIES DEFINE_SYMBOL "AGUI_BACKEND_BUILD")
  target_link_libraries (agui_allegro5 agui)
  endif()
else()
  add_library(agui STATIC ${AGUI_SOURCES})

  if(MACOSX)
  target_link_libraries (agui ${COCOA_LIBRARY})
  endif(MACOSX)

    if(WANT_ALLEGRO5_BACKEND)
    add_library(agui_allegro5 STATIC ${ALLEGRO5_BACKEND_SOURCES})
   set_target_properties(agui_allegro5 PROPERTIES COMPILE_DEFINITIONS "ALLEGRO_STATICLINK")
  endif()
endif()

我告诉 cmake 查找并链接到 cocoa,但它似乎没有这样做。

谢谢

I have a file in my API that is objective c++. The library builds fine but when I go to compile an application that uses the library I get linker errors for all my objective-c calls:

Undefined symbols:
  "_objc_msgSend", referenced from:
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
     (maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
  "_NSPasteboardTypeString", referenced from:
      agui::OSXClipboard::paste()     in libAgui.a(OSXClipboard.mm.o)
  "_OBJC_CLASS_$_NSArray", referenced from:
      objc-class-ref-to-NSArray in libAgui.a(OSXClipboard.mm.o)
  "_OBJC_CLASS_$_NSString", referenced from:
      objc-class-ref-to-NSString in libAgui.a(OSXClipboard.mm.o)
  "_objc_msgSend_fixup", referenced from:
      l_objc_msgSend_fixup_alloc in libAgui.a(OSXClipboard.mm.o)
      l_objc_msgSend_fixup_release in libAgui.a(OSXClipboard.mm.o)
     (maybe you meant: l_objc_msgSend_fixup_release, l_objc_msgSend_fixup_alloc )
  "_OBJC_CLASS_$_NSAutoreleasePool", referenced from:
      objc-class-ref-to-NSAutoreleasePool in libAgui.a(OSXClipboard.mm.o)
  "_NSStringPboardType", referenced from:
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
      agui::OSXClipboard::copy(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in libAgui.a(OSXClipboard.mm.o)
  "_OBJC_CLASS_$_NSPasteboard", referenced from:
      objc-class-ref-to-NSPasteboard in libAgui.a(OSXClipboard.mm.o)
ld: symbol(s) not found

My goal is not to have to link to cocoa every time I build an app with my gui lib.

Here is the cmake file:

cmake_minimum_required(VERSION 2.6)

project(agui)

OPTION(WANT_SHARED "Build agui and the backend as a shared library" OFF) 

OPTION(WANT_ALLEGRO5_BACKEND "Build the Allegro 5 backend" ON) 

if(APPLE AND NOT IPHONE)
    set(MACOSX 1)
endif(APPLE AND NOT IPHONE)

if(MACOSX)
ADD_DEFINITIONS("-x objective-c++")
endif(MACOSX)

if(MACOSX)
find_library(COCOA_LIBRARY Cocoa)
endif(MACOSX)

set(CLIPBOARD_SOURCE)

if(WIN32)
set(CLIPBOARD_SOURCE src/Agui/Clipboard/WinClipboard.cpp)
endif(WIN32)

if(MACOSX)
set(CLIPBOARD_SOURCE src/Agui/Clipboard/OSXClipboard.mm)
endif(MACOSX)


set(AGUI_SOURCES
    ${CLIPBOARD_SOURCE}
    src/Agui/Clipboard/Clipboard.cpp
    src/Agui/ActionEvent.cpp
    src/Agui/ActionListener.cpp
    src/Agui/BaseTypes.cpp
    src/Agui/BlinkingEvent.cpp
    src/Agui/BorderLayout.cpp
    src/Agui/Color.cpp
    src/Agui/Dimension.cpp
    src/Agui/EmptyWidget.cpp
    src/Agui/EventArgs.cpp
    src/Agui/FlowLayout.cpp
    src/Agui/FocusListener.cpp
    src/Agui/FocusManager.cpp
    src/Agui/Font.cpp
    src/Agui/FontLoader.cpp
    src/Agui/Graphics.cpp
    src/Agui/GridLayout.cpp
    src/Agui/Gui.cpp
    src/Agui/Image.cpp
    src/Agui/ImageLoader.cpp
    src/Agui/Input.cpp
    src/Agui/KeyboardListener.cpp
    src/Agui/Layout.cpp
    src/Agui/MouseListener.cpp
    src/Agui/Point.cpp
    src/Agui/Rectangle.cpp
    src/Agui/ResizableText.cpp
    src/Agui/ResizableBorderLayout.cpp
    src/Agui/SelectionListener.cpp
    src/Agui/TopContainer.cpp
    src/Agui/Widget.cpp
    src/Agui/WidgetListener.cpp
    src/Agui/Widgets/Button/Button.cpp
    src/Agui/Widgets/Button/ButtonListener.cpp
    src/Agui/Widgets/CheckBox/CheckBox.cpp
    src/Agui/Widgets/CheckBox/CheckBoxListener.cpp
    src/Agui/Widgets/DropDown/DropDown.cpp
    src/Agui/Widgets/DropDown/DropDownListener.cpp
    src/Agui/Widgets/Frame/Frame.cpp
    src/Agui/Widgets/Frame/FrameListener.cpp
    src/Agui/Widgets/Label/Label.cpp
    src/Agui/Widgets/Label/LabelListener.cpp
    src/Agui/Widgets/ListBox/ListBox.cpp
    src/Agui/Widgets/ListBox/ListBoxListener.cpp
    src/Agui/Widgets/RadioButton/RadioButton.cpp
    src/Agui/Widgets/RadioButton/RadioButtonListener.cpp
    src/Agui/Widgets/RadioButton/RadioButtonGroup.cpp
    src/Agui/Widgets/ScrollBar/HScrollBar.cpp
    src/Agui/Widgets/ScrollBar/HScrollBarListener.cpp
    src/Agui/Widgets/ScrollBar/VScrollBar.cpp
    src/Agui/Widgets/ScrollBar/VScrollBarListener.cpp
    src/Agui/Widgets/ScrollPane/ScrollPane.cpp
    src/Agui/Widgets/Slider/Slider.cpp
    src/Agui/Widgets/Slider/SliderListener.cpp
    src/Agui/Widgets/Tab/Tab.cpp
    src/Agui/Widgets/Tab/TabbedPane.cpp
    src/Agui/Widgets/Tab/TabbedPaneListener.cpp
    src/Agui/Widgets/TextBox/TextBox.cpp
    src/Agui/Widgets/TextBox/TextBoxListener.cpp
    src/Agui/Widgets/TextBox/ExtendedTextBox.cpp
    src/Agui/Widgets/TextField/TextField.cpp
    src/Agui/Widgets/TextField/TextFieldListener.cpp
    )

set(ALLEGRO5_BACKEND_SOURCES
    src/Agui/Backends/Allegro5/Allegro5Font.cpp
    src/Agui/Backends/Allegro5/Allegro5FontLoader.cpp
    src/Agui/Backends/Allegro5/Allegro5Graphics.cpp
    src/Agui/Backends/Allegro5/Allegro5Image.cpp
    src/Agui/Backends/Allegro5/Allegro5ImageLoader.cpp
    src/Agui/Backends/Allegro5/Allegro5Input.cpp
    )

    include_directories (./include) 
if(WANT_SHARED)
  add_library(agui SHARED ${AGUI_SOURCES})

  if(MACOSX)
  target_link_libraries (agui ${COCOA_LIBRARY})
  endif(MACOSX)

  set_target_properties(agui PROPERTIES DEFINE_SYMBOL "AGUI_BUILD")
  if(WANT_ALLEGRO5_BACKEND)
    add_library(agui_allegro5 SHARED ${ALLEGRO5_BACKEND_SOURCES})
  set_target_properties(agui_allegro5 PROPERTIES DEFINE_SYMBOL "AGUI_BACKEND_BUILD")
  target_link_libraries (agui_allegro5 agui)
  endif()
else()
  add_library(agui STATIC ${AGUI_SOURCES})

  if(MACOSX)
  target_link_libraries (agui ${COCOA_LIBRARY})
  endif(MACOSX)

    if(WANT_ALLEGRO5_BACKEND)
    add_library(agui_allegro5 STATIC ${ALLEGRO5_BACKEND_SOURCES})
   set_target_properties(agui_allegro5 PROPERTIES COMPILE_DEFINITIONS "ALLEGRO_STATICLINK")
  endif()
endif()

I'm telling cmake to find and link to cocoa but it does not seem to do it.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海夜未眠 2024-12-18 02:03:00

我在编译 Objective-C++ 应用程序时也遇到了链接器错误。我需要将其添加到我的 CMakeLists.txt 中:
<代码>
add_definitions("-x Objective-c++") # 正如你已经拥有的
设置(CMAKE_EXE_LINKER_FLAGS“-framework ServiceManagement -framework Foundation -framework Cocoa”)

如果没有这个,__OBJC__就不会被定义。

I had linker errors when compiling an objective-c++ app too. I needed to add this to my CMakeLists.txt:

add_definitions("-x objective-c++") # as you already have
set(CMAKE_EXE_LINKER_FLAGS "-framework ServiceManagement -framework Foundation -framework Cocoa")

Without this, the __OBJC__ wasn't defined.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文