苹果的开发语言Objective-C是完全兼容C语言的,所以在ios项目中调用C语言的函数也是非常简单方便的,它允许开发者使用c语言文件和objective-c文件混合编程。
但是,如果你在Objective-C的代码中调用C文件中的函数,你不能直接将.c文件import到你的OC文件中,这样是不起作用的。你需要先创建一个.h 头文件 里面包含你的
函数申明,同时将这个.h 头文件import到.c文件中,.c文件负责实现要调用的函数。最后将你新创建的.h头文件import到OC文件中,这样你就可以在OC的文件中调用C的方法了。
下面是我随便写的一个例子,在OC的.m文件中调用.c中的打印函数。
C语言的.h文件
//
// TestPrint.h
// TestDemo
//
// Created by Techsun on 14-8-12.
// Copyright (c) 2014年 techsun. All rights reserved.
//
#ifndef TestDemo_TestPrint_h
#define TestDemo_TestPrint_h
void printlog();
#endifC语言中.c文件//
// TestPrint.c
// TestDemo
//
// Created by Techsun on 14-8-12.
// Copyright (c) 2014年 techsun. All rights reserved.
//
#include <stdio.h>
#include "TestPrint.h"
void printlog(){
printf("hello world !!!");
}OC的.m文件//
// AClass.m
// TestDemo
//
// Created by Techsun on 14-8-12.
// Copyright (c) 2014年 techsun. All rights reserved.
//
#import "AClass.h"
#import "TestPrint.h"
@implementation AClass
- (void)printfhello{
printlog();
}
@end
本文介绍如何在Objective-C项目中调用C语言函数。通过创建并使用.h头文件来声明C函数,并在.c文件中实现这些函数,最终可以在Objective-C代码中顺利调用它们。
1148

被折叠的 条评论
为什么被折叠?



