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()
{
"working_dir": "${project_path:${folder}}",
"shell_cmd": "start D:\\soft\\cocos\\workspace\\python_run.bat D:\\soft\\gnustep\\workspace\\run_oc.py ${file}"
}
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;
}
完成
上一篇: 06. Linux Bash 帮助,系统 man、info 页面
下一篇: 使用Spring发送基于freemarker模板的邮件 博客分类: Spring SpringFreemarkerJavaMail模板邮件
推荐阅读
-
Windows下Sublime Text编译运行Object C
-
从零开始 windows下配置vscode 编译/调试运行C++
-
C++环境安装及 如何用 Sublime Text 实现C/C++代码的编译运行
-
Sublime text3 实现C语言编译运行以及过程中可能遇到的问题
-
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
-
Windows下使用VS Code 编译、运行和调试C/C++
-
win10 系统下sublime text3 下C/C++编译环境搭建
-
解决windows下Sublime Text 2 运行 PyQt 不显示的方法分享
-
Windows下使用MinGW在命令行编译运行C++程序
-
linux和Windows下用sublime text3编译运行C,C++