English 中文(简体)
目标C
原标题:Objective C compilation with gcc 4.6.2

我正试图在窗口上学习目标c。 我的方案以警告汇编成册。

我的法典

#include <objc/Object.h>

@interface Greeter:Object
{
  /* This is left empty on purpose:
   ** Normally instance variables would be declared here,
   ** but these are not used in our example.
   */
}

- (void)greet;

@end

#include <stdio.h>

@implementation Greeter

- (void)greet
{
    printf("Hello, World!
");
}

@end

#include <stdlib.h>

int main(void)
{
    id myGreeter;
    myGreeter=[[Greeter alloc] init];
    [myGreeter greet];
    [myGreeter release];
    return EXIT_SUCCESS;
}

我利用以下指挥机构汇编了我关于“全球疫苗”的方案。

 gcc -o Greeter Greeter.m -I /GNUstep/System/Library/Headers -L /GNUstep/System/Libra
 /Libraries -lobjc -lgnustep-base -fconstant-string-class=NSConstantString

我收到以下关于汇编的警告:

:  Greeter  may not respond to  +alloc  [enabled by default]
: (Messages without a matching method signature [enabled by default]
: will be assumed to return  id  and accept [enabled by default]
:  ...  as arguments.) [enabled by default]
: no  -init  method found [enabled by default]
: no  -release  method found [enabled by default]

因此,当我听从我的可耻感时,这个物体不会被当即。

我正在使用来自MinGW的电梯,该电离层4.6.2。

------

当我从NSObject而不是目标延伸时,该方案会受到罚款。

页: 1

我的目标。

#include <objc/runtime.h>

@interface Object
{
    Class isa;
}
@end

页: 1

我修改了我的法典如下。 这部法律汇编了罚款,但我不敢肯定这是否是处理事情的正确途径。

@interface Greeter
{
  /* This is left empty on purpose:
   ** Normally instance variables would be declared here,
   ** but these are not used in our example.
   */
}

- (void)greet;

+ (id)alloc;
- (id)init;
- release;

@end

#include <stdio.h>

@implementation Greeter

- (void)greet
{
    printf("Hello, World!
");
}

+ (id)alloc
  {
    printf("Object created");
    return self;
  }

- (id)init
  {
    printf("Object instantiated");
    return self;
  }

- release {}

@end

#include <stdlib.h>

int main(void)
{
    id myGreeter;
    myGreeter=[[Greeter alloc] init];
    [myGreeter greet];
    [myGreeter release];
    return EXIT_SUCCESS;
}
最佳回答

<代码> 目标类别不执行保留项,因此t有release/code>, ll有free或其他一些方法。 它应当有<代码>+alloc和-init。 由于没有“客观标准”,你不得不打开<条码>objc/Object.h,并确切看到它所提供的东西。

注: 关于海合会4.6.2,objc/Object.h 实际包括<代码>objc/deprecated/Object.h,即支持Object,作为类别可能相当有限。 如果其中不包括:

#import <objc/deprecated/Object.h>
问题回答

除非你研究目标C的历史,否则试图根据目标类别学习语言是完全浪费时间的。 The Object category was final used Common as a fundamental category in pre-1994 NEXTSTEP.

如果您的目标is到1994年之前学习 因此,目标C指出,如果是,你迄今的答复完全是错误的。 即使目标在于采用现代模式,答案也更加接近<代码>。 我怎么能重新确定NSObject?。 请注意,如果<>>>> 是您的目标......,......这好! 1994年以前 目标C与OOP宏观评估一样,通过这种评估,在金属简单化时有一吨权力。

例如,你说,“我修改了我的法典如下。 这部法律汇编了罚款,但我不清楚这是否是处理事情的正确途径”。

That code compiles, but -- no -- it doesn t work. Not at all. For starters, the +alloc method doesn t actually allocate anything. Nor does the Greeter class implement near enough functionality to act anything like an NSObject.

如果你的目标是学习与现代目标C相近的东西,并利用Windows这样做,那么最好的方法可能是安装全球需求评估工具链。 至少,你将用<条码>NSObject编制一套类似于现代可可的原样(以及在较小的程度上是SOS)。

If your goal is to learn truly modern Objective-C, you ll want an environment that can run the latest versions of LLVM, at the very least. And, of course, if you want to write Objective-C based iOS or Mac OS X apps, you ll want a Mac running Lion.

进口基金会。

#import <Foundation/Foundation.h>

扩大国家统计局,而不是目标。

@interface Greeter : NSObject

What I did was to install gcc-4.6 alongside the 4.7 that came with the linux system. It seems to work, as it has a compatability layer 页: 1older code. In my basic Makefile I specify

>     CC=gcc-4.6
>     LIBS=-lobjc -lpthread
>     
>     all:       objc-test.m
>         $(CC)  -o objctest  objc-test.m $(LIBS)

There is nothing "wrong" with using and older version of gcc. The new 4.7 version has gutted the objc system so it is not a stand-alone compilation suite. That sucks. I imagine there is some reason, possibly a political one, possibly just that it is difficult to make one compiler do it all 页: 1everyone. I have successfully made objc programs with gnustep in X86_64 Linux with gcc 4.7.3 after banging out failure 页: 1two days the old way.

它涉及一个建筑群:

1. 建立具有环境变量的环境变量

source /usr/share/GNUstep/Makefiles/GNUstep.sh

and conforming to their build system.
A Basic GNUmakefile:

include $(GNUSTEP_MAKEFILES)/common.make

TOOL_NAME = test

test_OBJC_FILES = main.m  

include $(GNUSTEP_MAKEFILES)/tool.make

页: 1

主 席:

#import <Foundation/Foundation.h>

int
main (void)
{ 
  NSLog (@"Executing");
  return 0;
}

运行

g _

1. 以代号<>obj建造二元。

It is actually quite frustrating to fight with the build system like that and have to spend hours teasing out tidbitsfrom docs just to get basic functionality from such a great compiler. I hope they fix it in coming iterations.

Have you tried with [Greeter new]; ? Open Object.h and take a look at the methods defined in the Object class...

EDIT: To implement the alloc, retain and release you have to call the objc runtime. So.. I think you have to write something like this:

@interface RootObject : Object
+ (id)alloc;
- (id)init;
- (id)retain;
- (void)release;
@end


@implementation RootObject
{
    unsigned int retainCount;
}

+ (id)alloc
{
    id myObj = class_createInstance([self class], 0);
    /* FOR NEWBIES this works for GCC (default ubuntu 20.04 LTS)
    id myObj = class_createInstance(self, 0);
    */ 
    return myObj;
}
- (id)init
{
    retainCount = 1;
    return self;
}
- (id)retain
{
    retainCount++;
    return self;
}
- (void)release
{
    retainCount--;
    if (retainCount == 0) {
        object_dispose(self);
    }
}

@end

那么,你就能够下根基。





相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签