简体   繁体   中英

Matlab error compiling C files with mex

i'm trying to compile a program for image deblurring. I try to run

mex apply_blur_kernel_mex.c

where the file apply_blur_kernel_mex.c have the following code

#include <mex.h>
#include <stdlib.h>
#include <math.h>
#include <matrix.h>
#include "ow_homography.h"
...
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
...
compute_homography_matrix(Ksharp, &theta_list[k*3], invKblurry, H);
...
}

The problem is in the function compute_homography_matrix that is in another file ow_homography.h

#ifndef OW_HOMOGRAPHY_H
#define OW_HOMOGRAPHY_H

#include "ow_mat3.h"

INLINE void compute_homography_matrix(const double *Ksharp, const double *theta, const double *invKblurry, double *H) {
    double R[9];
    /* Compute homography */
    cp3(invKblurry,H);
    rot3(theta[0],theta[1],theta[2],R);
    mmip3(R,H);
   mmip3(Ksharp,H);
}

This last operations (cp3, rot3...) are in another file ow_mat3.h that contains all the operations for the program. So when i try to call

mex apply_blur_kernel_mex.c

i have the following problem:

Error using mex
Undefined symbols for architecture x86_64:
    "_compute_homography_matrix", referenced from: 
       mexFunction in apply_blur_kernel_mex.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Any suggestions to solve this problem? Thank you all.

http://clang.llvm.org/compatibility.html#inline

C compatibility C99 inline functions

By default, Clang builds C code in GNU C11 mode, so it uses standard C99 semantics for the inline keyword. These semantics are different from those in GNU C89 mode, which is the default mode in versions of GCC prior to 5.0. For example, consider the following code:

 inline int add(int i, int j) { return i + j; } int main() { int i = add(4, 5); return i; } 

In C99, inline means that a function's definition is provided only for inlining, and that there is another definition (without inline) somewhere else in the program. That means that this program is incomplete, because if add isn't inlined (for example, when compiling without optimization), then main will have an unresolved reference to that other definition. Therefore we'll get a (correct) link-time error like this:

 Undefined symbols: "_add", referenced from: _main in cc-y1jXIr.o 

By contrast, GNU C89 mode (used by default in older versions of GCC) is the C89 standard plus a lot of extensions. C89 doesn't have an inline keyword, but GCC recognizes it as an extension and just treats it as a hint to the optimizer.

There are several ways to fix this problem:

1) Change add to a static inline function. This is usually the right solution if only one translation unit needs to use the function. static inline functions are always resolved within the translation unit, so you won't have to add a non-inline definition of the function elsewhere in your program.

2) Remove the inline keyword from this definition of add. The inline keyword is not required for a function to be inlined, nor does it guarantee that it will be. Some compilers ignore it completely. Clang treats it as a mild suggestion from the programmer.

3) Provide an external (non-inline) definition of add somewhere else in your program. The two definitions must be equivalent!

4)Compile in the GNU C89 dialect by adding -std=gnu89 to the set of Clang options. This option is only recommended if the program source cannot be changed or if the program also relies on additional C89-specific behavior that cannot be changed.

All of this only applies to C code; the meaning of inline in C++ is very different from its meaning in either GNU89 or C99.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM