How to make an Objective-C project work on Ubuntu?
如何让Objective-C项目在Ubuntu上运行?
My files are:
我的文件是:
Fraction.h
Fraction.h
#import <Foundation/NSObject.h>
@interface Fraction: NSObject {
int numerator;
int denominator;
}
-(void) print;
-(void) setNumerator: (int) n;
-(void) setDenominator: (int) d;
-(int) numerator;
-(int) denominator;
@end
Fraction.m
Fraction.m
#import "Fraction.h"
#import <stdio.h>
@implementation Fraction
-(void) print {
printf( "%i/%i", numerator, denominator );
}
-(void) setNumerator: (int) n {
numerator = n;
}
-(void) setDenominator: (int) d {
denominator = d;
}
-(int) denominator {
return denominator;
}
-(int) numerator {
return numerator;
}
@end
main.m
main.m
#import <stdio.h>
#import "Fraction.h"
int main( int argc, const char *argv[] ) {
// create a new instance
Fraction *frac = [[Fraction alloc] init];
// set the values
[frac setNumerator: 1];
[frac setDenominator: 3];
// print it
printf( "The fraction is: " );
[frac print];
printf( "\n" );
// free memory
[frac release];
return 0;
}
I've tried two approaches to compile it:
我尝试了两种方法来编译它:
-
Pure gcc:
纯粹的gcc:
$ sudo apt-get install gobjc gnustep gnustep-devel $ gcc `gnustep-config --objc-flags` -o main main.m -lobjc -lgnustep-base /tmp/ccIQKhfH.o:(.data.rel+0x0): undefined reference to `__objc_class_name_Fraction'
-
I created a GNUmakefile Makefile:
我创建了一个GNUmakefile Makefile:
include ${GNUSTEP_MAKEFILES}/common.make TOOL_NAME = main main_OBJC_FILES = main.m include ${GNUSTEP_MAKEFILES}/tool.make
... and ran:
…,跑:
$ source /usr/share/GNUstep/Makefiles/GNUstep.sh $ make Making all for tool main... Linking tool main ... ./obj/main.o:(.data.rel+0x0): undefined reference to `__objc_class_name_Fraction'
So in both cases compiler gets stuck at
所以在这两种情况下编译器都被卡住了
undefined reference to `__objc_class_name_Fraction'
Do you have and idea how to resolve this issue?
你知道怎么解决这个问题吗?
6 个解决方案
#1
9
It's right. In both cases you did not include Fraction.m
in your list of files to be compiled, so it can't find the implementation of the class Fraction
这是正确的。在这两种情况下都不包含分数。m在要编译的文件列表中,因此无法找到类片段的实现
From the comment, this command works
从注释中,这个命令可以工作
gcc `gnustep-config --objc-flags` -o main *.m -lobjc -lgnustep-base
#2
4
I am not an expert at writing the make files like that, I find simply typing the following works on ubuntu quite well:
我不擅长写这样的制作文件,我发现简单地在ubuntu上输入下面的作品就很好:
gcc -I /usr/include/GNUstep/ -I /usr/include/mysql -L /usr/lib/GNUstep/\
-lgnustep-base -lmysqlclient\
-g -ggdb\
-fconstant-string-class=NSConstantString -o test *.m
I am using it on this project:
我正在这个项目中使用它:
http://github.com/uptecs/SmsgateDelivery/
http://github.com/uptecs/SmsgateDelivery/
If the above GCC command does not work you have not installed enough packages, use apt-cache to search for more gcc and objective c packages to install (I just installed more packages that looked relevant at random until it worked)
如果上面的GCC命令不工作,那么您就没有安装足够的包,使用apt-cache来搜索更多的GCC和objective c包来安装(我只是安装了更多看起来与随机相关的包,直到它成功)
#3
3
the make file:
使文件:
include ${GNUSTEP_MAKEFILES}/common.make
APP_NAME=Fraction
Fraction_HEADERS = Fraction.h
Fraction_OBJC_FILES = main.m Fraction.m
include ${GNUSTEP_MAKEFILES}/application.make
#4
2
The approach I just got working was (in Ubuntu, which is closely related to Debian):
我刚开始工作的方法是(在Ubuntu中,它与Debian紧密相关):
- Use Synaptic to install all likely-looking GnuStep packages;
- 使用Synaptic安装所有看起来像GnuStep的软件包;
- Source (
.
) the GnuStep startup script,/usr/share/GNUstep/Makefiles/GNUstep.sh
(this can go into.profile
or.bashrc
or something so you don't have to do it manually every time) - 源()。GnuStep启动脚本,/usr/ share/gnustep/makefiles/gnustepsh(它可以进入。profile .bashrc或其他内容,因此不必每次都手动执行)
- Create a
GNUmakefile
according to the instructions in A First Tool - 根据第一个工具中的说明创建一个GNUmakefile
This allowed me to successfully build command line programs.
这使我能够成功地构建命令行程序。
#5
2
Add Fraction.m in make file,
增加分数。在做文件,
include ${GNUSTEP_MAKEFILES}/common.make
包括$ { GNUSTEP_MAKEFILES } / common.make
TOOL_NAME = main
TOOL_NAME =主要
main_OBJC_FILES = main.m Fraction.m
main_OBJC_FILES =主要。m Fraction.m
include ${GNUSTEP_MAKEFILES}/tool.make
包括$ { GNUSTEP_MAKEFILES } / tool.make
and then make ^^
然后让^ ^
#6
2
Just add Fraction.m in main.m instead to add Fraction.h in main.m. And this will work. The following is the main.m file i used :
把分数。在主。m代入分数。在main.m h。这将工作。以下是主要内容。我使用的m文件:
#import <stdio.h>
#import "Fraction.m"
int main( int argc, const char *argv[] ) {
//create a new instance
Fraction *frac = [[Fraction alloc] init];
//set the values
[frac setNumerator: 1];
[frac setDenominator: 3];
//print it
printf("The fraction is : ");
[frac print];
printf("\n");
//free memory
[frac release];
return 0;
}
#1
9
It's right. In both cases you did not include Fraction.m
in your list of files to be compiled, so it can't find the implementation of the class Fraction
这是正确的。在这两种情况下都不包含分数。m在要编译的文件列表中,因此无法找到类片段的实现
From the comment, this command works
从注释中,这个命令可以工作
gcc `gnustep-config --objc-flags` -o main *.m -lobjc -lgnustep-base
#2
4
I am not an expert at writing the make files like that, I find simply typing the following works on ubuntu quite well:
我不擅长写这样的制作文件,我发现简单地在ubuntu上输入下面的作品就很好:
gcc -I /usr/include/GNUstep/ -I /usr/include/mysql -L /usr/lib/GNUstep/\
-lgnustep-base -lmysqlclient\
-g -ggdb\
-fconstant-string-class=NSConstantString -o test *.m
I am using it on this project:
我正在这个项目中使用它:
http://github.com/uptecs/SmsgateDelivery/
http://github.com/uptecs/SmsgateDelivery/
If the above GCC command does not work you have not installed enough packages, use apt-cache to search for more gcc and objective c packages to install (I just installed more packages that looked relevant at random until it worked)
如果上面的GCC命令不工作,那么您就没有安装足够的包,使用apt-cache来搜索更多的GCC和objective c包来安装(我只是安装了更多看起来与随机相关的包,直到它成功)
#3
3
the make file:
使文件:
include ${GNUSTEP_MAKEFILES}/common.make
APP_NAME=Fraction
Fraction_HEADERS = Fraction.h
Fraction_OBJC_FILES = main.m Fraction.m
include ${GNUSTEP_MAKEFILES}/application.make
#4
2
The approach I just got working was (in Ubuntu, which is closely related to Debian):
我刚开始工作的方法是(在Ubuntu中,它与Debian紧密相关):
- Use Synaptic to install all likely-looking GnuStep packages;
- 使用Synaptic安装所有看起来像GnuStep的软件包;
- Source (
.
) the GnuStep startup script,/usr/share/GNUstep/Makefiles/GNUstep.sh
(this can go into.profile
or.bashrc
or something so you don't have to do it manually every time) - 源()。GnuStep启动脚本,/usr/ share/gnustep/makefiles/gnustepsh(它可以进入。profile .bashrc或其他内容,因此不必每次都手动执行)
- Create a
GNUmakefile
according to the instructions in A First Tool - 根据第一个工具中的说明创建一个GNUmakefile
This allowed me to successfully build command line programs.
这使我能够成功地构建命令行程序。
#5
2
Add Fraction.m in make file,
增加分数。在做文件,
include ${GNUSTEP_MAKEFILES}/common.make
包括$ { GNUSTEP_MAKEFILES } / common.make
TOOL_NAME = main
TOOL_NAME =主要
main_OBJC_FILES = main.m Fraction.m
main_OBJC_FILES =主要。m Fraction.m
include ${GNUSTEP_MAKEFILES}/tool.make
包括$ { GNUSTEP_MAKEFILES } / tool.make
and then make ^^
然后让^ ^
#6
2
Just add Fraction.m in main.m instead to add Fraction.h in main.m. And this will work. The following is the main.m file i used :
把分数。在主。m代入分数。在main.m h。这将工作。以下是主要内容。我使用的m文件:
#import <stdio.h>
#import "Fraction.m"
int main( int argc, const char *argv[] ) {
//create a new instance
Fraction *frac = [[Fraction alloc] init];
//set the values
[frac setNumerator: 1];
[frac setDenominator: 3];
//print it
printf("The fraction is : ");
[frac print];
printf("\n");
//free memory
[frac release];
return 0;
}