0

I encounter a problem when using cblas. I am new to use C++ to do numerics and I know Openblas is one of the famous library to perform linear algebra computation. I use brew install openblas in my M1 Macbook. When the installation finishes, I follow the output instruction by simply typing these commands in terminal. However, when I use the following example to test openblas,

#include <iostream>
#include "cblas.h"
#include "lapacke.h"

using namespace std;

int main(){
    float b[] = {3, 1, 3, 1, 5};
    cblas_sasum(5, b, 1);
    cout << "Program finished";
}

Xcode says that fatal error: 'cblas.h' file not found. Therefore, I am wondering how to solve this issue. I appreciate any comment.

Ricky Pang
  • 101
  • 3
  • What is the command you use to compile? Can we see the full command? Probably you restarted the terminal after setting CPPFLAGS or for some reason the env variable is not read by the compiler. – Fra93 Jun 29 '22 at 13:01
  • Hi, @Fra93, I just type ```export LDFLAGS="-L/opt/homebrew/opt/openblas/lib"``` and ```export CPPFLAGS="-I/opt/homebrew/opt/openblas/include"``` in terminal – Ricky Pang Jun 29 '22 at 13:04
  • What is then the command that you use for compilation? Also, is `cblas.h` present in the `/opt/homebrew/opt/openblas/include`? – Fra93 Jun 29 '22 at 14:01
  • Suppose I save the above example as main.cpp , then I use ```g++ -I/opt/homebrew/opt/openblas/include main.cpp -o main``` in terminal. The output is ```Undefined symbols for architecture arm64: "_cblas_sasum", referenced from: _main in main-cf8342.o ld: symbol(s) not found for architecture arm64``` – Ricky Pang Jun 29 '22 at 14:23

1 Answers1

0

After several trial and error, I solve my problem by writing the following makefile:

CC = g++
INCLUDES = -I/opt/homebrew/Cellar/openblas/0.3.20/include
CPPFLAGS = -g -Wall $(INCLUDES)
LDFLAGS = -L/opt/homebrew/opt/openblas/lib

all:main.o /opt/homebrew/opt/openblas/lib/libopenblas.a
        $(CC) main.o $(LDFLAGS) -lopenblas -o main.out

main.o:main.cpp /opt/homebrew/Cellar/openblas/0.3.20/include/cblas.h
        $(CC) $(CPPFLAGS) -c main.cpp
clean:
        rm -f main.out

I follow this post to write a makefile and it does work. To use Xcode to compile the main.cpp file, we need to add linker into Xcode. The instruction is discussed in this youtube video

Ricky Pang
  • 101
  • 3