티스토리 뷰

Programming/IOS

ios - c++ static library xcode에서 사용하기

파란크리스마스 2012. 9. 13. 02:16
728x90

출처
Apple LLVM compilerと無名カテゴリ
Building static C++ lib for use with Objective-c app in Xcode 4
static library 빌드 및 라이브러리 사용 어플리케이션 통합 디버깅하기 - 아이폰 4.x
Using C/C++ static libraries from iPhone ObjectiveC Apps
Xcode architectureエラー
objective c에서 STL 사용하기
아이폰에서 C로 된 정적 라이브러리 사용하기
Xcode 4.0 에서 c로 된 static library link error 조치

testlib.h 파일

/*
 * testlib.h
 *
 *  Created on: 2012. 9. 6.
 *      Author: bluexmas
 */

#ifndef LIBTEST_H_
#define LIBTEST_H_

#if defined(_WIN32) || defined(__WIN32__)
	#define DLL_CALLCONV __stdcall
	#ifdef LIBTEST_EXPORTS
		#define DLL_API __declspec(dllexport)
	#else
		#define DLL_API __declspec(dllimport)
	#endif // LIBTEST_EXPORTS
#else 
	// try the gcc visibility support (see http://gcc.gnu.org/wiki/Visibility)
	#if defined(__GNUC__) && ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
		#ifndef GCC_HASCLASSVISIBILITY
			#define GCC_HASCLASSVISIBILITY
		#endif
	#endif // __GNUC__
	#define DLL_CALLCONV
	#if defined(GCC_HASCLASSVISIBILITY)
		#define DLL_API __attribute__ ((visibility("default")))
	#else
		#define DLL_API
	#endif		
#endif // WIN32 / !WIN32

DLL_API const char *DLL_CALLCONV get_version(void);

#endif /* LIBTEST_H_ */

testlib.cpp 파일

/*
 * testlib.cpp
 *
 *  Created on: 2012. 9. 6.
 *      Author: bluexmas
 */

#include "testlib.h"

const char* get_version() {
  return "testlib ver 0.2";
}

makefile.iphone-debug 파일

RM := rm -rf

GCC_VERSION = 4.2
IPHONEOS_DEPLOYMENT_TARGET = 5.1
MACOSX_DEPLOYMENT_TARGET = 10.8

PLATFORM_SIM = iPhoneSimulator
PLATFORM_PHONE = iPhoneOS

ARCH_SIM = i386
ARCH_PHONE = armv7

TOOLCHAIN=/Volumes/Mac/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
DEVROOT=/Volumes/Mac/Applications/Xcode.app/Contents/Developer
PLATFORM_SIM_DEVELOPER_BIN_DIR = $(DEVROOT)/Platforms/$(PLATFORM_SIM).platform/Developer/usr/bin
PLATFORM_PHONE_DEVELOPER_BIN_DIR = $(DEVROOT)/Platforms/$(PLATFORM_PHONE).platform/Developer/usr/bin

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

IDIR=./inc
CLANG=$(TOOLCHAIN)/clang
CLANGFLAGS_SIM=-x objective-c-header -O0 -g3 -Wall -c -fmessage-length=0 -DDEBUG=1
CLANGFLAGS=-x objective-c -O0 -g3 -Wall -c -fmessage-length=0
LIBTOOL=$(TOOLCHAIN)/libtool

SDIR=./src
ODIR=./lib

#CPP_SRCS = \
#	./src/testlib.cpp 

OBJS += \
	./lib/testlib.o-sim
	
CPP_DEPS += \
	./lib/testlib.d	-sim
	
USER_OBJS :=

LIBS :=	

# Each subdirectory must supply rules for building sources it contributes
$(ODIR)/%.o-sim: $(SDIR)/%.cpp
	@echo 'Building file: $<'
	@echo 'Invoking: Cygwin C++ Compiler'
	$(CLANG) -c $(CLANGFLAGS_SIM) -arch $(ARCH_SIM) -isysroot $(SDKROOT_SIM) -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o-sim=%.d-sim)" -I$(IDIR) -o "$@" "$<"
	@echo 'Finished building: $<'
	@echo ' '

# All Target
all: libtestlib-sim.a

# Tool invocations
libtestlib-sim.a: $(OBJS) $(USER_OBJS)
	@echo 'Building target: $@'
	@echo 'Invoking: GCC Archiver'
	$(LIBTOOL) -static -arch_only $(ARCH_SIM) -syslibroot $(SDKROOT_SIM) -o libtestlib-sim.a $(OBJS) $(USER_OBJS) $(LIBS)
	@echo 'Finished building target: $@'
	@echo ' '	
	
# Other Targets
clean:
	-$(RM) $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(ARCHIVES)$(CPP_DEPS)$(CXX_DEPS)$(C_UPPER_DEPS) libtestlib-sim.a
	-@echo ' '

makefile.cygwin 파일(참고용)

RM := rm -rf

IDIR=./inc
CC=g++
CFLAGS=-I$(IDIR)

SDIR=./src
ODIR=./lib

#CPP_SRCS = \
#	./src/testlib.cpp 

OBJS += \
	./lib/testlib.o 
	
CPP_DEPS += \
	./lib/testlib.d	
	
USER_OBJS :=

LIBS :=	

# Each subdirectory must supply rules for building sources it contributes
$(ODIR)/%.o: $(SDIR)/%.cpp
	@echo 'Building file: $<'
	@echo 'Invoking: Cygwin C++ Compiler'
	$(CC) $(CFLAGS) -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"$(@:%.o=%.d)" -MT"$(@:%.o=%.d)" -o "$@" "$<"
	@echo 'Finished building: $<'
	@echo ' '

# All Target
all: libtestlib.a

# Tool invocations
libtestlib.a: $(OBJS) $(USER_OBJS)
	@echo 'Building target: $@'
	@echo 'Invoking: GCC Archiver'
	ar -r  "libtestlib.a" $(OBJS) $(USER_OBJS) $(LIBS)
	@echo 'Finished building target: $@'
	@echo ' '	
	
# Other Targets
clean:
	-$(RM) $(C++_DEPS)$(OBJS)$(C_DEPS)$(CC_DEPS)$(ARCHIVES)$(CPP_DEPS)$(CXX_DEPS)$(C_UPPER_DEPS) libtestlib.a
	-@echo ' '	

정적라이브러리 만들기

 

빌드옵션

Header Search Paths 추가

Build Phase -> Link Binary With Libraries 추가

정적 라이브러리 메소드 호출

#include "testlib.h" 추가

실행

버튼 선택

 

 

 

- end -

댓글
300x250
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30
글 보관함