cxcodelinkeraccelerate

How do I link the Accelerate Framework to a c program in MacOs?


I just started with c development and I need to compile and link a program which uses the Accelerate Framework from Apple: Simple example accelerate.c:

#include <stdio.h>
#include <Accelerate/Accelerate.h>

double vectorvector_product(double * a, double * b, int dim){
    // This function returns in res the elementwiseproduct between a and b,
    // a and b must have the same dimension dim.
    return cblas_ddot(dim,a,1,b,1);
}

int main(){
        double a[4] = {1.0,2.0,3.0,4.0};
        double b[4] = {1.0,2.0,3.0,4.0};
        double res = vectorvector_product(a,b,4);
        printf("Res: %f",res);
}

I compiled it with clang:

>>> cc -Wall -g -c accelerate.c

And obtained a new file accelerate.o What would I do now in order to properly link it? All I know is that this Accelerate framework is located at /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Accelerate.framework

>>> ls /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/Accelerate.framework

Accelerate.tbd  Frameworks  Headers     Modules     Versions

p.s.: If I Run this program with Xcode it magically works, but I need to do it from the command line and I would like to know what I'm doing.


Solution

  • Apparently the correct way to link Accelerate.h is by passing -framework Accelerate as argument e.g.

    >>> cc -framework Accelerate accelerate.c
    

    will compile and link accelerate.c by generating an executable a.out.