unresolved external symbol - Error using Matlab API - matlab

I'm trying to read a .mat-file in C++ with MSVS 2008 but when building a simple program I get the following error:
1>ex3.obj : error LNK2019: unresolved external symbol _matClose referenced in function _main
1>ex3.obj : error LNK2019: unresolved external symbol _matOpen referenced in function _main
I've researched Google as well and it seems that the compiler can't link to the libraries needed for using this functions (matOpen and matClose). I never used an external library before but I tried everything I found in Google to add the Matlab libraries. I did the following:
TOOLS --> Options --> Projects and Solutions --> VC++ Directories --> Show directories for: include files --> then I added the path of the matlab include directory --> C:\Program Files\MATLAB\extern\include
I did the same with the library files: C:\Program Files\MATLAB\extern\lib\win64\microsoft
I also did that for the project:
Right click on the project --> Properties --> Configuration Properties --> C/C++ --> General --> Additional Include Directories --> and added "C:\Program Files\MATLAB\extern\include\win64"
Then I did the same at Linker --> General --> Additional Library Directories --> and added "C:\Program Files\MATLAB\extern\lib\win64\microsoft"
So I really don't know where the problem is. Here is the source code I'm trying to build:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <mat.h>
int main(int argc, char *argv[])
{
const char *file = "mozart_part1.mat";
MATFile *pmat;
pmat = matOpen(file, "r");
if(pmat == NULL)
{
std::cout << "Error: could not open MAT-file!";
return(1);
}
matClose(pmat);
}
Can you see or guess any mistakes I made

Take a look here.
Go through the steps.
What I think you've missed is step 7:
7.Locate the .lib files for the compiler you are using under matlabroot\extern\lib\win32\microsoft or matlabroot\extern\lib\win64\microsoft. Under Linker Input properties, add libmx.lib, libmex.lib, and libmat.lib as additional dependencies.
Edit:
Both Matlab and Visual C++ should be either 32bit or 64bit. There are two options:
Find these 3 lib files from another Matlab which is 32bit. Direct your linker there instead.
Make your Visual C++ 64bit. See here how it's done.
Solution:
What eventually worked was option 2, using this link with instructions.

I solved the Problem!
#Michael Litvin: you were right! I didn't know that you have to switch MSVS 2008 to x64 platform.
As the Matlab libraries are provided as x64 binaries you have to switch your MSVS compiler to x64 as well. I followed these steps to do that: http://software.intel.com/en-us/articles/configuring-microsoft-visual-studio-for-64-bit-applications/
Thanks for you help!

Related

Windows: Eclipse & googletest

I am trying to setup googletest in Eclipse. I really got stuck.
I downloaded the current googletest version 1.13.0. fuse_gtest_files.py is not included in this version (which is often referred to in instructions).
Anyway, I set up an empty c++ project (GCC C++ compiler). Then I added the include paths for the gtest header files (compare picture).
enter image description here
Afterwards, I tried to compile the code. I got the error message "undefined reference to `testing::InitGoogleTest(int*, char**)'" - Reason: It could not find the related .cc files.
#include "gtest/gtest.h"
int main(int argc, char **argv) {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}
So I added the source Location of the cc files (compare Screenshot).
enter image description here
Now, the function is found. But I get a bunch of errors "multiple definition of xx"
C:/xxx/tools/googletest-1.13.0/googletest/src/gtest-assertion-result.cc:45: multiple definition of testing::AssertionResult::AssertionResult(testing::AssertionResult const&)'
src\gtest-all.o:C:/xxx/tools/googletest-1.13.0/googletest/src/gtest-assertion-result.cc:45: first defined here.`
Does anyone has an idea?
Thanks
Jenny

mex file compiled without errors but not working in matlab

First I wanted to compile MatConvNet library for using in windows form this tutorial
(Compiling MatConvNet on Windows)
but I couldn't. Then I think it is better to compile a very simple file and after that going to compile the library.
I have Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
my program Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
I can compile Test.cpp in matlab with mex Test.cpp
And when I type test output is Hello! :)
I also can set the correct configuration according to tutorials below and compile it without errors.
1) http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm
2) http://www.orangeowlsolutions.com/archives/490
But when I run it in Matlab, nothing happen. There is no output and Matlab doesn't give me any error.
What is the problem?
Notice that:
in (1) second step is adding "mexversion.rc" from "matlab\extern\include" to the project But this file dose not exist in my computer so I couldn't do it.
In Visual Studio I needed to add tow headers in below for compiling the program.
include "stdafx.h"
include "maxrix.h"
so the Test.cpp in Visual Studio is:
#include "mex.h"
#include "stdafx.h"
#include "matrix.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
printf("Hello! :)\n");
}
Pre-compiled header shenanigans
A problem with the Visual Studio version of the code is the pre-compiled header file stdafx.h causing the compiler to ignore any code above it (the mex.h include):
#include "mex.h"
#include "stdafx.h" // ANYTHING above here is IGNORED!
#include "matrix.h"
Move the stdafx.h include to the top or turn off PCH in projects settings and remove the include.
printf vs. mexPrintf
Before getting into MEX project setup, note that printf points to mexPrintf courtesy of mex.h:
#define printf mexPrintf
So, using printf is not an issue, but probably not good practice. The problem comes if you redefine printf after including mex.h or fail to get this define on account of the PCH header.
Regarding MEX in Visual Studio
I posted a more formal guide to setting up a Visual Studio projects for building MEX files as an answer to the more commonly-reference question on this topic, and I would also suggest here to use Visual Studio property sheets to get your project set up to build a MEX file. The details are in the referenced post, but you just need to:
Set the MATLAB_ROOT environment variable.
Create a new DLL project.
Under Property Manager (from View menu), right click on each project's build configuration and "Add Existing Property Sheet...", choosing the MATLABx64.props file from this GitHub repo.
printf only works in native C. You need to use mexPrintf. Therefore, your code should be this:
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello! :)\n");
}
In general, printing to standard output in a MEX script doesn't appear in the MATLAB command prompt. If you want to display messages in MATLAB, you need to use mexPrintf instead of printf.
To be explicit, if you consult the mexPrintf documentation, a caveat can be seen towards the end:
In a C MEX-file, you must call mexPrintf instead of printf to display a string.
BTW, I recommend this awesome MEX tutorial here: http://classes.soe.ucsc.edu/ee264/Fall11/cmex.pdf. This is the tutorial that I used to get started with programming MEX wrappers in MATLAB. You'll also see that the first example is of the same "Hello World" caliber that you are trying to get running :)
Good luck!
How To Compile a C/CPP File and Create mex File for Using in Matlab
I found the solution by #rayryeng help and (How to build mex file directly in Visual Studio?) #jorre's post.
This is tested with Matlab R2013a 64 bit and Visual Studio 2010 64 bit.
Test.cpp
#include "mex.h"
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
mexPrintf("Hello World123! :)\n");
}
1.Create a new project in VS -> Visual C++ -> General -> Empty Project.
The name of your Project will be the name of mex file.
You can change it later.
2.Change the Debug to Release.
Right click on the project -> Properties -> Configuration properties -> General
Set Target Extension to .mexw64
Set Configuration Type to Dynamic Library (.dll)
Configureation poperties -> VC++ Directories:
5.Add $(MATLAB_ROOT)\extern\include; to Include Directories
Configuration properties -> Linker -> General:
Add $(MATLAB_ROOT)\extern\lib\win64\microsoft; to Additional Library Directories
Configuration properties -> Linker -> Input:
Add these things to Additional Dependencies
libmx.lib
libmex.lib
libmat.lib
Configuration properties -> Linker -> Command Line:
Add /export:mexFunction to Additional Options
Now you must set you platform to x64 otherwise you'll get error like"Error 1 error LNK2001: unresolved external symbol _mexPrintf".
9.Configuration Properties -> Configuration Manager -> Active Solution Platform -> New -> x64 -> Copy Settings From Win32
Now you can compile your file and get mex file.
If you see these tutorials there are other things thar are not needed and may cause problems.
(http://coachk.cs.ucf.edu/GPGPU/Compiling_a_MEX_file_with_Visual_Studio2.htm)
Create an empty Project, not a MFC Dll Project.
There is no need to *.def file.
There is no need to add "mexversion.rc" into your project.
There is no need to add "MATLAB_MEX_FILE" as a preprocessor definition.

Linking and LOADING static .lib with mex

So, I have a MEX gateway script file that calls my C source code. I've used the -L and -I commands to link my 64-bit compiled GSL libraries (.libs) to my mex executable, which is then compiled under the extension of .mexw64.
I want for this executable to be transferred to another windows machine and run fine, without any GSL libraries installed. That is the the only solution, I don't care what he arguments are regarding the benefits of the dynamic linking/code generation upon compile-time are. I want an executable that has every function not only (of course) pre-declared, but also PRE-DEFINED.
I was lead to believe that this is what 'static' linking is vs. dynamic; but I've read some contradictory definitions all around the interwebs. I need a completely 100% standalone, singular file.
Supposedly you can link the actual .obj file in the mex function, which I can generate, but unfortunately I then get unresolved symbol errors.
Someone else mentioned that I can use the -l (lowercase L) to directly link the actual .lib(s) needed, statically, but that is NOT true.
So is there anyone that can lead me in the right direction, either how to have everything not only linked but to also have the DEFINITIONS linked and ready to load when executable is run--completely standalone, or why I am running into unresolved symbols/linker errors when I include my .obj file? Am I misunderstanding something elementary about the linking process?
Also: To elaborate a bit more, I have the GSL libraries built and linked via Visual Studio for the 64 bit architecture, and I can link it easily with MATLAB, so that is not my problem (any more).
EDIT: I've seen the post here:
Generating standalone MEX file with GNU compilers, including libraries
This doesn't solve my problem, however, although it is the same question. I don't have access to gcc; it's finally compiling on the MSVS12 compiler in MATLAB, I'm not going try to recompile using GCC via MinGW (already tried, couldn't figure it out), so -static and .a options are out.
In your previous post, you mentioned that you decided to compile GSL library with Visual C++, using the VS solution provided by Brian Gladman.
Here is a step-by-step illustration on how to build a MEX-function that links against GSL libraries statically:
Download GNU GSL sources (GSL v1.16)
Download the matching Visual Studio project files (VS2012 for GSL v1.16)
Extract the GSL tarball, say to C:\gsl-1.16
Extract the VS project files on top of the sources, this will overwrite three files as well as add a folder C:\gsl-1.16\build.vc11.
Open Visual Studio 2012, and load the solution: C:\gsl-1.16\build.vc11\gsl.lib.sln
Change the configuration to the desired output: for me I chose platform=x64 and mode=Release
First you must build the gslhdrs project first
Now build the whole solution. This will create two static libraries cblas.lib and gsl.lib stored in C:\gsl-1.16\lib\x64\Release (along with corresponding PDB debugging symbols). It will also create a directory containing the final header files: C:\gsl-1.16\gsl
Next we proceed to build a MEX-function. Take the following simple program (computes some value from a Bessel function, and return it as output):
gsl_test.c
#include "mex.h"
#include <gsl/gsl_sf_bessel.h>
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
if (nrhs != 0 || nlhs > 1) mexErrMsgTxt("Wrong number of args.");
plhs[0] = mxCreateDoubleScalar(gsl_sf_bessel_J0(5.0));
}
This is how to compile the above C code in MATLAB:
>> mex -largeArrayDims gsl_test.c -I"C:\gsl-1.16" -L"C:\gsl-1.16\lib\x64\Release" cblas.lib gsl.lib
Finally we test the MEX-file, and compare it against the value reported by MATLAB's own Bessel function:
>> x = gsl_test()
ans =
-0.1776
>> y = besselj(0,5)
y =
-0.1776
>> max(x-y) % this should be less than eps
ans =
8.3267e-17
Note that the built MEX-function has no external DLL dependencies (other than "Visual C Runtime" which is expected, and the usual MATLAB libraries). You can verify that by using Dependency Walker if you want. So you can simply deploy the gsl_test.mexw64 file alone (assuming the users already have the corresponding VC++ runtime installed on their machines).

Android JNI c++ files compiling

I'm using JNI in Android development. I want to know where do the compiler find the referenced .h files?
Here in jni/jni_part.cpp, it includs lots of .h files. Opencv related files are defined by the SDK, but the "cartoon.h" is sure to be a project-specified header. But I cannot find it in the project folder. Then do the eclipse compile the JNI c++ code at every run? If so , in which directories does the eclipse find these headers?
Maybe it's basic for JNI but I'm not familiar with it. So plz help me out with one or two sentences. Or you could just paste a simple tutorial for JNI in Android, eclipse.
#include <jni.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>
#include "cartoon.h"
#include "ImageUtils.h" // Handy functions for debugging OpenCV images, by Shervin Emami.
using namespace std;
using namespace cv;
extern "C" {
.....
This is my eclipse project folder structure
/project
/jni
jni_part.cpp
/src
something.java
You'll need to start using android-ndk to compile the .c, .cpp files to a .so library object, which you can then use in your android project.
Please refer to the Android NDK to get started.
http://developer.android.com/tools/sdk/ndk/index.html#GetStarted
you can find examples in the NDK download.
If Eclipse "knows" where cartoon.h file is on disk, it will help you find it - put the cursor on that #include line and press F3.
Regarding your other question: Eclipse compiles only if the relevant files (sources, headers, or mk) change.

Adding QtCore Library in blackberry 10 sdk

Hi guys I am creating a simple game using cocos2d-x and blackberry. I need some place to store my game settings, something similar to shared preferences in ios and android. I found some code using qsettings, but the problem is I am not able to add the QtCore library.
I add the library using RightClick->configure->add Library and Standard BlackBerry Platform Library. The library gets added successfully.
#include "dataProcessor.h"
#include <QtCore>
void dataProcessor::setup(){
QDir dir;
dir.mkpath("data/files/text");
dir.cd("data/files/text");
}
but when I compile the above code, I get the error C:/Users/I076636/Documents/target_10_0_9_1673/qnx6/usr/include/qt4/QtCore/qatomic.h:45:28: fatal error: QtCore/qglobal.h: No such file or directory
But I noticed 2 things,
1.qglobal.h file is there inside the QtCore directory I have included.
2.inside qatomic.h if I change
#ifndef QATOMIC_H
#define QATOMIC_H
#include <QtCore/qglobal.h>
#include <QtCore/qbasicatomic.h>
into
#ifndef QATOMIC_H
#define QATOMIC_H
#include <qglobal.h>
#include <QtCore/qbasicatomic.h>
the error for qglobal goes and now the same error comes for qbasicatomic.h.
I think it is something simple like incorrect mapping between QtCore keyword and include directory or something..
Please do have a look.
The IDE is made on eclipse.
You can understand what is going wrong if you look closely at the error message:
/target_10_0_9_1673/qnx6/usr/include/qt4/QtCore/qatomic.h:45:28:
fatal error: QtCore/qglobal.h: No such file or directory
The error isn't in your inclusion of QtCore, but is occurring inside QtCore/qatomic.h, on line 45 (you can find this file in the [YOUR BBNDK DIRECTORY]/target_10_0_9_1673/qnx6/usr/include/qt4/QtCore/qatomic.h):
#include <QtCore/qglobal.h>
qatomic.h is already in the QtCore directory, and you'll find a qglobal.h directory there as well. So what this means is that qatomic.h expects the parent directory to be on the include path, so that including <QtCore/qglobal.h> will work.
So you just need to add [YOUR BBNDK DIRECTORY]/target_10_0_9_1673/qnx6/usr/include/qt4 to your include directories.
Do it like this:
Right click over your project in Project Explorer and choose Properties
Expand the tree to C/C++ General / Paths and Symbols
Change the Configuration in the Paths and Symbols frame to [All configurations]
Click the Includes tag and select GNU C in the Languages list (or do this for every language).
Click Add... and type ${QNX_TARGET}/usr/include/qt4 and press OK
Click Add... and type ${QNX_TARGET}/usr/include/qt4/QtCore and press OK
Now your include of #include <QtCore> should work.
Next up: linking errors ;-)
It sounds like your BB10 NDK did not get installed properly, or your project wasn't set up properly. If you expand your project and the Includes you should see (along with others):
<NDK_INSTALL_LOCATION>/target_<VERSION>/qnx6/usr/include/qt4/QtCore