0

I want to compile sample c code from Rosettacode. I want to adapt it from my iOS project to do a simple matrix multiply where one of the 2 matrices is just a row, not a row and a column. The Rosettacode is as follows.

#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 but I am getting compile errors and need more guidance.

The xcode 4.5.2 project is currently for a Mac, but ultimately will be part of a project for iOS.

The compile errors I get are as follows.

Undefined symbols for architecture x86_64:
  "_dot", referenced from:
      _mat_mul in code.o
ld: symbol(s) not found for architecture x86_64

I have placed the code in a file named code.m which has its own function main() and I wonder if that conflicts with the file main.m's main() function, for example?

Also, I would like to know how to actually see the resulting demo which I suppose requires using NSLog in the code.

Btw, in the array db[], what does the data notation using / mean? Does it signify a rational fraction?

Community
  • 1
  • 1
zerowords
  • 2,915
  • 4
  • 29
  • 44
  • 2
    what happens when you add the keyword "`static`" in front of the inline function declaration? – Michael Dautermann Dec 30 '12 at 14:48
  • `static` must have helped. Now the only error is duplicate symbol _main in: /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 I guess that means my suspicions about two mains is correct. How do I fix it? – zerowords Dec 30 '12 at 14:56
  • Ok, I changed main() to mat() and added `mat();` inside main.m's `main(..)`. Now I get a build with a warning "Implicit declaration of function 'mat' is invalid in C99", and I see a 4x4 identity matrix in the console. Is that as good as it gets? The Application window is just a blank window. – zerowords Dec 30 '12 at 15:26
  • `int mat();` fixed the warning. I'm slow, but I'm bad . Do I answer my own question or does Michael Dautermann get to do it? Btw, why is `static` required? How do I get to see the output? – zerowords Dec 30 '12 at 15:51
  • 2
    Roughly: the `inline` without `static` makes `dot` a declaration that could be used if the compiler made use of its inline properties, but the compiler did not inline the code and needed a definition. The rules for `inline` are tricky. See [extern inline](http://stackoverflow.com/q/216510/15168) and [inline function undefined symbols error](http://stackoverflow.com/q/10243018/15168). – Jonathan Leffler Dec 30 '12 at 16:42
  • I'd give Michael his due, but then that's just because he's a really nice guy and deserves it. :-) – user1118321 Dec 30 '12 at 17:43
  • "xcode does not compile c source" - True, only the compiler does. And since Xcode is not a compiler, ... –  Dec 30 '12 at 20:35
  • @user1118321 Jokes aside, I don't know what you mean. How can I give Michael his due? Doesn't he have to answer the question himself to get credit? I don't really understand the system here. – zerowords Dec 30 '12 at 20:53
  • @zerowords He'd have to enter it as an answer, you then you could check the green checkmark next to it to accept it as the answer. – user1118321 Dec 31 '12 at 00:55

1 Answers1

0

Add the keyword static in front of an inline function declaration and the compiler should be much happier.

Robert Rouhani
  • 14,512
  • 6
  • 44
  • 59
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215