Skip to content

Instantly share code, notes, and snippets.

@gunyarakun
Created May 25, 2015 00:05
Show Gist options
  • Save gunyarakun/e8943f2088d9c43656f3 to your computer and use it in GitHub Desktop.
Save gunyarakun/e8943f2088d9c43656f3 to your computer and use it in GitHub Desktop.
Compile MacOSX Objective-C CLI program using NSBundle with CMake
PROJECT(hello)
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)
SET(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/CMakeModules)
SET(PROJECT_BINARY_DIR build)
SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)
SET(CMAKE_C_FLAGS "-std=c99 -Wall")
IF(APPLE)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -framework Foundation")
SET(LINK_FLAGS "${LINK_FLAGS} -framework Foundation")
ENDIF()
FILE (GLOB EXTRA_RESOURCES
${CMAKE_CURRENT_SOURCE_DIR}/text.txt
)
SET_SOURCE_FILES_PROPERTIES(
${EXTRA_RESOURCES}
PROPERTIES
MACOSX_PACKAGE_LOCATION "Resources"
)
SOURCE_GROUP(Resources FILES
${EXTRA_RESOURCES}
)
ADD_EXECUTABLE(hello MACOSX_BUNDLE hello.m ${EXTRA_RESOURCES})
TARGET_LINK_LIBRARIES(
hello
)
#import <Foundation/Foundation.h>
int main(int argc, char *argv[])
{
NSBundle *bundle = [NSBundle mainBundle];
NSString *bundlePath = [bundle bundlePath];
NSString *resourcePath = [bundle resourcePath];
NSString *pathForResource = [bundle pathForResource:@"text" ofType:@"txt"];
NSLog(@"bundle:%@ bundlePath:%@ resourcePath:%@ pathForResource:%@", bundle, bundlePath, resourcePath, pathForResource);
if (pathForResource) {
NSString *txt = [NSString stringWithContentsOfFile:pathForResource encoding:NSUTF8StringEncoding error:NULL];
if (txt) {
NSLog(@"%@", txt);
}
}
return 0;
}
Hello, World!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment