首页 > 解决方案 > Objective C 和 automake

问题描述

我有一个非常小的 obj 程序

标题test.h


#import <Foundation/Foundation.h>

@interface Token : NSObject {
@private
  NSString * literal;
  size_t line;
  size_t column;
}
  @property (readonly) size_t line;
  @property (readonly) size_t column;
  @property (readonly) NSString * literal;
  + (id)newReturnTokenAtLine: (size_t) line column: (size_t) column;
  - (id)initWithLine: (size_t)aLine withColumn: (size_t)aColumn;
@end


@end

并且实现test.m

#import "test.h"


@implementation Token

@synthesize line;
@synthesize column;
@synthesize literal;


+ (id)newReturnTokenAtLine: (size_t) aLine column: (size_t) aColumn {
    Token * tok = [Token alloc];
    return (Token*)  [tok initWithLine: aLine column: aColumn];
}

- (id) initWithLine: (size_t) aLine withColumn: (size_t) aColumn {
    line = aLine;
    column = aColumn;
    return self;
}
@end

我的问题是,目标 C 编译器似乎认为 initWithLine 没有定义

test.m:13:27: error: instance method '-initWithLine:column:' not found (return type defaults to 'id') [-Werror,-Wobjc-method-access]
    return (Token*)  [tok initWithLine: aLine column: aColumn];
                          ^~~~~~~~~~~~~~~~~~~~~~~~~~
./test.h:5:12: note: receiver is instance of class declared here
@interface Token : NSObject {
           ^
1 error generated.

我错过了一些明显的东西吗?

我尝试在自动制作设置中使用它。因此configure.ac

define(MINIOBJC_CONFIGURE_COPYRIGHT,[[
public domain
]])

AC_INIT([miniobjc], [0.0.1])
AC_CONFIG_SRCDIR([test.m])
AC_CONFIG_MACRO_DIR([m4])
AC_CONFIG_AUX_DIR([build-aux])

AM_INIT_AUTOMAKE([foreign serial-tests])

AC_PROG_CC
AC_PROG_OBJC
AC_PROG_LIBTOOL

AC_CONFIG_FILES([Makefile])
AC_CONFIG_FILES([env],[chmod +x env])

AM_SILENT_RULES

AC_SUBST(OBJCFLAGS)
AC_SUBST(CFLAGS)

AC_OUTPUT

并且Makefile.am

lib_LTLIBRARIES = libminiobjc.la

libminiobjc_la_SOURCES = test.h test.m
libminiobjc_la_OBJCFLAGS = $(AM_CFLAGS) -Werror=objc-method-access

标签: objective-cautoconfautomakelibtool

解决方案


在 Objective-C 中,方法的名称包括所有参数标签和分号。-initWithLine:column:不存在,使用-initWithLine:withColumn:替代或替换

- (id) initWithLine: (size_t) aLine withColumn: (size_t) aColumn

经过

- (id) initWithLine: (size_t) aLine column: (size_t) aColumn

推荐阅读