我在OSX 10.9上使用XCode 5.0.1,我正在连接libc ++(LLVM C ++标准库)和有效架构armv7; armv7s和两者。
1º我创建了一个使用Cmake使用OpenCV2.framework + foo的项目,使用foo.a创建一个XCode项目。我的可执行文件链接OpenCV2.framework + foo.a并且完美地工作,编译和链接。
2º我使用Ogre的模板创建了另一个项目。这个项目工作得很好。
3º然后,我尝试将foo.a添加到新项目中,以创建与我在第1步中使用的可执行文件相同的内容。
那么,如果库在另一个项目中工作,并且函数在里面,为什么会出现链接问题?
编译此示例的我的CmakeList是:
# /////////////////////////////////////////////////////////
# //SAMPLE
# /////////////////////////////////////////////////////////
# 3rdParty (OGRE LOCATION)
set(OGRE_SDK_ROOT /Users/vgoni/Librerias/ogre1.9-pre/OgreSDK/ CACHE PATH "Ogre SDK ROOT")
set(OGRE_DEPENDENCIES_DIR ${OGRE_SDK_ROOT}/iOSDependencies CACHE PATH "Ogre SDK DEP")
# Set up project
SET(PROJ_NAME ogreIOS)
PROJECT( ${PROJ_NAME} )
SET(PRODUCT_NAME ${PROJ_NAME})
SET(EXECUTABLE_NAME ${PROJ_NAME}Executable)
# Add variable to generate iphone project in ADD_EXECUTABLE
SET(APP_TYPE MACOSX_BUNDLE)
# Headers
SET(${PROJ_NAME}_HEADERS
include/OgreStaticPluginLoader.h
include/OgreDemoApp.h
include/OgreFramework.h
include/AppDelegate.h
)
# Source
SET(${PROJ_NAME}_SRC
src/OgreFramework.cpp
src/OgreDemoApp.cpp
src/main.mm
)
# Add there files to Resources package into XCODE
SET_SOURCE_FILES_PROPERTIES(
resources/ogre.cfg
resources/plugins.cfg
resources/resources.cfg
resources/ogreiosSample-Info.plist
resources/ogreiosSample-Prefix.pch
resources/en.lproj/InfoPlist.strings
PROPERTIES
MACOSX_PACKAGE_LOCATION Resources
)
# Include self directories
INCLUDE_DIRECTORIES( include )
# Include OGRE headers needed
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/include )
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/include/OIS )
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/include/OGRE )
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/include/OGRE/Overlay )
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/include/OGRE/iOS )
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/include/OGRE/RTShaderSystem )
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/include/OGRE/RenderSystems/GLES2 )
INCLUDE_DIRECTORIES( ${OGRE_SDK_ROOT}/iOSDependencies/include )
# Link Directories where libraries needed are
LINK_DIRECTORIES( ${OGRE_SDK_ROOT}/lib/Release )
LINK_DIRECTORIES( ${OGRE_SDK_ROOT}/iOSDependencies/lib )
LINK_DIRECTORIES( ${OGRE_SDK_ROOT}/iOSDependencies/lib/Release )
# Add ogre & boots libraries flags
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lmesaglsl2 -lboost_system -lboost_chrono -lboost_date_time -lboost_thread -lFreeType -lFreeImage -lzzip -lz -lc++ -lforce_load -lfoo")
# foo is my library, force to load all to avoid fail in linking
# 3rdParty ogre libraries
SET(OGRE_LIBS OIS OgreMainStatic RenderSystem_GLES2Static OgreRTShaderSystemStatic )
# Link the libraries needed
#TARGET_LINK_LIBRARIES (${PROJ_NAME} ${OGRE_LIBS})
# Create executable for iOS
ADD_EXECUTABLE(
${EXECUTABLE_NAME}
${APP_TYPE}
${${PROJ_NAME}_HEADERS}
${${PROJ_NAME}_SRC}
)
# Adding info to XCode project
#set_target_properties(${EXECUTABLE_NAME} PROPERTIES
#MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/resources/Info.plist)
# Change name to make work Macros
SET(PROJ_NAME ogreIOSExecutable)
# Macro to add frameworks to Sample Executable
link_ios_framework(AssetsLibrary ${EXECUTABLE_NAME})
link_ios_framework(AVFoundation ${EXECUTABLE_NAME})
link_ios_framework(CoreMedia ${EXECUTABLE_NAME})
link_ios_framework(CoreGraphics ${EXECUTABLE_NAME})
link_ios_framework(CoreVideo ${EXECUTABLE_NAME})
link_ios_framework(ImageIO ${EXECUTABLE_NAME})
link_ios_framework(Foundation ${EXECUTABLE_NAME})
link_ios_framework(OpenGLES ${EXECUTABLE_NAME})
link_ios_framework(QuartzCore ${EXECUTABLE_NAME})
link_ios_framework(UIKit ${EXECUTABLE_NAME})
# Needed dependencies
link_ios_sdk(libc++.1.dylib ${EXECUTABLE_NAME})
link_ios_sdk(libz.dylib ${EXECUTABLE_NAME})
#Add OpenCV Libraries to link
TARGET_LINK_LIBRARIES (${PROJ_NAME} ${OpenCV_LIBS})
LINK_DIRECTORIES(
${LIBRARY_OUTPUT_PATH}
)
答案 0 :(得分:3)
您必须为要添加所有要链接的功能的每个lib添加一次标志-ObjC和-force_load,以避免因未添加的功能而导致失败。
例如,您必须更改添加到lib boost_system的cpp标志,因为这会导致链接器问题:
SET (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lmesaglsl2 -force_load -lboost_system -lboost_chrono -lboost_date_time -lboost_thread -lFreeType -lFreeImage -lzzip -lz -lc++ -lforce_load -lfoo")
并为您需要的每个lib应用此标志。
答案 1 :(得分:2)
Clang的链接器对库链接顺序有点严格,您是否可以尝试交换项目中的链接顺序,看看是否是这个原因?
你能在这里制作链接步骤命令行吗?你只需要点击那里的按钮:
此外,也许可以尝试从其他链接器选项中删除-lfoo
,因为将.a
添加到项目中就足够了。
答案 2 :(得分:0)
基本问题,您是否已将foo.a添加到链接库中?对于好的目标(打开右侧面板并检查lib是否与所需的所有目标相关联)?