编译/链接FreeImage for iOS 5.0

时间:2013-01-04 15:57:21

标签: c++ ios5 c++11 makefile freeimage

我最近决定尝试在iOS设备上运行一些图形代码,但我使用FreeImage来加载纹理。因此,我需要为iOS 5.0构建它。

当我尝试使用FreeImage库时,我正在收到链接错误。链接错误都是与标准C ++库相关的。例如......

Undefined symbols for architecture i386:
  "std::basic_string<char, std::char_traits<char>, std::allocator<char>>::~basic_string()", referenced from:
  _FreeImage_GetMetadata in libfreeimage-iphonesimulator.a(BitmapAccess.o-i386)
  _FreeImage_SetMetadata in libfreeimage-iphonesimulator.a(BitmapAccess.o-i386)
  _FreeImage_CloneMetadata in libfreeimage-iphonesimulator.a(BitmapAccess.o-i386)
  _FreeImage_Clone in libfreeimage-iphonesimulator.a(BitmapAccess.o-i386)
  std::pair<std::string const, FITAG*>::~pair() in libfreeimage-iphonesimulator.a(BitmapAccess.o-i386)
  Load(FreeImageIO*, void*, int, int, void*) in libfreeimage-iphonesimulator.a(PluginEXR.o-i386)
  C_OStream::write(char const*, int) in libfreeimage-iphonesimulator.a(PluginEXR.o-i386)
  ...

不幸的是,用于构建FreeImage for iOS的提供的makefile有点过时,所以我不得不更新它。此外,在我的XCode项目中,我将编译器切换为支持C ++ 11功能并使用libc ++(如此处所述Can I use C++11 with Xcode?

所以我试图在FreeImage的makefile中镜像这些更改,但我仍然遇到这些错误。

我的makefile看起来像这样(我觉得这篇文章有点帮助http://sourceforge.net/p/freeimage/discussion/36110/thread/51445acc

# Configuration for iPhone OS, making static libs
# this will generate both iPhone (arm) and iPhoneSimulator (i686) libs

include Makefile.srcs

CFLAGS =  -g -O2 -Wall -Wmissing-prototypes -std=c99 -ffast-math -fno-strict-aliasing
CXXFLAGS = -g -O2 -Wall -fno-strict-aliasing -std=c++0x -stdlib=libc++

GCC_VERSION = 4.2
IPHONEOS_DEPLOYMENT_TARGET = 5.0
MACOSX_DEPLOYMENT_TARGET = 10.6

PLATFORM_SIM = iPhoneSimulator
PLATFORM_PHONE = iPhoneOS

ARCH_SIM = i386
ARCH_PHONE = armv7

PLATFORM_SIM_DEVELOPER_BIN_DIR = /Developer/Platforms/$(PLATFORM_SIM).platform/Developer/usr/bin
PLATFORM_PHONE_DEVELOPER_BIN_DIR = /Developer/Platforms/$(PLATFORM_PHONE).platform/Developer/usr/bin

SDKROOT_SIM = /Developer/Platforms/$(PLATFORM_SIM).platform/Developer/SDKs/$(PLATFORM_SIM)$(IPHONEOS_DEPLOYMENT_TARGET).sdk
SDKROOT_PHONE = /Developer/Platforms/$(PLATFORM_PHONE).platform/Developer/SDKs/$(PLATFORM_PHONE)$(IPHONEOS_DEPLOYMENT_TARGET).sdk

EXTRA_CFLAGS_SIM += -arch $(ARCH_SIM) -pipe -mdynamic-no-pic -fvisibility=hidden $(INCLUDE) -isysroot $(SDKROOT_SIM)
EXTRA_LDFLAGS_SIM += -arch $(ARCH_SIM) -isysroot $(SDKROOT_SIM) -Wl,-dead_strip
EXTRA_CFLAGS_SIM += -D__IPHONE_OS_VERSION_MIN_REQUIRED=20000 -mmacosx-version-min=$(MACOSX_DEPLOYMENT_TARGET)
EXTRA_LDFLAGS_SIM += -mmacosx-version-min=$(MACOSX_DEPLOYMENT_TARGET)

EXTRA_CFLAGS_PHONE += -arch $(ARCH_PHONE) -pipe -mdynamic-no-pic -fvisibility=hidden $(INCLUDE) -isysroot $(SDKROOT_PHONE)
EXTRA_LDFLAGS_PHONE += -arch $(ARCH_PHONE) -isysroot $(SDKROOT_PHONE) -Wl,-dead_strip
EXTRA_CFLAGS_PHONE += -miphoneos-version-min=$(IPHONEOS_DEPLOYMENT_TARGET)
EXTRA_LDFLAGS_PHONE += -miphoneos-version-min=$(IPHONEOS_DEPLOYMENT_TARGET)

AR_SIM = $(PLATFORM_SIM_DEVELOPER_BIN_DIR)/ar
AR_PHONE = $(PLATFORM_PHONE_DEVELOPER_BIN_DIR)/ar

CC_SIM = $(PLATFORM_SIM_DEVELOPER_BIN_DIR)/llvm-gcc-$(GCC_VERSION)
CC_PHONE = $(PLATFORM_PHONE_DEVELOPER_BIN_DIR)/llvm-gcc-$(GCC_VERSION)

CFLAGS_SIM = $(CFLAGS) $(EXTRA_CFLAGS_SIM)
LDFLAGS_SIM = $(EXTRA_LDFLAGS_SIM)
CXX_SIM = $(PLATFORM_SIM_DEVELOPER_BIN_DIR)/clang++
CXXFLAGS_SIM += $(EXTRA_CFLAGS_SIM) -fvisibility-inlines-hidden 
LIBTOOL_SIM = /Developer/Platforms/$(PLATFORM_SIM).platform/Developer/usr/bin/libtool

CFLAGS_PHONE = $(CFLAGS) $(EXTRA_CFLAGS_PHONE)
LDFLAGS_PHONE += $(EXTRA_LDFLAGS_PHONE)
CXX_PHONE = $(PLATFORM_PHONE_DEVELOPER_BIN_DIR)/clang++
CXXFLAGS_PHONE += $(EXTRA_CFLAGS_PHONE) -fvisibility-inlines-hidden
LIBTOOL_PHONE = /Developer/Platforms/$(PLATFORM_PHONE).platform/Developer/usr/bin/libtool

TARGET = freeimage
STATICLIB_SIM = lib$(TARGET)-iphonesimulator.a
STATICLIB_PHONE = lib$(TARGET)-iphone.a
HEADER = Source/FreeImage.h

.SUFFIXES: .o-i386 .o-arm
MODULES_ARM = $(SRCS:.c=.o-arm)
MODULES_ARM := $(MODULES_ARM:.cpp=.o-arm)
MODULES_i386 = $(SRCS:.c=.o-i386)
MODULES_i386 := $(MODULES_i386:.cpp=.o-i386)

default: all

all: dist

dist: FreeImage
    cp *.a Dist
    cp Source/FreeImage.h Dist

FreeImage: $(STATICLIB_SIM) $(STATICLIB_PHONE)

$(STATICLIB_SIM): $(MODULES_i386)
    $(LIBTOOL_SIM) -arch_only $(ARCH_SIM) -o $@ $(MODULES_i386)

.c.o-i386:
    $(CC_SIM) $(CFLAGS_SIM) -c $< -o $@

.cpp.o-i386:
    $(CXX_SIM) $(CXXFLAGS_SIM) -c $< -o $@

$(STATICLIB_PHONE): $(MODULES_ARM)
    $(LIBTOOL_PHONE) -arch_only $(ARCH_PHONE) -o $@ $(MODULES_ARM)

.c.o-arm:
    $(CC_PHONE) $(CFLAGS_PHONE) -c $< -o $@

.cpp.o-arm:
    $(CXX_PHONE) $(CXXFLAGS_PHONE) -c $< -o $@

clean:
    rm -f core Dist/*.* u2dtmp* $(MODULES_i386) $(MODULES_ARM) $(STATICLIB_SIM) $(STATICLIB_PHONE)

也许有人可以指出我出错的地方

编辑: 我修复了一个问题,即它没有重建i386目标文件,希望它是解决方案,但它仍然有相同的链接错误。

编辑: 我调整了我的makefile以使用clang ++

我还将libc ++添加到链接框架

enter image description here

以下是关于C ++的XCode项目的设置

enter image description here

我的XCode项目中也有.cpp文件。然而,错误仍然存​​在。

EDIT3:

请求查询的结果:

nm libfreeimage-iphonesimulator.a | c++filt | grep '~basic_string()' | sort -u
nm: no name list
nm: no name list
         U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()

可以在此处找到完整的链接器错误输出:http://pastebin.com/wjbWgE4S

1 个答案:

答案 0 :(得分:4)

Objective C项目使用clang编译器(您可能仍在使用gcc),这是一个(技术上讲)C编译器,而不是C++编译器。它足够聪明,可以使用文件扩展名来确定是否将代码编译为CObjective CC++

当您链接Objective C项目时,它使用clang链接,C++libc++运行时没有链接。您需要在运行时链接。

如果您使用libc++库构建库,则需要将Build Phases -> Link Binary With Libraries添加到libstdc++中链接到项目的库列表中。如果您使用libstdc++库进行构建,则需要将clang++添加到链接的库列表中。

如果项目中只有一个C++文件,则Xcode非常智能,可以使用C++进行链接,从而无需在该情况下在C++运行时显式链接。

tl; dr - 您似乎没有使用与构建主应用程序相同的标记来编译模拟器的CXXFLAGS_SIM += $(EXTRA_CFLAGS_SIM) -fvisibility-inlines-hidden 代码。这一行:

$(CXXFLAGS)

实际上缺少基础CXXFLAGS_SIM += $(CXXFLAGS) $(EXTRA_CFLAGS_SIM) -fvisibility-inlines-hidden 项,应该显示为:

CXXFLAGS_PHONE

同样适用于libc++

因此您混合了libstdc++CXXFLAGS已编译的代码,导致链接错误。应该提示c++行没有被使用,因为它在编译行的开头有libc++,如果在任何代码中使用它会触发编译错误

还有其他问题,例如使用__IPHONE_OS_VERSION_MIN_REQUIRED=50000您的iPhone部署目标必须是iOS 5或更高版本(因此模拟器编译需要Source/LibRawLite/./internal/dcraw_common.cpp:3926:19: error: constant expression evaluates to 128 which cannot be narrowed to type 'signed char' [-Wc++11-narrowing] ),并且由于符号扩展问题,您需要修复一些文件例如

C++

如果要检查-stdlib=libc++代码是否已使用-stdlib=libstdc++nm进行编译,您可以执行c++filt任何生成的已编译代码,并将其传递给它通过std::__1::。如果您看到-stdlib=libc++的命名空间,则代码使用-stdlib=libstdc++进行编译,否则使用{{1}}编译