⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mex.pl

📁 此程序是本人编写的在MFC环境下调用Matlab函数进行编程的实例
💻 PL
📖 第 1 页 / 共 5 页
字号:
#######################################################################
# Begin perl code
#######################################################################
#
# MEX builds MEX-files.
#
# Copyright 1984-2001 The MathWorks, Inc.
# $Revision: 1.145 $  $Date: 2001/04/04 13:42:24 $
#
#   Functional overview:
#   1. Define subroutines
#   2. Initialize variables
#   3. Parse input arguments & determine MEX-file name
#   4. Source options file
#   5. Determine final values
#   6. Compile source files
#   7. Perform prelink tasks
#   8. Link files
#
# Globals used: $main::mbuild => {"yes", "no"} is the mbuild or mex?
#               $main::script_directory => string with $MATLAB\bin in it
#               $main::cmd_name => {"MBUILD.BAT", "MEX.BAT"}
#               $main::no_execute => {0, 1} RunCmd execute command? (used by -n switch)
#######################################################################
# Set the Perl path and $cmd_name variable
#######################################################################

#use strict; # big time nostrict

#######################################################################
# Set @INC for 5.00503 perl.
# If perl gets upgraded, this may have to be changed.
#######################################################################
BEGIN {
    if ( $] < 5.00503 ) {
	die "ERROR: This script requires perl 5.00503 or higher.
You have perl $] installed on your machine and you did not set MATLAB variable,
so the correct version of perl could not be found";
    }
    if ( ! $ENV{"MATLAB"} ) {
	my($f) = $0;
	$f =~ s%[\\/][\w\.]+[\\/]\.\.[\\/]%\\%g;
	$f =~ s%^[^\.][^\\/]+[\\/]\.\.[\\/]%%g;
	# print "$0 -> $f ";
	$0 = $f;
	if ($f =~ s%^bin[\\/]win32[\\/].*%% ) {
	    $f = '.';
	} elsif ($f =~ s%[\\/]bin[\\/]win32[\\/].*%% ) {
	    # fine
	} else {
	    $f .= "\\..\\..\\..";
	    $f =~ s%\\[^\.][^\\]+\\\.\.\\%\\%g;
	    $f =~ s%^[^\.][^\\]+\\\.\.\\%%g;
	}
	# print "-> $f\n";
	$ENV{'MATLAB'} = $f;
    }
    if ( $ENV{'MATLAB'} ) {
	unshift (@INC, map { "$ENV{MATLAB}/sys/perl/win32/$_" }
		 ("lib", "site/lib"));
        push(@INC, "$ENV{MATLAB}/bin/win32");
    } else {
	warn "WARNING: MATLAB variable is not set.";
    }
}

use Cwd;
use mexsetup;
use getprofiledir;
use File::DosGlob 'glob';
require "shellwords.pl";

if ( $ENV{'MATLAB'} =~ /^\.\\?$/ ) {
    ($ENV{'MATLAB'} = getcwd()) =~ s%/%\\%g;
}

#######################################################################
# Define subroutines
#######################################################################

# smart_quote adds quotes (") at the beginning and end of  its input
# if the input contains a space. The quoted string is returned as the
# output. If the input contains no spaces, the input is returned as the
# output.
sub smart_quote
{
    my ($str) = @_;	# input

    $str = "\"$str\"" if ($str =~ / /);
    $str;		# output
} # smart_quote

sub describe {
# Describe:  Issues messages.  This way lengthy messages do not clutter
#            up the main body of code.
    local($_) = $_[0];
 DESCRIPTION: {
     /^help$/ && print(<<'end_help') && last DESCRIPTION;
MEX [option1 ... optionN] sourcefile1 [... sourcefileN]
    [objectfile1 ... objectfileN] [libraryfile1 ... libraryfileN]

Description:
  MEX compiles and links source files into a shared library called a
  MEX-file, executable from within MATLAB. The resulting file has a
  platform-dependent extension, as shown in the table below:

    sol2, SunOS 5.x - .mexsol
    hpux            - .mexhpux
    hp700           - .mexhp7
    ibm_rs          - .mexrs6
    sgi             - .mexsg
    alpha           - .mexaxp
    glnx86          - .mexglx
    Windows         - .dll

  The first file name given (less any file name extension) will be the name
  of the resulting MEX-file. Additional source, object, or library files can
  be given to satisfy external references. On Windows, either C or Fortran,
  but not both, may be specified. On UNIX, both C and Fortran source files
  can be specified when building a MEX-file. If C and Fortran are mixed, the
  first source file given determines the entry point exported from the
  MEX-file (MATLAB loads and runs a different entry point symbol for C or
  Fortran MEX-files).

  Both an options file and command line options affect the behavior of MEX.
  The options file contains a list of variables that are passed as arguments
  to various tools such as the compiler, linker, and other platform-
  dependent tools (such as the resource linker on Windows). Command line
  options to MEX may also affect what arguments are passed to these tools,
  or may control other aspects of MEX's behavior.

Command Line Options:
  Options available on all platforms:

  -ada <sfcn.ads>
      Use this option to compile a Simulink S-function written in Ada, where
      <sfcn.ads> is the Package Specification for the S-function. When this
      option is specified, only the -v (verbose) and -g (debug) options are
      relevant. All other options are ignored. See
      $MATLAB/simulink/ada/examples/README for examples and info on
      supported compilers and other requirements.
  -argcheck
      Add argument checking. This adds code so that arguments passed
      incorrectly to MATLAB API functions will cause assertion failures.
      Adds -DARGCHECK to the C compiler flags, and adds
      $MATLAB/extern/src/mwdebug.c to the list of source files. (C functions
      only).
  -c
      Compile only. Creates an object file but not a MEX-file.
  -D<name>
      Define a symbol name to the C preprocessor. Equivalent to a
      "#define <name>" directive in the source.
  -D<name>#<value>
      Define a symbol name and value to the C preprocessor. Equivalent to a
      "#define <name> <value>" directive in the source.
  -f <optionsfile>
      Specify location and name of options file to use. Overrides MEX's
      default options file search mechanism.
  -g
      Create a debuggable MEX-file. If this option is specified, MEX appends
      the value of options file variables ending in DEBUGFLAGS with their
      corresponding base variable. (For example, the value of LINKDEBUGFLAGS
      would be appended to the LINKFLAGS variable before calling the
      linker.) This option also disables MEX's default behavior of
      optimizing built object code.
  -h[elp]
      Print this message.
  -I<pathname>
      Add <pathname> to the list of directories to search for #include
      files.
  -inline
      Inline matrix accessor functions (mx*). The MEX-function generated
      may not be compatible with future versions of MATLAB.
  -n
      No execute mode. Print out any commands that MEX would otherwise have
      executed, but do not actually execute any of them.
  -O
      Optimize the object code by including the optimization flags listed in
      the options file. If this option is specified, MEX appends the value
      of options file variables ending in OPTIMFLAGS with their
      corresponding base variable. (For example, the value of LINKOPTIMFLAGS
      would be appended to the LINKFLAGS variable before calling the
      linker.) Note that optimizations are enabled by default, are disabled
      by the -g option, but are reenabled by -O.
  -outdir <dirname>
      Place all output files in directory <dirname>.
  -output <resultname>
      Create MEX-file named <resultname> (an appropriate MEX-file extension
      is automatically appended). Overrides MEX's default MEX-file naming
      mechanism.
  -setup
      Interactively specify the compiler options file to use as default for
      future invocations of MEX by placing it in "<UserProfile>\Application
      Data\MathWorks\MATLAB\R12" (for Windows) or $HOME/.matlab/R12 (for
      UNIX). When this option is specified, no other command line input is
      accepted.
  -U<name>
      Remove any initial definition of the C preprocessor symbol <name>.
      (Inverse of the -D option.)
  -v
      Print the values for important internal variables after the options
      file is processed and all command line arguments are considered.
      Prints each compile step and final link step fully evaluated to see
      which options and files were used. Very useful for debugging.
  -V4
      Compile a MATLAB 4-style MEX-file. This option is intended as an aid
      to migration, and is not recommended as a permanent solution.
  <name>#<value>
      Override an options file variable for variable <name>. See the
      platform-dependent discussion of options files below for more details.
      This option is processed after the options file is processed and all
      command line arguments are considered.

Additional options available on Windows platforms:

  @<rspfile>
      Include contents of the text file <rspfile> as command line arguments
      to MEX.

Additional options available on Unix platforms:

  -<arch>
      Assume local host has architecture <arch>. Possible values for <arch>
      include sol2, hpux, hp700, alpha, ibm_rs, sgi, and glnx86.
  -D<name>=<value>
      Define a symbol name and value to the C preprocessor. Equivalent to a
      "#define <name> <value>" directive in the source.
  -fortran
      Specify that the gateway routine is in Fortran. This will override
      what the script normally assumes, which is that the first source file
      in the list is the gateway routine.
  -l<name>
      Link with object library "lib<name>" (for "ld(1)").
  -L<directory>
      Add <directory> to the list of directories containing object-library
      routines (for linking using "ld(1)").
  <name>=<value>
      Override an options file variable for variable <name>. See the
      platform-dependent discussion of options files below for more details.

Options File Details:
  On Windows:
    The options file is written as a DOS batch file. If the -f option is not
    used to specify the options file name and location, then MEX searches
    for an options file named mexopts.bat in the following directories: the
    current directory, then the directory "<UserProfile>\Application
    Data\MathWorks\MATLAB\R12". Any variable specified in the options file
    can be overridden at the command line by use of the <name>#<value>
    command line argument. If <value> has spaces in it, then it should be
    wrapped in double quotes (e.g., COMPFLAGS#"opt1 opt2"). The definition
    can rely on other variables defined in the options file; in this case
    the variable referenced should have a prepended "$" (e.g.,
    COMPFLAGS#"$COMPFLAGS opt2").

    Note: The options files in $MATLAB\bin\mexopts named *engmatopts.bat are
    special case options files that can be used with MEX (via the -f option)
    to generate stand-alone MATLAB Engine and MATLAB MAT-API executables.
    Such executables are given a ".exe" extension.

  On UNIX:
    The options file is written as a UNIX shell script. If the -f option is
    not used to specify the options file name and location, then MEX
    searches for an options file named mexopts.sh in the following
    directories: the current directory (.), then $HOME/.matlab/R12, then
    $MATLAB/bin. Any variable specified in the options file can be
    overridden at the command line by use of the <name>=<def> command line
    argument. If <def> has spaces in it, then it should be wrapped in single
    quotes (e.g., CFLAGS='opt1 opt2'). The definition can rely on other
    variables defined in the options file; in this case the variable
    referenced should have a prepended "$" (e.g., CFLAGS='$CFLAGS opt2').

    Note: The options files in $MATLAB/bin named engopts.sh and matopts.sh
    are special case options files that can be used with MEX (via the -f
    option) to generate stand-alone MATLAB Engine and MATLAB MAT-API
    executables. Such executables are not given any default extension.

Examples:
    The following command will compile "myprog.c" into "myprog.mexsol" (when
    run under Solaris):

      mex myprog.c

    When debugging, it is often useful to use "verbose" mode as well
    as include symbolic debugging information:

      mex -v -g myprog.c

end_help
     /^usage$/ && print(<<'end_usage') && last DESCRIPTION;
    Usage:
        MEX [option1 ... optionN] sourcefile1 [... sourcefileN]
            [objectfile1 ... objectfileN] [libraryfile1 ... libraryfileN]

      or (to build an Ada S-function):
        MEX [-v] [-g] -ada <sfcn>.ads

    Use the -help option for more information, or consult the MATLAB API Guide.

end_usage
     /^general_info$/ && print(<<"end_general_info") && last DESCRIPTION;
 This is mex, Copyright 1984-2001 The MathWorks, Inc.

end_general_info
     /^invalid_options_file$/ && print(<<"end_invalid_options_file") && last DESCRIPTION;
    Warning: An options file for MEX.BAT was found and sourced, but the
             value for 'COMPILER' was not set.  This could mean that the
             value is not specified within the options file, or it could
             mean that there is a syntax error within the file.

end_invalid_options_file
     /^final_options$/ && print(<<"end_final_options") && last DESCRIPTION;
$sourced_msg
----------------------------------------------------------------
->    Options file           = $OPTFILE_NAME
      MATLAB                 = $MATLAB
->    COMPILER               = $COMPILER
->    Compiler flags:
         COMPFLAGS           = $COMPFLAGS
         OPTIMFLAGS          = $OPTIMFLAGS
         DEBUGFLAGS          = $DEBUGFLAGS
         arguments           = $ARG_FLAGS
         Name switch         = $NAME_OBJECT
->    Pre-linking commands   = $PRELINK_CMDS
->    LINKER                 = $LINKER
->    Link directives:
         LINKFLAGS           = $LINKFLAGS
         LINKFLAGSPOST       = $LINKFLAGSPOST
         Name directive      = $NAME_OUTPUT
         File link directive = $LINK_FILE
         Lib. link directive = $LINK_LIB
         Rsp file indicator  = $RSP_FILE_INDICATOR
->    Resource Compiler      = $RC_COMPILER
->    Resource Linker        = $RC_LINKER
----------------------------------------------------------------

end_final_options
     /^file_not_found$/ && print(<<"end_file_not_found") && last DESCRIPTION;
  $main::cmd_name:  $filename not a normal file or does not exist.

end_file_not_found
     /^meaningless_output_flag$/ && print(<<"end_meaningless_output_flag")  && last DESCRIPTION;
  Warning: -output ignored (no MEX-file is being created).

end_meaningless_output_flag

    /^compiler_not_found$/ && print(<<"end_compiler_not_found") && last DESCRIPTION;
  Could not find the compiler "$COMPILER" on the DOS path.
  Use mex -setup to configure your environment properly.

end_compiler_not_found

    /^outdir_missing_name_object$/ && print(<<"end_outdir_missing_name_object") && last DESCRIPTION;
  Warning: The -outdir switch requires the mex options file to define
           NAME_OBJECT. Make sure you are using the latest version of
           your compiler's mexopts file.

end_outdir_missing_name_object

    /^fortran_cannot_change_entrypt$/ && print(<<"end_fortran_cannot_change_entrypt") && last DESCRIPTION;
  Warning: -entrypt ignored (FORTRAN entry point cannot be overridden).

end_fortran_cannot_change_entrypt

    do {
        print "Internal error: Description for $_[0] not implemented\n";
        last DESCRIPTION;
    };
 }
}

sub describe_mb {
# Describe:  Issues messages.  This way lengthy messages do not clutter

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -