0
我想编译sample c code from Rosettacode。我想从我的iOS项目中调整它来做一个简单的矩阵乘法,其中2个矩阵中的一个只是一行,而不是一行和一列。 Rosettacode如下。xcode不编译c源码
#include <stdio.h>
#include <stdlib.h>
/* Make the data structure self-contained. Element at row i and col j
is x[i * w + j]. More often than not, though, you might want
to represent a matrix some other way */
typedef struct { int h, w; double *x;} matrix_t, *matrix;
inline double dot(double *a, double *b, int len, int step)
{
double r = 0;
while (len--) {
r += *a++ * *b;
b += step;
}
return r;
}
matrix mat_new(int h, int w)
{
matrix r = malloc(sizeof(matrix) + sizeof(double) * w * h);
r->h = h, r->w = w;
r->x = (double*)(r + 1);
return r;
}
matrix mat_mul(matrix a, matrix b)
{
matrix r;
double *p, *pa;
int i, j;
if (a->w != b->h) return 0;
r = mat_new(a->h, b->w);
p = r->x;
for (pa = a->x, i = 0; i < a->h; i++, pa += a->w)
for (j = 0; j < b->w; j++)
*p++ = dot(pa, b->x + j, a->w, b->w);
return r;
}
void mat_show(matrix a)
{
int i, j;
double *p = a->x;
for (i = 0; i < a->h; i++, putchar('\n'))
for (j = 0; j < a->w; j++)
printf("\t%7.3f", *p++);
putchar('\n');
}
int main()
{
double da[] = { 1, 1, 1, 1,
2, 4, 8, 16,
3, 9, 27, 81,
4,16, 64, 256 };
double db[] = { 4.0, -3.0, 4.0/3,
-13.0/3, 19.0/4, -7.0/3,
3.0/2, -2.0, 7.0/6,
-1.0/6, 1.0/4, -1.0/6};
matrix_t a = { 4, 4, da }, b = { 4, 3, db };
matrix c = mat_mul(&a, &b);
/* mat_show(&a), mat_show(&b); */
mat_show(c);
/* free(c) */
return 0;
}
I have found some pointers here但我收到编译错误,需要更多的指导。
xcode 4.5.2项目目前用于Mac,但最终将成为iOS项目的一部分。
我得到的编译错误如下。
Undefined symbols for architecture x86_64:
"_dot", referenced from:
_mat_mul in code.o
ld: symbol(s) not found for architecture x86_64
我已经把代码在一个名为code.m
文件,它有它自己的功能main()
,我不知道这是否与该文件main.m's
main()
功能相冲突,例如?
此外,我想知道如何实际看到最终演示,我认为需要在代码中使用NSLog
。
顺便说一句,在数组db []中,使用/
的数据表示法是什么意思?它是否意味着一个理性的分数?
当您在内联函数声明前添加关键字“'static”时会发生什么? –
'static'肯定有帮助。现在唯一的错误是重复的符号_main: /Users/brian/Library/Developer/Xcode/DerivedData/MultRosetta-eqcsuymbbjlarqbdkhgvhlqgvlaw/Build/Intermediates/MultRosetta.build/Debug/MultRosetta.build/Objects-normal/x86_64/main。 o /Users/brian/Library/Developer/Xcode/DerivedData/MultRosetta-eqcsuymbbjlarqbdkhgvhlqgvlaw/Build/Intermediates/MultRosetta.build/Debug/MultRosetta.build/Objects-normal/x86_64/code.o我想这意味着我对两个人的怀疑主线是正确的。我如何解决它? – zerowords
好吧,我将main()更改为mat(),并在main.m的main(..)内添加了'mat();'。现在我得到一个警告“隐式声明的函数'mat'在C99中是无效的”,并且我在控制台中看到一个4x4的单位矩阵。这样就好吗?应用程序窗口只是一个空白窗口。 – zerowords