C函数总是返回零目标C [英] C function always returns zero to Objective C

查看:104
本文介绍了C函数总是返回零目标C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我结合一些辅助函数C文件的目标C项目。我有一个严重和非常讨厌的问题试图从C文件返回浮动秒。

I have an Objective C project incorporating a C file with some helper functions. I have a grave and very irritating problem trying to return floats from the C file.

C文件:

float returnFloat() {
    return 10.0;
}

与此同时,在一个目标C实例方法:

Meanwhile in an Objective C instance method:

float x;
x = returnFloat();

x是永远0.000000。任何想法我做错了吗?

x is always 0.000000. Any ideas what I'm doing wrong?

修改

OK,我已经意识到我有一堆的目标C文件隐式声明的警告,有关使用我有在C文件中的函数。

OK, I've realised I have a bunch of "implicit declaration" warnings in the Objective C file, relating to use of the functions I have in the C file.

使用返回 INT s的做工精细的功能分配。凡分配从函数返回一个浮动制作,调试器说:变量优化掉由编译器。

Assignments using functions that return ints are working fine. Where an assignment is made from a function returning a float, the debugger says "variable optimized away by compiler".

时很可能我不使用正确的方式包括含有在目标C项目辅助函数C文件?我有(愚蠢?)只是令X code链路它自动地。即便如此,为什么这个问题文件中体现,只有当函数返回一个浮动

Is it likely I'm not using the "correct" way to include a C file containing helper functions in an Objective C project? I have (stupidly?) just let Xcode link it in automagically. Even if so, why should this problem manifest only when the function is returning a float?

推荐答案

您必须使用.h文件,就像.m文件,申报你在另一个文件中做什么。所以,你需要像这样的这种情况下(这是不完全):

You have to use a .h file, just like with .m files, to declare what you're doing in another file. So, you need something like this for this scenario (these are incomplete):

returnfloat.c

float returnFloat() {
    return 10.0;
}

returnfloat.h

float returnFloat(void);

usefloat.m

#import "returnfloat.h"

- (void) someMethod {
    float ten = returnFloat();
}

问题(由你的隐式声明的警告送人)是编译器是假设您正在调用的东西,会返回一个 INT ID ,而不是浮动。当你用C的工作,事情需要原型(GCC会把像C .c文件,和所有的C规则,即使你在一个Objective-C的项目是)。

The problem (given away by your "implicit declaration" warnings) is that the compiler is assuming that you are calling something that returns an int or id, not a float. When you work with C, things need to be prototyped (GCC will treat the .c file like C, and all C rules apply, even though you're in an Objective-C project).


如果你想看到一个例子,这里是从我的项目之一的东西 - 生产code(你可以在.M结尾的文件纯C写的,而GCC将像对待的Objective-C在某些方面):

If you'd like to see an example, here's something from one of my projects -- production code (you can write pure C in a file ending in .m, and GCC will treat it like Objective-C in some ways):

DebugF.m

#import "DebugF.h"

void __Debug(const char *file, int line, NSString *format, ...) {
#ifdef DEBUG
    /* Wraps NSLog() with printf() style semantics */
#endif
}

DebugF.h

#ifndef __DEBUGF_H_INCLUDED__
#define __DEBUGF_H_INCLUDED__

#ifdef DEBUG
#define DebugF(args...) __Debug(__FILE__, __LINE__, args)
#else
#define DebugF(...)
#endif /* DEBUG */

void __Debug(const char *file, int line, NSString *fmt, ...);

#endif /* __DEBUGF_H_INCLUDED */

SomeViewController.m

DebugF(@"Got these arguments: %u, %@, %@", 4, anObject, [anObject somethingElse]);

这篇关于C函数总是返回零目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆