Cannot load LJM library in MATLAB under Linux | LabJack
 

Cannot load LJM library in MATLAB under Linux

10 posts / 0 new
Last post
iandol
iandol's picture
Cannot load LJM library in MATLAB under Linux

Hi, if I try to load the LJM library in MATLAB I get lots of warnings and then errors (summary here, full log attached):

Error using loadlibrary Building libLabJackM_thunk_glnxa64 failed. Compiler output is: /home/linuxbrew/.linuxbrew/bin/gcc -I"/usr/local/MATLAB/R2019b/extern/include" -fexceptions -fPIC -fno-omit-frame-pointer -pthread -I"/home/cog1" -I"/usr/local/include" "libLabJackM_thunk_glnxa64.c" -o "libLabJackM_thunk_glnxa64.so" -Wl,-E -shared libLabJackM_thunk_glnxa64.c:33:34: error: expected declaration specifiers or '...' before string constant EXPORT_EXTERN_C (__visibility__("default")))intLJM_Close(intHandleThunk(void fcn(),const char *callstack,int stacksize)

I can load the exodriver library on the same system without any problems. Ubuntu 19.10 + MATLAB 2019b.

File Attachment: 
LabJack Support
labjack support's picture
LabJackM.h uses the attribute

LabJackM.h uses the attribute specifier from C++11: https://en.cppreference.com/w/cpp/language/attributes

Does compilation work if you add the following compile flag?

-std=c++11

iandol
iandol's picture
I'm not quite sure where this

I'm not quite sure where this can be passed in to MATLAB, the documentation for loadlibrary is here:

 

https://uk.mathworks.com/help/matlab/ref/loadlibrary.html

https://uk.mathworks.com/help/matlab/matlab_external/limitations-to-shar...

 

I have no experiience of C or C++, other than typing make on the commandline ;-)

 

iandol
iandol's picture
I found the file to edit in

I found the file to edit in MATLAB: ~/.matlab/R2019b/mex_C_glnxa64.xml --- and I edited it to add that flag:

 

https://gist.github.com/iandol/07242323696092a46c2e1ee1add9cc0d

 

But I get these errors:

 

Error using loadlibrary Building libLabJackM_thunk_glnxa64 failed.  Compiler output is: /usr/bin/gcc -I"/usr/local/MATLAB/R2019b/extern/include" -std=c++11 -fexceptions -fPIC -fno-omit-frame-pointer -pthread -I"/home/cog1" -I"/usr/local/include" "libLabJackM_thunk_glnxa64.c" -o "libLabJackM_thunk_glnxa64.so" -Wl,-E -shared cc1: warning: command line option ‘-std=c++11’ is valid for C++/ObjC++ but not for C libLabJackM_thunk_glnxa64.c:33:34: error: expected declaration specifiers or ‘...’ before string constant    33 | EXPORT_EXTERN_C  (__visibility__("default")))intLJM_Close(intHandleThunk(void fcn(),const char *callstack,int stacksize)       |                                  ^~~~~~~~~ libLabJackM_thunk_glnxa64.c:42:34: error: expected declaration specifiers or ‘...’ before string constant    42 | EXPORT_EXTERN_C  (__visibility__("default")))intLJM_CloseAll(voidThunk(void fcn(),const char *callstack,int stacksize)       |                                  ^~~~~~~~~
LabJack Support
labjack support's picture
Sorry, I see now that you

Sorry, I see now that you were compiling for C, not C++.

If you remove __attribute__((__visibility__("default"))) from LabJackM.h, what is the compilation output? Essentially, change:

    #else // Linux
        #define LJM_ERROR_RETURN __attribute__((__visibility__("default"))) int
        #define LJM_LONG_LONG_RETURN __attribute__((__visibility__("default"))) long long
        #define LJM_VOID_RETURN __attribute__((__visibility__("default"))) void
        #define LJM_ERROR_STRING __attribute__((__visibility__("default"))) const char *
        #define LJM_DOUBLE_RETURN __attribute__((__visibility__("default"))) double
    #endif

to:

    #else // Linux
        #define LJM_ERROR_RETURN int
        #define LJM_LONG_LONG_RETURN long long
        #define LJM_VOID_RETURN void
        #define LJM_ERROR_STRING const char *
        #define LJM_DOUBLE_RETURN double
    #endif

iandol
iandol's picture
Hi, yes, that works!

Hi, yes, that works!

 

>> header = '/usr/local/include/LabJackM.h'

>> library = '/usr/local/lib/libLabJackM.so'

>> [f,w]=loadlibrary(library,header)
Warning: The data type 'FcnPtr' used by function LJM_SetStreamCallback does not exist.
> In loadlibrary
Warning: The data type 'FcnPtr' used by function LJM_RegisterDeviceReconnectCallback does not exist.
> In loadlibrary

>> libisloaded('libLabJackM')

ans =  logical   1

Should I maintain my own modified header file, or will you change this in a future release?

 

LabJack Support
labjack support's picture
I think it's possible to use

I think it's possible to use macros to correctly select which version of the defines should be used. I will look into this.

iandol
iandol's picture
Thank you for your help!

Thank you for your help!

LabJack Support
labjack support's picture
We've released LJM 1.2100,

We've released LJM 1.2100, which uses a macro to determine which definition of e.g. LJM_ERROR_RETURN is used. The __attribute__ version is used only when LABJACK_LJM_ATTRIBUTE_VISIBILITY_DEFAULT is defined, so you should be able to build without altering LabJackM.h.

If you would like to test it out, we would be happy to hear your results. Currently, LJM 1.2100 is available as a beta installer:

https://labjack.com/support/software/installers/ljm

iandol
iandol's picture
Hi, I can confirm that the

Hi, I can confirm that the beta fixes the problem; thanks for the support!

 

Best wishes and stay safe, Ian