简体   繁体   English

Matlab 2013b生成的代码在mex中生成体系结构x86_64错误的未定义符号

[英]Matlab 2013b generated code produces Undefined symbols for architecture x86_64 error in mex

I have a C-Code generated by the Simulink Coder that I need to use in a C S-function. 我有一个Simulink编码器生成的C代码,需要在C S函数中使用。

This may sound a bit strange but I need this Code to be executed multiple times within the s-function before writing the outputs of the s-function (it's an evolutionary algorithm that needs to simulate a prediction for a lot of individuals before evaluation and ranking...but those details do not really matter). 这听起来可能有点奇怪,但我需要在编写s函数的输出之前在s函数内多次执行此代码(这是一种进化算法,需要在评估和排名之前为很多人模拟预测...但这些细节并不重要)。

The Problem is that I seem to have a problem with the 64bit instruction set when trying to mex my code. 问题是尝试混合代码时,我似乎对64位指令集有问题。

I am taking the generated erg_main.c as an example on how to interact with the generated code: 我以生成的erg_main.c为例,说明如何与生成的代码进行交互:

/*
 * File: ert_main.c
 *
 * Code generated for Simulink model 'Peltier_model'.
 *
 * Model version                  : 1.14
 * Simulink Coder version         : 8.5 (R2013b) 08-Aug-2013
 * C/C++ source code generated on : Thu Apr  3 18:01:46 2014
 *
 * Target selection: ert.tlc
 * Embedded hardware selection: 32-bit Generic
 * Emulation hardware selection:
 *    Differs from embedded hardware (MATLAB Host)
 * Code generation objective: Debugging
 * Validation result: Passed (0), Warning (1), Error (0)
 */

#include <stdio.h>                     /* This ert_main.c example uses printf/fflush */
#include "Peltier_model.h"             /* Model's header file */
#include "rtwtypes.h"

/*
 * Associating rt_OneStep with a real-time clock or interrupt service routine
 * is what makes the generated code "real-time".  The function rt_OneStep is
 * always associated with the base rate of the model.  Subrates are managed
 * by the base rate from inside the generated code.  Enabling/disabling
 * interrupts and floating point context switches are target specific.  This
 * example code indicates where these should take place relative to executing
 * the generated code step function.  Overrun behavior should be tailored to
 * your application needs.  This example simply sets an error status in the
 * real-time model and returns from rt_OneStep.
 */
void rt_OneStep(void)
{
  static boolean_T OverrunFlag = 0;

  /* Disable interrupts here */

  /* Check for overrun */
  if (OverrunFlag) {
    rtmSetErrorStatus(Peltier_model_M, "Overrun");
    return;
  }

  OverrunFlag = TRUE;

  /* Save FPU context here (if necessary) */
  /* Re-enable timer or interrupt here */
  /* Set model inputs here */

  /* Step the model */
  Peltier_model_step();

  /* Get model outputs here */

  /* Indicate task complete */
  OverrunFlag = FALSE;

  /* Disable interrupts here */
  /* Restore FPU context here (if necessary) */
  /* Enable interrupts here */
}

/*
 * The example "main" function illustrates what is required by your
 * application code to initialize, execute, and terminate the generated code.
 * Attaching rt_OneStep to a real-time clock is target specific.  This example
 * illustates how you do this relative to initializing the model.
 */
int_T main(int_T argc, const char *argv[])
{
  /* Unused arguments */
  (void)(argc);
  (void)(argv);

  /* Initialize model */
  Peltier_model_initialize();

  /* Attach rt_OneStep to a timer or interrupt service routine with
   * period 0.01 seconds (the model's base sample time) here.  The
   * call syntax for rt_OneStep is
   *
   *  rt_OneStep();
   */
  printf("Warning: The simulation will run forever. "
         "Generated ERT main won't simulate model step behavior. "
         "To change this behavior select the 'MAT-file logging' option.\n");
  fflush((NULL));
  while (rtmGetErrorStatus(Peltier_model_M) == (NULL)) {
    /*  Perform other application tasks here */
  }

  /* Disable rt_OneStep() here */

  /* Terminate model */
  Peltier_model_terminate();
  return 0;
}

/*
 * File trailer for generated code.
 *
 * [EOF]
 */

For now I am just trying to initialize my model using the Peltier_model_initialize(); 现在,我只是尝试使用Peltier_model_initialize();初始化模型Peltier_model_initialize(); and including those headers from the erg_main.c 并包括来自the erg_main.c标头

trying to mex this results in the following: 尝试将其与以下结果混合:

**************************************************************************
  Warning: Neither -compatibleArrayDims nor -largeArrayDims is selected. 
           Using -compatibleArrayDims. In the future, MATLAB will require
           the use of -largeArrayDims and remove the -compatibleArrayDims
           option. For more information, see:
           http://www.mathworks.com/help/matlab/matlab_external/upgrading-mex-files-to-use-64-bit-api.html
**************************************************************************

-> mexopts.sh sourced from directory (DIR = $PREF_DIR)
   FILE = /Users/JoachimSA/.matlab/R2013b/mexopts.sh
----------------------------------------------------------------
->    MATLAB                = /Applications/MATLAB_R2013b.app
->    CC                    = xcrun  -sdk macosx10.9  clang
->    CC flags:
         CFLAGS             = -fno-common -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9  -fexceptions
         CDEBUGFLAGS        = -g
         COPTIMFLAGS        = -O2 -DNDEBUG
         CLIBS              = -L/Applications/MATLAB_R2013b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments          =  -DMX_COMPAT_32
->    CXX                   = xcrun  -sdk macosx10.9  clang++
->    CXX flags:
         CXXFLAGS           = -fno-common -fexceptions -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9
         CXXDEBUGFLAGS      = -g
         CXXOPTIMFLAGS      = -O2 -DNDEBUG
         CXXLIBS            = -L/Applications/MATLAB_R2013b.app/bin/maci64 -lmx -lmex -lmat -lstdc++
         arguments          =  -DMX_COMPAT_32
->    FC                    = gfortran
->    FC flags:
         FFLAGS             = -fexceptions -m64 -fbackslash
         FDEBUGFLAGS        = -g
         FOPTIMFLAGS        = -O
         FLIBS              = -L/Applications/MATLAB_R2013b.app/bin/maci64 -lmx -lmex -lmat -L -lgfortran -L -lgfortranbegin
         arguments          =  -DMX_COMPAT_32
->    LD                    = xcrun -sdk macosx10.9 clang
->    Link flags:
         LDFLAGS            = -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2013b.app/extern/lib/maci64/mexFunction.map
         LDDEBUGFLAGS       = -g
         LDOPTIMFLAGS       = -O
         LDEXTENSION        = .mexmaci64
         arguments          = 
->    LDCXX                 = 
->    Link flags:
         LDCXXFLAGS         = 
         LDCXXDEBUGFLAGS    = 
         LDCXXOPTIMFLAGS    = 
         LDCXXEXTENSION     = 
         arguments          = 
----------------------------------------------------------------

-> xcrun  -sdk macosx10.9  clang -c  -I/Applications/MATLAB_R2013b.app/rtw/c/src -I/Users/JoachimSA/Documents/WICHTIG/Diplomarbeit/Matlab_FIles/Peltier_Codegen/Peltier_model_ert_rtw -I/Applications/MATLAB_R2013b.app/extern/include -I/Applications/MATLAB_R2013b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9  -fexceptions  -DMX_COMPAT_32 -O2 -DNDEBUG  "CC_IMEA.c"

-> xcrun  -sdk macosx10.9  clang -c  -I/Applications/MATLAB_R2013b.app/rtw/c/src -I/Users/JoachimSA/Documents/WICHTIG/Diplomarbeit/Matlab_FIles/Peltier_Codegen/Peltier_model_ert_rtw -I/Applications/MATLAB_R2013b.app/extern/include -I/Applications/MATLAB_R2013b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9  -fexceptions  -DMX_COMPAT_32 -O2 -DNDEBUG  "CC_Opt_Objectives.c"

-> xcrun  -sdk macosx10.9  clang -c  -I/Applications/MATLAB_R2013b.app/rtw/c/src -I/Users/JoachimSA/Documents/WICHTIG/Diplomarbeit/Matlab_FIles/Peltier_Codegen/Peltier_model_ert_rtw -I/Applications/MATLAB_R2013b.app/extern/include -I/Applications/MATLAB_R2013b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9  -fexceptions  -DMX_COMPAT_32 -O2 -DNDEBUG  "CC_System_Utils.c"

-> xcrun  -sdk macosx10.9  clang -c  -I/Applications/MATLAB_R2013b.app/rtw/c/src -I/Users/JoachimSA/Documents/WICHTIG/Diplomarbeit/Matlab_FIles/Peltier_Codegen/Peltier_model_ert_rtw -I/Applications/MATLAB_R2013b.app/extern/include -I/Applications/MATLAB_R2013b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9  -fexceptions  -DMX_COMPAT_32 -O2 -DNDEBUG  "CC_Opt_SystemCabin.c"

-> xcrun  -sdk macosx10.9  clang -c  -I/Applications/MATLAB_R2013b.app/rtw/c/src -I/Users/JoachimSA/Documents/WICHTIG/Diplomarbeit/Matlab_FIles/Peltier_Codegen/Peltier_model_ert_rtw -I/Applications/MATLAB_R2013b.app/extern/include -I/Applications/MATLAB_R2013b.app/simulink/include -DMATLAB_MEX_FILE -fno-common -arch x86_64 -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9  -fexceptions  -DMX_COMPAT_32 -O2 -DNDEBUG  "CC_Optimiser.c"

-> xcrun -sdk macosx10.9 clang -O -arch x86_64 -Wl,-syslibroot,/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk -mmacosx-version-min=10.9 -bundle -Wl,-exported_symbols_list,/Applications/MATLAB_R2013b.app/extern/lib/maci64/mexFunction.map -o  "CC_Optimiser.mexmaci64"  CC_IMEA.o CC_Opt_Objectives.o CC_System_Utils.o CC_Opt_SystemCabin.o CC_Optimiser.o  -L/Applications/MATLAB_R2013b.app/bin/maci64 -lmx -lmex -lmat -lstdc++

Undefined symbols for architecture x86_64:
  "_Peltier_model_initialize", referenced from:
      _CC_Opt_SystemCabin in CC_Opt_SystemCabin.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

    mex: link of ' "CC_Optimiser.mexmaci64"' failed.

Unable to complete successfully.

Error in build_mex_files (line 6)
mex -v ...

Error in run (line 63)
evalin('caller', [script ';']);

Do I really have a problem with my 6bit version or am I just not handling the code right ? 我的6位版本真的有问题吗,还是只是不正确处理代码?

Thanks a lot for the help ! 非常感谢您的帮助 !

The OP discovered that all source files need to be specified on the mex command. OP发现需要在mex命令上指定所有源文件。

This can cause compilation to succeed, but linking to fail, if all the necessary declarations are included by the main source file. 如果主源文件包含所有必需的声明,则可能导致编译成功,但链接失败。

Here is some additional information from the Linking Multiple Files section of "Building MEX-Files" on the MathWorks website: 这是 MathWorks网站上“构建MEX文件”链接多个文件”部分的一些其他信息

You can combine multiple source files, object files, and file libraries to build a binary MEX-file. 您可以组合多个源文件,目标文件和文件库来构建二进制MEX文件。 To do this, list the additional files, with their file extensions, separated by spaces. 为此,请列出其他文件及其文件扩展名,以空格分隔。 The name of the MEX-file is the name of the first file in the list. MEX文件的名称是列表中第一个文件的名称。

The following command combines multiple files of different types into a binary MEX-file called circle.ext, where ext is the extension corresponding to the current platform: 以下命令将多个不同类型的文件组合到一个名为circle.ext的二进制MEX文件中,其中ext是与当前平台相对应的扩展名:

mex circle.c square.obj rectangle.c shapes.lib

As it says above, put the file with mexFunction as the first in the list so that the MEX-file gets the name from that file. 如上所述,将带有mexFunction的文件放在列表的第一位,以便MEX文件从该文件中获取名称。

The mex help page has a section "Build MEX-File from Multiple Source Files" with a similar example, although with Fortran source: mex帮助页面的“从多个源文件构建MEX文件”部分带有一个类似的示例,尽管使用了Fortran源:

mex -largeArrayDims fulltosparse.F loadsparse.F mex -largeArrayDims fulltosparse.Floadsparse.F

The MEX-file name is fulltosparse because fulltosparse.F is the first file on the command line. MEX文件名是fulltosparse,因为fulltosparse.F是命令行上的第一个文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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