Thread: Comment inclure des fichiers d'en-tête dans les fichiers Mex

  1. #1
    Registered User
    Join Date
    Oct 2016
    Posts
    8

    Comment inclure des fichiers d'en-tête dans les fichiers Mex

    Salut,
    je suis en train de créer un fichier Mex à partir du code C sur Matlab. qui appelle une fonction de C (avec le fichier d'en-tête afficheData.h)
    #include "afficheData.h"
    J'ai essayé d'appeler mexfunction dans MATLAB. Mais je reçois une erreur : undefined reference to '_afficheData' .même si la fonction afficheData est déjà définie.
    Est-ce que quelqu'un sait quel est le problème ici?
    Merci d'avance

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    This forum, and most related to programing, are English based.

    Please post your question in English, and we will try to help.

    You have our apologies for not being able to be multi-lingual.

  3. #3
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Hello
    I am trying to create a Mex file from C on Matlab code. calling a C function (with theafficheData.h header file)
    #include "afficheData.h"
    I tried to call mexfunction in MATLAB. But I get an error: undefined reference to '_afficheData' even if the afficheData function is already defined.
    Anyone know what is the problem?
    Thanks in advance

  4. #4
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    How are you compiling the C file? Are you linking against the "mex" (or whatever) library? Just including the .h file in the source code doesn't provide the actual function definitions.

    Looking here, there seems to be a special tool for compiling the C source since it doesn't contain a "main" function but instead just a "mexFunction" function.

  5. #5
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    I have the function "mexFunction" and I have another function "afficheData"in another file and I defined the header .

    /******************************************
    mexFunction.c************************************* ****/
    Code:
    
    #include <stdio.h>
    #include <stdlib.h>
    #include "mex.h"
    #include "data_function.h"
    void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) 
    {
        double *sx, *sy, *coef;
        int mc, nc, msx, msy, count = 0;   
        if (nrhs != 3)
        {
            mexErrMsgTxt("Three inputs required.");
        }
        sx = mxGetPr(prhs[0]);
        msx = mxGetM(prhs[0]);
        sy = mxGetPr(prhs[1]);
        msy = mxGetM(prhs[1]);
        coef = mxGetPr(prhs[2]);
        mc = mxGetM(prhs[2]);
        nc = mxGetN(prhs[2]);
        afficheData("Coef", coef, mc, nc);
                
    }

    /*********************************data_function.h** ***************************************/
    Code:
    void afficheData(char *Name, double *Data, int M, int N);

    /******************************data_function.c***** **************************************/
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include "mex.h"
    void afficheData(char *Name, double *Data, int M, int N)
    {
        int m, n;
        mexPrintf("%s = \n", Name);
        for(m = 0; m < M; m++, mexPrintf("\n"))
            for(n = 0; n < N; n++)
                mexPrintf("%8.4f ", Data[m + M*n]);
    }

    compilation error :
    >> mex mexFunction.c
    Writing library for mexFunction.mexw32
    c:\users\abdou\appdata\local\temp\mex_wqly2n\mexfu nction.obj .text: undefined reference to '_afficheData'

    C:\PROGRA~1\MATLAB\R2013A\BIN\MEX.PL: Error: Link of 'mexFunction.mexw32' failed.

  6. #6
    Registered User
    Join Date
    Jun 2015
    Posts
    1,640
    Try adding the other file's source file name (or object file name). Something like:
    >> mex mexFunction.c afficheData.c

  7. #7
    Registered User
    Join Date
    Oct 2016
    Posts
    8
    Thank you brother

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 15
    Last Post: 03-07-2011, 12:48 PM
  2. need a pro to look over and comment.
    By Ciaran789 in forum C Programming
    Replies: 7
    Last Post: 04-25-2010, 07:29 AM
  3. Comment using // or /* &&& */
    By Roaring_Tiger in forum C Programming
    Replies: 3
    Last Post: 03-16-2005, 02:45 PM
  4. Need comment
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-24-2001, 08:05 PM

Tags for this Thread