简体   繁体   English

在Xcode上测试纯C代码?

[英]Testing a pure C code on Xcode?

I am trying to use the latest Xcode to simulate some C code, that has nothing to do with iPhone, only test some data manipulation on C functions, and print results to see it working. 我正在尝试使用最新的Xcode来模拟一些与iPhone无关的C代码,仅测试对C函数的一些数据操作,并打印结果以查看其是否有效。

I have started a new playground in Xcode , for iOS , but it gives error when you try to write some C code ( char me[]= "this is me" ; ) 我已经在iOS的Xcode中启动了一个新游乐场,但是当您尝试编写一些C代码时,它给出了错误( char me[]= "this is me" ;

How can i do it anyway ? 我该怎么办? thanks. 谢谢。

The playground is for swift, not C, hence the errors. 操场是为了迅速,而不是C,因此是错误。

You can develop C programs using Xcode, but you want to target OSX, probably using the command line project template. 您可以使用Xcode开发C程序,但是您可能希望使用命令行项目模板来定位OSX。

Start by writing main() : 首先编写main()

#include <stdio.h>

int main(int argc, const char **argv)
{
    printf("Hello world!\n");
    return 0;
}

EDIT : In order to call a function outside of main() you must declare it before it is referenced: 编辑 :为了在main()之外调用函数,必须在引用它之前声明它:

// Forward declaration of function
static void printit(const char *message);

int main(int argc, const char **argv)
{
    printit("Hello World\n");
    return 0;
}

static void printit(const char *message)
{
    fwrite(message, 1, strlen(message), stdout);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM