Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
7002 Discussions

How to compile and run a simple program in C using MKL in linux

shitij90
Beginner
2,393 Views
Hi !!

I have tried googling and searching the answer on this site, but to no avail. I am a beginner in linux as well as MKL, but I can compile and run programs in linux (not using MKL) fine. Intel's manual is a tad too technical for me, I cant understand if i have to link statically, dynamically, and with what options......

This is a simple program I am trying to run:

----------------------------------------------------------

#include
#include "mkl.h"

/*below is the declaration as given in header file*/
/*float cblas_sasum(const MKL_INT N, const float *X, const MKL_INT incX);*/

int main()
{

int n=5,inc=1;
float arr[]={1,2,3,4,5};
float result;
result=cblas_sasum(n, arr, inc);

/*or sasum(&n,arr,&inc) ?? --- both give similar errors*/

printf("result");
return 0;
}

-------------------------------------------------------------

now the problems and doubts:
1. Is the program itself fine ?
2. How do I compile it? I read about long link commands for this and the intel's link advisor (and the manual), tried them as it is, but they dont work.
3. If I simply write "icc exp4.c", where exp4.c is the name of the program, I get the error:-

/usr/include/gnu/stubs.h(7): catastrophic error: could not open source file "gnu/stubs-32.h"
# include
^


compilation aborted for exp4.c (code 4)

what is this error ?

4. If I compile by gcc, which I guess I shouldnt/cant, but still.... I get

/tmp/ccCqahjj.o: In function `main':
exp4.c:(.text+0x53): undefined reference to `cblas_sasum'
collect2: ld returned 1 exit status


Thank you for your help in advance !!
0 Kudos
4 Replies
Gennady_F_Intel
Moderator
2,393 Views
We can recommend you to read the User's Guide first of all.
- after that please see at the examples which you can find into the package.For example the similar test you can find in ..\ComposerXE-2011\mkl\examples\cblas\source\ directoty ( on windows, but on linux it would be the similar folder).
-Please see the cblas_sasumx.c example.You can find there the makefiles which you can use for building these examples also.
- how to link with MKL? there are several KB articles, but first we recomend to see how to link with MKL - these article in the case if your version is 10.*
0 Kudos
Gennady_F_Intel
Moderator
2,392 Views

here is the link to the MKL Knowledge Base

 

0 Kudos
mecej4
Honored Contributor III
2,392 Views
Code that contains calls to special libraries, such as MKL, needs to be linked with these libraries in addition to the standard C and system libraries. This is done either by specifying compiler options, configuration files or simply by specifying the special library in the linker command.

Change

#include "mkl.h"

to

#include

Depending on the version of Linux and MKL, the command can be as simple as

icc -mkl xcblas.c

OR

with the help of the MKL Link Line Advisor Tool, with suitable choices for your setup,

[bash]$ gcc -I$MKLROOT/include xcblas.c -L $MKLROOT/lib/64 -L $MKLROOT/lib/64 -lmkl_solver_lp64_sequ
ential -Wl,--start-group -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -Wl,--end-group -lpthread -lm[/bash]
0 Kudos
shitij90
Beginner
2,392 Views
Thank you so much guys.
It finally worked after I tried to understand what the huge link line was about, and after being more comfortable with linux commands. I have a different problem now, which I suppose I should ask in a different thread.
Thanks again !!
0 Kudos
Reply