c - xcode不编译c源代码

标签 c xcode

我要编译sample c code from Rosettacode 。我想从我的 iOS 项目中改编它来进行简单的矩阵乘法,其中 2 个矩阵之一只是一行,而不是一行和一列。 Rosetta代码如下。

#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 的 main() 函数?

此外,我想知道如何实际查看生成的演示,我认为这需要在代码中使用 NSLog

顺便说一句,在数组db[]中,使用/的数据表示法是什么意思?它表示有理分数吗?

最佳答案

在内联函数声明前面添加关键字static,编译器应该会更高兴。

关于c - xcode不编译c源代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14091592/

相关文章:

c - 如何终止非阻塞套接字连接尝试?

c - 从函数获取字符指针

ios - 如何在 swift iOS 中使用 swift 高阶函数从本地 json 创建 ViewModel

iphone - 您可以在 IB 中建立一个按钮并以编程方式移动它吗?

ios - 在 iTunes 连接上上传应用程序后在电子邮件中出现问题,意外的 CFBundleExecutable key

c - 在 union 中使用多个相同类型的结构

c++ - 包含目录 Visual Studio 2010

c++ - 记录极少量数据的最有效方法是什么?

iphone - 用于比较 Xcode 项目文件的工具

iphone - Xcode 组和文件 : How can I copy files from project A into project B without getting crazy?