I called matlab function in C++ and got LNK2001 error.

Hello
C:\Program Files\MATLAB\MATLAB Runtime\v97\extern\lib\win64\mingw64
C:\Program Files\MATLAB\MATLAB Runtime\v97\extern\lib\win64\microsoft
C:\Program Files\MATLAB\MATLAB Runtime\v97\extern\include
C:\Program Files\MATLAB\MATLAB Runtime\v97\bin\win64
C:\Program Files\MATLAB\MATLAB Runtime\v97\lib\win64
C:\Program Files\MATLAB\MATLAB Runtime\v97\runtime\win64
I have already added the above paths to the project.
mclbase.lib, mclcommain.lib, mclmcr.lib, mclmcrrt.lib and mclxlmain.lib were also added.
This causes a LNK2001 unresolved external symbol error in the GT_model function.
The other GT_modelInitialize and mclInitializeApplication functions seem to work well
it seems that lib or dll is added well
GT_model(1, y_pwrOut, inputIn) is not work
i got LNK2001 unrresolved error
I don't know what's wrong.
Thank you for any advice.
Main.cpp
------------------------------------------------------------------------------------
#include <iostream>
#include "GT_model.h"
void GT_modelSample()
{
mxDouble inputInData[] = { -20.0, 101.325, 0.6, 321.3713, -20.0, 100.8011, 281.3149, 873.2329, 2.5707, 200.0, 49492.4247435387, 357.7379, 121.1334, 357.7379, 102.0625, 0.0, 3600.0 };
mwArray inputIn(1, 17, mxDOUBLE_CLASS);
inputIn.SetData(inputInData, 17);
std::cout << "4\n";
mwArray y_pwrOut;
mxArray *plhs;
mxDouble *prhs;
try
{
GT_model(1, y_pwrOut, inputIn);
std::cout << y_pwrOut << std::endl;
std::cout << "5\n";
}
catch (const mwException & e)
{
std::cerr << e.what() << std::endl;
}
catch (...)
{
std::cerr << "Unexpected error thrown" << std::endl;
}
}
int run_main(int argc, const char** argv)
{
std::cout << "2\n";
if (!GT_modelInitialize())
{
std::cerr << "Could not initialize the library properly" << std::endl;
return -2;
}
else
{
std::cout << "3\n";
GT_modelSample();
std::cout << "6\n";
// Call the application and library termination routine
GT_modelTerminate();
std::cout << "7\n";
}
// Note that you should call mclTerminateApplication at the end of
// your application to shut down all MATLAB Runtime instances.
mclTerminateApplication();
std::cout << "8\n";
return 0;
}
// The main routine. On macOS, the main thread runs the system code, and
// user code must be processed by a secondary thread. On other platforms,
// the main thread runs both the system code and the user code.
int main(int argc, const char** argv)
{
/* Call the mclInitializeApplication routine. Make sure that the application
* was initialized properly by checking the return status. This initialization
* has to be done before calling any MATLAB APIs or MATLAB Compiler SDK
* generated shared library functions.
*/
if (!mclmcrInitialize()) {
std::cerr << "Could not initialize the application." << std::endl;
return -1;
}
if (!mclInitializeApplication(nullptr, 0))
{
std::cerr << "Could not initialize the application." << std::endl;
return -1;
}
std::cout << "1\n";
return mclRunMain(static_cast<mclMainFcnType>(run_main), argc, argv);
}
---------------------------------------------------------------------------------
GT_model.h
---------------------------------------------------------------------------------
//
// MATLAB Compiler: 7.1 (R2019b)
// Date: Wed Feb 5 15:51:57 2020
// Arguments:
// "-B""macro_default""-W""cpplib:GT_model,all,version=1.0""-T""link:lib""-d""C:
// \Users\sergey.mishchenko\Desktop\dll
// file\GT_model\for_testing""-v""C:\Users\sergey.mishchenko\Desktop\dll
// file\GT_model.m"
//
#ifndef GT_model_h
#define GT_model_h 1
#if defined(__cplusplus) && !defined(mclmcrrt_h) && defined(__linux__)
# pragma implementation "mclmcrrt.h"
#endif
#include "mclmcrrt.h"
#include "mclcppclass.h"
#ifdef __cplusplus
extern "C" { // sbcheck:ok:extern_c
#endif
/* This symbol is defined in shared libraries. Define it here
* (to nothing) in case this isn't a shared library.
*/
#ifndef LIB_GT_model_C_API
#define LIB_GT_model_C_API /* No special import/export declaration */
#endif
/* GENERAL LIBRARY FUNCTIONS -- START */
extern LIB_GT_model_C_API
bool MW_CALL_CONV GT_modelInitializeWithHandlers(
mclOutputHandlerFcn error_handler,
mclOutputHandlerFcn print_handler);
extern LIB_GT_model_C_API
bool MW_CALL_CONV GT_modelInitialize(void);
extern LIB_GT_model_C_API
void MW_CALL_CONV GT_modelTerminate(void);
extern LIB_GT_model_C_API
void MW_CALL_CONV GT_modelPrintStackTrace(void);
/* GENERAL LIBRARY FUNCTIONS -- END */
/* C INTERFACE -- MLX WRAPPERS FOR USER-DEFINED MATLAB FUNCTIONS -- START */
extern LIB_GT_model_C_API
bool MW_CALL_CONV mlxGT_model(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);
/* C INTERFACE -- MLX WRAPPERS FOR USER-DEFINED MATLAB FUNCTIONS -- END */
#ifdef __cplusplus
}
#endif
/* C++ INTERFACE -- WRAPPERS FOR USER-DEFINED MATLAB FUNCTIONS -- START */
#ifdef __cplusplus
/* On Windows, use __declspec to control the exported API */
#if defined(_MSC_VER) || defined(__MINGW64__)
#ifdef EXPORTING_GT_model
#define PUBLIC_GT_model_CPP_API __declspec(dllexport)
#else
#define PUBLIC_GT_model_CPP_API __declspec(dllimport)
#endif
#define LIB_GT_model_CPP_API PUBLIC_GT_model_CPP_API
#else
#if !defined(LIB_GT_model_CPP_API)
#if defined(LIB_GT_model_C_API)
#define LIB_GT_model_CPP_API LIB_GT_model_C_API
#else
#define LIB_GT_model_CPP_API /* empty! */
#endif
#endif
#endif
extern LIB_GT_model_CPP_API void MW_CALL_CONV GT_model(int nargout, mwArray& y_pwr, const mwArray& input);
/* C++ INTERFACE -- WRAPPERS FOR USER-DEFINED MATLAB FUNCTIONS -- END */
#endif
#endif
---------------------------------------------------------------------------------

7 Comments

Which symbol is noted as unresolved?
GT_model(1, y_pwrOut, inputIn);
I see interfaces defined to call GT_model from MATLAB but I do not see you listing any dll or lib that defines GT_model?
GT_model.lib added linker - input
and GT_model.dll in folder
Would it be possible to post the entire build log?
Severity Code Description Project File Line Suppression Suppression Status
Error (active) E0020 The identifier "mclTerminateApplication" is not defined. SDAS DLL TEST C: \ SDAS DLL TEST \ SDAS DLL TEST \ SDAS DLL TEST.cpp 149
Error LNK2001 "void __cdecl GT_model (int, class mwArray &, class mwArray const &)" (? GT_model @@ YAXHAEAVmwArray @@ AEBV1 @@ Z) The external symbol cannot be resolved. SDAS DLL TEST C: \ SDAS DLL TEST \ SDAS DLL TEST \ SDAS DLL TEST.obj 1
Error LNK1120 One unresolved external reference. SDAS DLL TEST C: \ SDAS DLL TEST \ SDAS DLL TEST \ x64 \ Release \ SDAS DLL TEST.exe 1
mclTerminateApplication is an intellisence error but not a build error
But I'm not sure what causes it

Sign in to comment.

Answers (0)

Asked:

on 20 Feb 2020

Edited:

on 24 Feb 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!