Eclipse IDEからmatlab libmat.soとlibmx.soに接続できません。

Eclipse IDEからmatlab libmat.soとlibmx.soに接続できません。

コンパイルしようとしています。matcreat.cpp下にEclipse C++ IDE。 MATLABデータをC ++プログラムと交換できます。ある時点で私はプログラムをコンパイルします。

/*
 * matcreat.cpp - MAT-file creation program
 *
 * See the MATLAB External Interfaces/API Guide for compiling information.
 *
 * Calling syntax:
 *
 *   matcreat
 *
 * Create a MAT-file which can be loaded into MATLAB.
 *
 * This program demonstrates the use of the following functions:
 *
 *  matClose
 *  matGetVariable
 *  matOpen
 *  matPutVariable
 *  matPutVariableAsGlobal
 *
 * Copyright 1984-2007 The MathWorks, Inc.
 */
#include <stdio.h>
#include <string.h> /* For strcmp() */
#include <stdlib.h> /* For EXIT_FAILURE, EXIT_SUCCESS */
#include <vector> /* For STL */
#include "mat.h"

#define BUFSIZE 256

int main() {
  MATFile *pmat;
  mxArray *pa1, *pa2, *pa3;
  std::vector<int> myInts;
  myInts.push_back(1);
  myInts.push_back(2);
  printf("Accessing a STL vector: %d\n", myInts[1]);

  double data[9] = { 1.0, 4.0, 7.0, 2.0, 5.0, 8.0, 3.0, 6.0, 9.0 };
  const char *file = "mattest.mat";
  char str[BUFSIZE];
  int status; 

  printf("Creating file %s...\n\n", file);
  pmat = matOpen(file, "w");
  if (pmat == NULL) {
    printf("Error creating file %s\n", file);
    printf("(Do you have write permission in this directory?)\n");
    return(EXIT_FAILURE);
  }

  pa1 = mxCreateDoubleMatrix(3,3,mxREAL);
  if (pa1 == NULL) {
      printf("%s : Out of memory on line %d\n", __FILE__, __LINE__); 
      printf("Unable to create mxArray.\n");
      return(EXIT_FAILURE);
  }

  pa2 = mxCreateDoubleMatrix(3,3,mxREAL);
  if (pa2 == NULL) {
      printf("%s : Out of memory on line %d\n", __FILE__, __LINE__);
      printf("Unable to create mxArray.\n");
      return(EXIT_FAILURE);
  }
  memcpy((void *)(mxGetPr(pa2)), (void *)data, sizeof(data));
  
  pa3 = mxCreateString("MATLAB: the language of technical computing");
  if (pa3 == NULL) {
      printf("%s :  Out of memory on line %d\n", __FILE__, __LINE__);
      printf("Unable to create string mxArray.\n");
      return(EXIT_FAILURE);
  }

  status = matPutVariable(pmat, "LocalDouble", pa1);
  if (status != 0) {
      printf("%s :  Error using matPutVariable on line %d\n", __FILE__, __LINE__);
      return(EXIT_FAILURE);
  }  
  
  status = matPutVariableAsGlobal(pmat, "GlobalDouble", pa2);
  if (status != 0) {
      printf("Error using matPutVariableAsGlobal\n");
      return(EXIT_FAILURE);
  } 
  
  status = matPutVariable(pmat, "LocalString", pa3);
  if (status != 0) {
      printf("%s :  Error using matPutVariable on line %d\n", __FILE__, __LINE__);
      return(EXIT_FAILURE);
  } 
  
  /*
   * Ooops! we need to copy data before writing the array.  (Well,
   * ok, this was really intentional.) This demonstrates that
   * matPutVariable will overwrite an existing array in a MAT-file.
   */
  memcpy((void *)(mxGetPr(pa1)), (void *)data, sizeof(data));
  status = matPutVariable(pmat, "LocalDouble", pa1);
  if (status != 0) {
      printf("%s :  Error using matPutVariable on line %d\n", __FILE__, __LINE__);
      return(EXIT_FAILURE);
  } 
  
  /* clean up */
  mxDestroyArray(pa1);
  mxDestroyArray(pa2);
  mxDestroyArray(pa3);

  if (matClose(pmat) != 0) {
    printf("Error closing file %s\n",file);
    return(EXIT_FAILURE);
  }

  /*
   * Re-open file and verify its contents with matGetVariable
   */
  pmat = matOpen(file, "r");
  if (pmat == NULL) {
    printf("Error reopening file %s\n", file);
    return(EXIT_FAILURE);
  }

  /*
   * Read in each array we just wrote
   */
  pa1 = matGetVariable(pmat, "LocalDouble");
  if (pa1 == NULL) {
    printf("Error reading existing matrix LocalDouble\n");
    return(EXIT_FAILURE);
  }
  if (mxGetNumberOfDimensions(pa1) != 2) {
    printf("Error saving matrix: result does not have two dimensions\n");
    return(EXIT_FAILURE);
  }

  pa2 = matGetVariable(pmat, "GlobalDouble");
  if (pa2 == NULL) {
    printf("Error reading existing matrix GlobalDouble\n");
    return(EXIT_FAILURE);
  }
  if (!(mxIsFromGlobalWS(pa2))) {
    printf("Error saving global matrix: result is not global\n");
    return(EXIT_FAILURE);
  }

  pa3 = matGetVariable(pmat, "LocalString");
  if (pa3 == NULL) {
    printf("Error reading existing matrix LocalString\n");
    return(EXIT_FAILURE);
  }
  
  status = mxGetString(pa3, str, sizeof(str));
  if(status != 0) {
      printf("Not enough space. String is truncated.");
      return(EXIT_FAILURE);
  }
  if (strcmp(str, "MATLAB: the language of technical computing")) {
    printf("Error saving string: result has incorrect contents\n");
    return(EXIT_FAILURE);
  }

  /* clean up before exit */
  mxDestroyArray(pa1);
  mxDestroyArray(pa2);
  mxDestroyArray(pa3);

  if (matClose(pmat) != 0) {
    printf("Error closing file %s\n",file);
    return(EXIT_FAILURE);
  }
  printf("Done\n");
  return(EXIT_SUCCESS);
}

ある瞬間、プログラムはlibmat.soとlibmx.so Matlab APIに依存しますが、Eclipseでどのようにリンクするかを知りたいと思います。リンクなしで私は得る

18:57:21 **** Incremental Build of configuration Debug for project essai ****
make all 
Building target: essai
Invoking: GCC C++ Linker
g++  -o "essai" ./matcreat.o   
/usr/bin/ld : ./matcreat.o : dans la fonction « main » :
/home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:44 : référence indéfinie vers « matOpen_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:51 : référence indéfinie vers « mxCreateDoubleMatrix_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:58 : référence indéfinie vers « mxCreateDoubleMatrix_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:64 : référence indéfinie vers « mxGetPr_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:66 : référence indéfinie vers « mxCreateString_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:73 : référence indéfinie vers « matPutVariable_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:79 : référence indéfinie vers « matPutVariableAsGlobal_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:85 : référence indéfinie vers « matPutVariable_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:96 : référence indéfinie vers « mxGetPr_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:97 : référence indéfinie vers « matPutVariable_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:104 : référence indéfinie vers « mxDestroyArray_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:105 : référence indéfinie vers « mxDestroyArray_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:106 : référence indéfinie vers « mxDestroyArray_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:108 : référence indéfinie vers « matClose_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:116 : référence indéfinie vers « matOpen_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:125 : référence indéfinie vers « matGetVariable_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:130 : référence indéfinie vers « mxGetNumberOfDimensions_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:135 : référence indéfinie vers « matGetVariable_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:140 : référence indéfinie vers « mxIsFromGlobalWS_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:145 : référence indéfinie vers « matGetVariable_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:151 : référence indéfinie vers « mxGetString_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:162 : référence indéfinie vers « mxDestroyArray_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:163 : référence indéfinie vers « mxDestroyArray_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:164 : référence indéfinie vers « mxDestroyArray_800 »
/usr/bin/ld : /home/baptiste/Code_Info/PLPLP4/PLPLP4_DEV/essai/Debug/../matcreat.cpp:166 : référence indéfinie vers « matClose_800 »
collect2: erreur: ld a retourné le statut de sortie 1
make: *** [makefile:60 : essai] Erreur 1
"make all" terminated with exit code 2. Build might be incomplete.

だから私もフォローしようとしています。https://stackoverflow.com/questions/61220237/undefine-references-for-mat-h​​​​-in-c-l/usr/local/MATLAB/R2020b/bin/glnxa64/libmx.so -l/usr/local/MATLAB/R2020b/bin/glnxa64/libmat.so をポップする >> C++ ビルド >> 設定 >>GCC C++に入れます。リンカー>>リンカーフラグ。

わかりました。

19:00:44 **** Incremental Build of configuration Debug for project essai ****
make all 
Building target: essai
Invoking: GCC C++ Linker
g++ -l/usr/local/MATLAB/R2020b/bin/glnxa64/libmx.so -l/usr/local/MATLAB/R2020b/bin/glnxa64/libmat.so -o "essai" ./matcreat.o   
/usr/bin/ld : ne peut trouver -l/usr/local/MATLAB/R2020b/bin/glnxa64/libmx.so
/usr/bin/ld : ne peut trouver -l/usr/local/MATLAB/R2020b/bin/glnxa64/libmat.so
collect2: erreur: ld a retourné le statut de sortie 1
make: *** [makefile:60 : essai] Erreur 1
"make all" terminated with exit code 2. Build might be incomplete

どんなアイデアがありますか?私はC++に初めて触れました。よろしくお願いします。 B

ベストアンサー1

lddコンパイラコマンドではありません。一部の実行可能ファイル(または他の動的ライブラリ)に必要な動的ライブラリのリストを提供するツール。

コンパイラのコマンドラインから削除します。

まず、ソースコードをオブジェクトファイルにコンパイルします(-cコンパイラのコマンドラインに入力します)。結果はmatcreat.o(key)ファイルにあります-o "matcreat.o"

このファイル(および他のファイル)は、実行可能ファイルまたは動的ライブラリを構築するためにリンクされています(コマンドは投稿にないため、これについては何も不明です)。

ldd -d /usr/local/MATLAB/R2020b/bin/glnxa64/libmat.so動的ライブラリで使用されるライブラリのリストを提供しますlibmat.so。そのライブラリがないと、ライブラリ自体は機能しません。

独自の実行ファイル(またはライブラリ)を正常にコンパイルしてlibmat.soにリンクすると、これが表示されますldd myexelibmat.soただし、libmat.soで使用されているライブラリは表示されません)。

おすすめ記事