CMake 和 XCode:“找不到“NSObject”的接口声明”

发布于 2024-10-27 02:36:46 字数 2172 浏览 1 评论 0原文

我正在尝试使用 CMake 生成 XCode 项目,但遇到了一些问题。

CMake 生成的项目很好,但后来很明显它没有链接到 Foundation 和 UIKit 框架。我对 CMake 还很陌生,一直在努力克服这个问题,但没有成功。

CMake 输出(部分):

...
Framework Foundation found at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/Frameworks/Foundation.framework
Framework CoreGraphics found at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/Frameworks/CoreGraphics.framework
Framework UIKit found at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/Frameworks/UIKit.framework
Configuring done
Generating done

下面是我的 CMakeLists.txt 文件(由 SO 上的几个答案汇总而成):

cmake_minimum_required(VERSION 2.8)

macro(ADD_FRAMEWORK fwname appname libpath)
    find_library(FRAMEWORK_${fwname}
        NAMES ${fwname}
        PATHS ${libpath}

        PATH_SUFFIXES Frameworks
        NO_DEFAULT_PATH)
    if( ${FRAMEWORK_${fwname}} STREQUAL FRAMEWORK_${fwname}-NOTFOUND)
        MESSAGE(ERROR ": Framework ${fwname} not found")
    else()
        TARGET_LINK_LIBRARIES(${appname} ${FRAMEWORK_${fwname}})
        MESSAGE(STATUS "Framework ${fwname} found at ${FRAMEWORK_${fwname}}")
    endif()
endmacro(ADD_FRAMEWORK)

project(test)
set(NAME test)
set (libpath /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library)


file(GLOB headers *.h)
file(GLOB sources *.m)

set(CMAKE_OSX_SYSROOT iphoneos4.2)
set(CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
set(CMAKE_CXX_FLAGS "-x objective-c++")
set(CMAKE_EXE_LINKER_FLAGS
    "-framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework UIKit -framework Foundation"
)

set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.mycompany.\${PRODUCT_NAME:identifier}")
set(APP_TYPE MACOSX_BUNDLE)

add_executable(${NAME}
    ${APP_TYPE}
    ${headers}
    ${sources}
)

ADD_FRAMEWORK(Foundation ${NAME} ${libpath})
ADD_FRAMEWORK(CoreGraphics ${NAME} ${libpath})
ADD_FRAMEWORK(UIKit ${NAME} ${libpath})

# code signing
set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer: Some Name")

I am trying to generate an XCode project with CMake, but I ran into some problems.

CMake generates the project fine, but afterwards it becomes apparent that it hasn't linked to the Foundation and UIKit frameworks. I am pretty new to CMake and have been trying to overcome this, but with no success.

The CMake output (partial):

...
Framework Foundation found at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/Frameworks/Foundation.framework
Framework CoreGraphics found at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/Frameworks/CoreGraphics.framework
Framework UIKit found at /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library/Frameworks/UIKit.framework
Configuring done
Generating done

Below is my CMakeLists.txt file (put together from several answers on SO):

cmake_minimum_required(VERSION 2.8)

macro(ADD_FRAMEWORK fwname appname libpath)
    find_library(FRAMEWORK_${fwname}
        NAMES ${fwname}
        PATHS ${libpath}

        PATH_SUFFIXES Frameworks
        NO_DEFAULT_PATH)
    if( ${FRAMEWORK_${fwname}} STREQUAL FRAMEWORK_${fwname}-NOTFOUND)
        MESSAGE(ERROR ": Framework ${fwname} not found")
    else()
        TARGET_LINK_LIBRARIES(${appname} ${FRAMEWORK_${fwname}})
        MESSAGE(STATUS "Framework ${fwname} found at ${FRAMEWORK_${fwname}}")
    endif()
endmacro(ADD_FRAMEWORK)

project(test)
set(NAME test)
set (libpath /Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS4.2.sdk/System/Library)


file(GLOB headers *.h)
file(GLOB sources *.m)

set(CMAKE_OSX_SYSROOT iphoneos4.2)
set(CMAKE_OSX_ARCHITECTURES $(ARCHS_STANDARD_32_BIT))
set(CMAKE_CXX_FLAGS "-x objective-c++")
set(CMAKE_EXE_LINKER_FLAGS
    "-framework AudioToolbox -framework CoreGraphics -framework QuartzCore -framework UIKit -framework Foundation"
)

set(MACOSX_BUNDLE_GUI_IDENTIFIER "com.mycompany.\${PRODUCT_NAME:identifier}")
set(APP_TYPE MACOSX_BUNDLE)

add_executable(${NAME}
    ${APP_TYPE}
    ${headers}
    ${sources}
)

ADD_FRAMEWORK(Foundation ${NAME} ${libpath})
ADD_FRAMEWORK(CoreGraphics ${NAME} ${libpath})
ADD_FRAMEWORK(UIKit ${NAME} ${libpath})

# code signing
set_target_properties(${NAME} PROPERTIES XCODE_ATTRIBUTE_CODE_SIGN_IDENTITY "iPhone Developer: Some Name")

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

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

发布评论

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

评论(2

笑着哭最痛 2024-11-03 02:36:46

我有一些建议:

  • 不要使用 add_framework。这使得 CMake 设置项目属性 FRAMEWORK_SEARCH_PATHS ,这不知何故搞砸了一切。相反,使用命令行开关 -framework
  • CMAKE_OSX_SYSROOT 你可以只使用 iphoneos,
  • 你不需要 CMAKE_CXX_FLAGS

有了这些你可能会更幸运。到目前为止,我设法克服了大部分链接错误。

I have a few suggestions:

  • don't use add_framework. This makes CMake set the project property FRAMEWORK_SEARCH_PATHS which somehow screws up everything. Instead use the command line switch -framework
  • for CMAKE_OSX_SYSROOT you can just use iphoneos
  • you don't need the CMAKE_CXX_FLAGS

With these you might have more luck. I managed to get past most of the link errors so far.

痞味浪人 2024-11-03 02:36:46

CMakeLists.txt 中尝试以下规则:

link_directories(\${HOME}/\${SDKROOT}/lib)

Try the following rule in your CMakeLists.txt:

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