CMake 和 XCode:“找不到“NSObject”的接口声明”
我正在尝试使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有一些建议:
add_framework
。这使得 CMake 设置项目属性FRAMEWORK_SEARCH_PATHS
,这不知何故搞砸了一切。相反,使用命令行开关-framework
CMAKE_OSX_SYSROOT
你可以只使用 iphoneos,CMAKE_CXX_FLAGS
有了这些你可能会更幸运。到目前为止,我设法克服了大部分链接错误。
I have a few suggestions:
add_framework
. This makes CMake set the project propertyFRAMEWORK_SEARCH_PATHS
which somehow screws up everything. Instead use the command line switch-framework
CMAKE_OSX_SYSROOT
you can just use iphoneosCMAKE_CXX_FLAGS
With these you might have more luck. I managed to get past most of the link errors so far.
在
CMakeLists.txt
中尝试以下规则:Try the following rule in your
CMakeLists.txt
: