欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Windows下Sublime Text编译运行Object C

程序员文章站 2024-03-24 14:26:10
...


Windows中跑Object C需要下载安装GNUStep,我这里已经装好了,本来可以用codeblocks配置编译环境,但还是觉得不好用,抛开IDE不说,还是更喜欢用Sublime Text写代码。

http://blog.csdn.net/u012370255/article/details/50618025点击打开链接

这里面有人已经实现了此功能,Sublime的buildSystem还是不太熟悉,配不成功,倒不如自己写一个编译脚本,Python来写

gcc编译命令:

gcc -o  test.exe  test.m  -I D:/soft/gnustep/gnustep/GNUstep/System/Library/Headers   -L  D:/soft/gnustep/gnustep/GNUstep/System/Library/Libraries  -std=c99  -lobjc -lgnustep-base   -fconstant-string-class=NSConstantString

编译原理:

   执行一个python脚本,传入入口源文件(*.m), 根据这个源文件,生成一条 gcc编译命令,然后调用os.system方法执行这个编译命令,编译成功后,调用exe文件


run_oc.py

# coding=UTF-8
#这个文件是用来编译并运行object-c代码的
import sys
import string
import os
import re
import shutil

reverXieGang = "\\"
xieGang = "/"
def formatDir(str):
	str = str.replace(reverXieGang, xieGang)
	if str[len(str) - 1] == '/':
		return str[0:len(str)-1]
	return str
def getParentDir(str):
	index = str.rfind(xieGang,0, len(str))
	if index > -1:
		ret = str[0:index]
		return ret
	return str
#取得文件名,没有后缀
def getFileNoExtName(str):
	index = str.rfind('.',0, len(str))
	if index > -1:
		ret = str[0: index]
		return ret
	return str
def getLastDir(str):
	index = str.rfind(xieGang,0, len(str))
	if index > -1:
		return str[index + 1: len(str)]
	return str
def myCpFile(filePath, destDir):
	fileName = getLastDir(filePath)
	destDir = formatDir(destDir)
	destFile = destDir + xieGang + fileName
	#print "复制文件%s到%s" % (filePath, destFile)
	shutil.copyfile(filePath, destFile)
	return
#复制文件
def cpFilesByRegrex(path, fileRegrex, flag, destDir):
	for dirpath,dirnames,filenames in os.walk(path):
	    for file in filenames:
	    	if dirpath == path:
	    		if re.match(fileRegrex, file, flag):
	    			myCpFile(dirpath + xieGang + file, destDir)
	    	else:
	    		return
def cpFilesByName(dirPath, fileName, distDir):
	myCpFile(dirPath + xieGang + fileName, distDir)
def  findDir(path, dirName):
	for dirpath,dirnames,filenames in os.walk(path):
	    for d in dirnames:
	    	if d == dirName:
	    		return 1
	return 0
#根据正则匹配取得目录或文件名称
def getDirOrFileNameByRegrex(dirPath, regrex, flag):
	for dirpath,dirnames,filenames in os.walk(dirPath):
	    if dirpath == dirPath:
	    	for dirName in dirnames:
	    		if re.match(regrex, dirName, flag):
	    			return dirName
	    	for fileName in filenames:
	    		if re.match(regrex, fileName, flag):
	    			return fileName
	    else:
	    	return ""	
	    	


#拷贝目录
def cpDir(dirPath, destDir):
	#print "复制目录%s到%s" % (dirPath, destDir)
	dirName = getLastDir(dirPath)
	if os.path.exists(destDir + xieGang + dirName) == False:
		os.chdir(destDir)
		#print "创建目录", dirName
		os.mkdir(dirName)
	for dirpath,dirnames,filenames in os.walk(dirPath):
		if dirpath == dirPath:
		    for file in filenames:
		    	cpFilesByName(dirPath, file, destDir + xieGang + dirName)
		    for subDir in dirnames:
		    	#print "子目录名:", subDir
		    	cpDir(dirpath + xieGang + subDir, destDir + xieGang + dirName)

def mkAllDir(curDir, path):
	pathArr = path.split(xieGang)
	tmpPath = curDir
	for i in xrange(0,len(pathArr)):
		p = tmpPath + xieGang + pathArr[i]
		if ~os.path.exists(p):
			#目录不存在
			os.chdir(tmpPath)
			os.mkdir(pathArr[i])
			tmpPath = p
	os.chdir(curDir)

#编译参数
compileParamArr = ['-I', 
	'D:/soft/gnustep/gnustep/GNUstep/System/Library/Headers', '-L', 
	'D:/soft/gnustep/gnustep/GNUstep/System/Library/Libraries', '-std=c99', '-lobjc',
	'-lgnustep-base', '-fconstant-string-class=NSConstantString']
def main():
	argsLen = len(sys.argv)
	if argsLen < 2:
		print "param error."
	else:
		filePath = sys.argv[1]
		print "Entry file is ", filePath
		filePath = formatDir(filePath)
		fileName = getLastDir(filePath)
		parentDir = getParentDir(filePath)
		#工程名
		parentDirName = getLastDir(parentDir)
		print "project name is ", parentDirName
		compileCmd = ''
		first = True
		for i in xrange(0,len(compileParamArr)):
			if first:
				compileCmd = compileCmd + compileParamArr[i]
				first = False
			else:
				compileCmd = compileCmd + ' ' + compileParamArr[i]

		debugRelativePath = 'bin/Debug'
		try:
			shutil.rmtree(parentDir + '/bin')
		except:
			pass
		mkAllDir(parentDir, debugRelativePath)
		exePath = parentDir + xieGang + debugRelativePath + xieGang + parentDirName + '.exe '
		compileCmd = 'gcc -o ' + exePath +  fileName + ' ' + compileCmd
		print "compile cmd is :", compileCmd
		try:
			os.system(compileCmd)
		except:
			pass
		if os.path.exists(exePath):
			#编译成功
			print 'Compile successfully....................................\n\n\n'
			os.system(exePath)



if __name__ == '__main__':
	main()


Object-c.sublime-build文件, sublime的build system文件, 这里使用start命令新开了一个cmd窗口,sublime自带的编译输入框有点看不懂

{
	"working_dir": "${project_path:${folder}}",
	"shell_cmd": "start D:\\soft\\cocos\\workspace\\python_run.bat D:\\soft\\gnustep\\workspace\\run_oc.py ${file}"
}


python_run.bat, 这个命令的作用只是在执行python脚本后,pause了一下,因为没有这个命令的话,cmd窗口闪一下就关闭了

python %1 %2
pause 

好了,最后新建一个m文件,测试下


#import <Foundation/Foundation.h>
int main (int argc, const char *argv[]){
    NSAutoreleasePool *pool =[[NSAutoreleasePool alloc] init];
    NSLog(@"%@",@"hello world\n");
    NSLog(@"aaaaaaaaaaaaaaaaa");
    [pool drain];

    return 0;

}

Windows下Sublime Text编译运行Object C

完成

相关标签: ObjectC