Problems with linking MATLAB and Visual Studios - c++

In my on my header file (pages.h), I've done:
#include "mat.h".
In my cpp, all I'm trying to do is a simple:
MATFile *pmat.
However, whenever I try to build my code, I get an error that says:
1>pages.obj : error LNK2019: unresolved external symbol _matOpen referenced in function "public: void __thiscall DataPage::LoadDBIData(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?LoadDBIData#DataPage##QAEXV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
and
1>C:\Users\celes\source\repos\bci2000-svn\tools\P300Classifier_electrode_selection\P300Classifier_electrode_selection.exe : fatal error LNK1120: 1 unresolved externals
I've tried going into Properties->C/C++->General->Additional Include Directories and adding a file path of C:\Program Files (x86)\MATLAB\R2015b\extern\include", but I am still receiving the same error. Without this included path, I only received the second error, not the first one.
I am using Visual Studio 2010 (it's for an old research lab) and R2015b for MATLAB.
Any sort of help would be appreciated!

The error message indicates that the compiler has trouble resolving the symbol. Apparently, you didn't set up the project correctly. Besides the "Additional Include Directories" option, you also need to set the library directory in the Linker-> Additional Library Directories for using an external library.
Because you are running a project in VS C++, you will need to have $(MATLABPATH)\extern\lib\<arch>\microsoft in that option, where $(MATLABPATH) is "C:\Program Files (x86)\MATLAB\R2015b" and the <arch> might be Win64 or Win32 depending on your target machine flag in your VS project (i.e. 32- or 64-bit).

Firstly, you could check if you add C:\Program Files (x86)\MATLAB\R2015b\extern\include in Porperties->VC++ Directories-> Include Directories, C:\Program Files (x86)\MATLAB\R2015b\extern\lib\winXX\microsoft in Porperties->VC++ Directories->Library Directories.
Secondly, you could check if you add libmat.lib;ibeng.lib;libmx.lib in Porperties->Linker->Input->Additional Dependencies.
Finally, you could refer to Microsoft Docs about LNK2019 and LNK1120.

Related

Installing Glog linking error

I am new to programming. I want to install Glog on my Windows 10 system. I have followed the instructions found in a Stack Overflow thread with the same name.
I used cmake and Visual Studio 2015 to build the project.
First anomaly of the built file: I noticed that in the glog directory the header file "log_severity.h" was missing. To alleviate this problem, I copied the "log_severity.h" from the GitHub repository and pasted it.
http://imgur.com/QeLTnat
I then created a sample program that just initializes the logger by only using
google::InitGoogleLogging(argv[0]);
in the main.
Before compiling I direct the linker to include an additional include directory. This include directory is the one with the header files as in the second image (with the log_severity.h etc.):
http://imgur.com/yrjOIot
Yet I still receive a linking error when compiling the code:
1>Source.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl google::InitGoogleLogging(char const *)" (__imp_?InitGoogleLogging#google##YAXPEBD#Z) referenced in function main
1>D:\Documents\Programming\C++\Programme\GLogTest\GlogTest\x64\Debug\GlogTest.exe : fatal error LNK1120: 1 unresolved externals
What am I doing wrong?
Thanks a lot of any answers.
I solved it. When you build the project in visual studio, there is also an "Installation" folder inside the MS project. I forgot to build that as well!

LINK2001 When Building Visual Studio Project, BGSLIbrary, where is Linker in v2017

I am trying to build the BGSLibrary for Windows 10, cmake 3.9, with python support, built with opencv 3.1 and boost 1.64.0 with Visual Studio Community 2017.
Can someone more familiar with Visual Studio interpret this error for me. The c++ seems to compile great, but then project solution fails. It starts by building all the .obj files
2>C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX86\x86\Lib.exe /OUT:"C:\Users\Ben\Documents\bgslibrary\build\libbgs.lib" /NOLOGO /machine:X86 libbgs.dir\Release\AdaptiveBackgroundLearning.obj
2>libbgs.dir\Release\AdaptiveSelectiveBackgroundLearning.obj
2>libbgs.dir\Release\CodeBook.obj
2>libbgs.dir\Release\DPAdaptiveMedian.obj
2>libbgs.dir\Release\DPEigenbackground.obj
The general error reads.
5>libbgs.lib(LBSP.obj) : error LNK2019: unresolved external symbol "public: virtual void __thiscall cv::Feature2D::read(class cv::FileNode const &)" (?read#Feature2D#cv##UAEXABVFileNode#2##Z) referenced in function "[thunk]:public: virtual void __thiscall cv::Feature2D::read`vtordisp{4294967292,0}' (class cv::FileNode const &)" (?read#Feature2D#cv##$4PPPPPPPM#A#AEXABVFileNode#2##Z)
But the file LBSP.obj compiled fine just 100 lines earlier? I can see it here.
This file was created during the build, why can't it link? Where do I need to add this dir?
I'm reading other SO questions that this error may mean I need to add additional paths, which was done pre-VS2017 by going to PROPERTIES->LINKER->ADDITIONAL DEPENDENCIES. I don't see this option or structure anymore.
3>C:\Users\Ben\Documents\opencv\modules\core\include\opencv2/core/types_c.h(894)
This was a Cmake path error, not a Visual Studio error. Adding OpenCV/bin to path solved this.

Unresolved external symbols in Visual Studio 2013 when using UHD

I can't seem to get a very simple program to compile in Visual Studio 2013. My goal is to incorporate UHD into a C++ program (UHD Driver and Libraries for Windows). I downloaded and installed the Windows UHD package.Since the library also depends on Boost, I also downloaded the appropriate Boost library. After that, I wrote a quick test program in Visual Studio 2013:
#include "stdafx.h"
#include <iostream>
#include <uhd\usrp\multi_usrp.hpp>
#include <uhd\types\device_addr.hpp>
using namespace uhd;
int main(void)
{
device_addr_t hint;
device_addrs_t dev_addrs = device::find(hint);
system("pause");
return 0;
}
In case anyone is interested, this program should locate any USRPs attached to a host PC. I defined the appropriate include paths in Visual Studio: . .
To be clear, I included two paths to the header files:
C:\Program Files (x86)\UHD\include and C:\Program Files (x86)\boost_1_55_0
and one path to the library:
C:\Program Files (x86)\UHD\lib
When I wrote the above program, Visual Studio recognized the additional include files, recognized device_addr_t as a typedef, and didn't throw any errors. However, when I built the program, I received these errors:
Error 3 error LNK2019: unresolved external symbol "__declspec(dllimport) public: static class std::vector<class uhd::device_addr_t,class std::allocator<class uhd::device_addr_t> > __cdecl uhd::device::find(class uhd::device_addr_t const &)" (__imp_?find#device#uhd##SA?AV?$vector#Vdevice_addr_t#uhd##V?$allocator#Vdevice_addr_t#uhd###std###std##ABVdevice_addr_t#2##Z) referenced in function _main C:\Users\...\Documents\Visual Studio 2013\Projects\uhd_test\uhd_test\uhd_test.obj uhd_test
Error 2 error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall uhd::device_addr_t::~device_addr_t(void)" (__imp_??1device_addr_t#uhd##QAE#XZ) referenced in function "public: void * __thiscall uhd::device_addr_t::`scalar deleting destructor'(unsigned int)" (??_Gdevice_addr_t#uhd##QAEPAXI#Z) C:\Users\...\Documents\Visual Studio 2013\Projects\uhd_test\uhd_test\uhd_test.obj uhd_test
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall uhd::device_addr_t::device_addr_t(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??0device_addr_t#uhd##QAE#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function _main C:\Users\...\Documents\Visual Studio 2013\Projects\uhd_test\uhd_test\uhd_test.obj uhd_test
Error 4 error LNK1120: 3 unresolved externals C:\Users\...\Documents\Visual Studio 2013\Projects\uhd_test\Debug\uhd_test.exe uhd_test
I've encountered these errors before in other programs, but I was able to resolve them relatively easily; either I would misspell a crucial include, or there would be an issue with a template class. However, in this instance, I can't seem to resolve the issue.
Any constructive input would be appreciated.
NOTE: In addition to defining the library directory path, I've explicitly added the UHD library, and I receive the same errors.
I got your example working. Here's the software versions I used since you didn't mention much in your description:
Microsoft Visual Studio 2013
VC++ Redistributable Package for VS2013 (vcredist_x86)
UHD 003.007.002, VS2013, Win32
Boost 1.56, lib32-msvc-12.0 binary package
Here are the project configuration properties you need to set in your VS2013 applications:
C/C++ -> General -> Additional Include Directories
C:\local\boost_1_56_0
C:\Program Files (x86)\uhd\include
Linker -> General -> Additional Library Directories
C:\local\boost_1_56_0\lib32-msvc-12.0
C:\Program Files (x86)\uhd\lib
Linker -> Input -> Additional Dependencies
uhd.lib
I think the last step is what's tripping you up, good luck!
I followed exactly what David Greene said, and I ended up with the same errors. Eventually I figured out that I had downloaded the 64-bit UHD, but I was using Visual Studio 2013 Express, which was a 32-bit compiler. Downloading the 32-bit version of UHD for Visual Studio 2013 solved the problem. For anyone else having this problem, double check that the compiler matches which version of UHD you install.

Linker error while linking some windows APIs

I have a makefile project in my system. Recently, I added some new functions which makes use of the following Windows APIs:
RegOpenKeyEx
RegEnumKeyEx
RegCloseKey
RegGetValue
For having those APIS I added the windows.h header file as well.
The code compiles and links fine in my machine. But, linking fails in my colleagues machine. We all are working on 64 bit windows machine.
In his PC I get the error:
error LNK2001: unresolved external symbol __imp_RegOpenKeyExW
error LNK2001: unresolved external symbol __imp_RegGetValueW
error LNK2001: unresolved external symbol __imp_RegCloseKey
error LNK2001: unresolved external symbol __imp_RegEnumKeyExW
What I tried:
Since the library being used was Advapi32.lib in C:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Lib\x64
I tried adding following line:
LINKFLAGS += -L "C:\Program Files (x86)\Microsoft
SDKs\Windows\v7.0A\Lib\x64"
I added the path of the library to environment variable PATH
I copied the lib to the output folder.
Nothing worked.
As I said earlier, the code works fine in my PC but fails in another one.
The registry functions require you to pass Advapi32.lib to the linker. This is the step that you have missed.
If you are using visual stdio, check your project property vs your friend's project property and check link library path+ library name with .lib shoul be included. It seems in your friends computer is not getting the library to resolve the symbols. Compile time checking is done only with header files. While linking it should find the definition of yor function in library.
for command line:
from:http://social.msdn.microsoft.com/Forums/vstudio/en-US/6bcae3d1-85b6-471d-a4ee-7b455b21460b/how-do-i-link-libraries-that-are-sitting-in-different-directories-from-the-command-line?forum=vcgeneral
cl main.obj ab1.lib ab2.lib de1.lib de2.lib gh1.lib /Fetestmain.exe /link /LIBPATH:C:\test\ab /LIBPATH:C:\test\de /LIBPATH:C:\test\gh
The "/link" is very important...do not use "/LINK", the uppercase LINK is not recognised.

DevIL library files and dependencies

Okay, here's the thing. I have all the IL files I need, namely
DevIL.dll
DevIL.lib
ILU.dll
ILU.lib
ILUT.dll
ILUT.lib
config.h
config.h.in
devil_cpp_wrapper.h
devil_internal_exports.h
il.h
ilu.h
ilu_region.h
ilut.h
ilut_config.h
My project directory looks like this, let's say my project's name is "Project1"
|-Debug---Project1.pdb
|
| |---Debug---[loads of files]
| |
| |---Glut---[OpenGL files]
| |
| |---IL---[all the files mentioned above]
|-Project1---|
| |---image.bmp
Project Folder---| |
| |---[header and .cpp files I made in the project]
| |
| |---[files produced by Visual Studio]
|
|-ipch---[unrelated stuff]
|
|-Project1.sln
|
|-[other files VS created]
I've put all the DevIL files in the IL folder, as mentioned, and I am sure I am using the unicode compatible versions of them, as I am using Unicode Character Set for the project. In my "Additional Dependencies" I have
ilut.lib; ilu.lib; DevIL.lib;
So, the dependencies are there, I know that's not the problem.
After all that, I am still getting linker errors, mainly LNK2019:unresolved external symbol__imp_ for all the IL functions.
What am I missing? It looks to me like maybe something to do with the project properties or the files themselves...maybe I missed a file?
EDIT: Here is the output messages
1>------ Build started: Project: Final Year Project, Configuration: Debug Win32 ------
1>Build started 29/4/2011 12:46:04 pm.
1>InitializeBuildStatus:
1> Touching "Debug\Final Year Project.unsuccessfulbuild".
1>ClCompile:
1> Main.cpp
1>c:\users\xxxx\desktop\final year project 0.2\final year project\main.cpp(152): warning C4390: ';' : empty controlled statement found; is this the intent?
1>c:\users\xxxx\desktop\final year project 0.2\final year project\main.cpp(141): warning C4101: 'alpha' : unreferenced local variable
1>ManifestResourceCompile:
1> All outputs are up-to-date.
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilInit#0 referenced in function "public: static void __cdecl Main::Init(int,char * *)" (?Init#Main##SAXHPAPAD#Z)
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilDeleteImages#8 referenced in function "public: static void __cdecl Main::DisplayScene(void)" (?DisplayScene#Main##SAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilGetData#0 referenced in function "public: static void __cdecl Main::DisplayScene(void)" (?DisplayScene#Main##SAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilConvertImage#8 referenced in function "public: static void __cdecl Main::DisplayScene(void)" (?DisplayScene#Main##SAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilGetInteger#4 referenced in function "public: static void __cdecl Main::DisplayScene(void)" (?DisplayScene#Main##SAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilLoadImage#4 referenced in function "public: static void __cdecl Main::DisplayScene(void)" (?DisplayScene#Main##SAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilBindImage#4 referenced in function "public: static void __cdecl Main::DisplayScene(void)" (?DisplayScene#Main##SAXXZ)
1>Main.obj : error LNK2019: unresolved external symbol __imp__ilGenImages#8 referenced in function "public: static void __cdecl Main::DisplayScene(void)" (?DisplayScene#Main##SAXXZ)
1>C:\Users\xxxx\Desktop\Final Year Project 0.2\Debug\Final Year Project.exe : fatal error LNK1120: 8 unresolved externals
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:01.93
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Since you added the build output, the answer is now easy: your linker errors have nothing to do with DevIL at all.
You need to link to SDL (Simple DirectMedia Layer).
Since SDL has a C interface, and - IIRC - doesn't require that the DLL uses the same heap as the application, the VC8 version of the "Development Libraries" should do fine (even if you use VC10). Just add SDL.lib to the "Additional Dependencies" and you should be fine.
EDIT
OK.
You're either
not linking against the required .lib files (DevIL.lib etc.) or
linking against corrupted/wrong .lib files
At least there is no other explanation I can think of.
The names mentioned in your build log (__imp__ilInit#0 etc.) are correct, and the current "DevIL SDK" (DevIL 1.7.8 SDK for 32-bit Windows) works fine with VC10 (I just verified it).
To assure you're linking against DevIL.lib etc. please put the following in your main.cpp file:
#pragma comment(lib, "DevIL.lib")
#pragma comment(lib, "ILU.lib")
#pragma comment(lib, "ILUT.lib")
To make sure you're linking against the correct version of those files, re-download the whole SDK and try again with the new files.
EDIT 2
Since I got half the reward, I feel I should be more helpful :)
One last thing you can try: enable verbose linker output to check if the linker finds the correct version of DevIL.lib. (If it didn't find any DevIL.lib, you would get an error LNK1104: cannot open file 'DevIL.lib' - and since you're not getting that message, that cannot be the problem.)
To enable verbose linker output, add the /VERBOSE switch (under Configuration Settings -> Linker -> Command Line -> Additional Options).
That will give you a ton of messages. Copy them into your favorite editor, and search for lines containing DevIL.lib. One of the lines should read Searching X:\path\to\DevIL.lib: - that's the path to the copy of DevIL.lib the linker is using. If that's not the path where you copied the files from the SDK you downloaded, you have found the problem.
And if there are no lines containing DevIL.lib, then the linker isn't even trying to locate it. However I've never seen #pragma comment fail, so if you indeed added those lines that almost surely cannot be the case.
BTW: please let me know if you managed to solve this. This is so strange that I really want to know what was going on :)
You need to tell the linker to link against the IL libs.
Project settings, Linker, Input
Make sure the directory the libs are in is in the Additional directorys field too.
I've noticed your config.h.in file should have .win extension because that it is in DevIL-SDK-x86-1.7.8.zip package. And there's no devil_cpp_wrapper.h file also.
Finally i solved this in this way:
Header files
config.h
config.h.win
devil_internal_exports.h
il.h
il_wrap.h
ilu.h
ilu_region.h
ilut.h
ilut_config.h
goes to VC include directory - all in one directory named IL (c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\IL\ by default)
Liblary files
DevIL.lib
ILU.lib
ILUT.lib
goes to lib directory (c:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\ by default)
DLL files
DevIL.dll
ILU.dll
ILUT.dll
goes to project dorectory (...\Project Folder\Project1\ in your case)
Then i added liblary files to project by right click on project->Properties->Linker->Input->Additional Dependencies (wrote just name of liblaries like ILU.lib)
In my project I just include IL\il.h, IL\ilu.h and IL\ilut.h
I think you may got wrong files by the way...
Just in case you never got this worked out, or anyone else has the same problem (like I did):
I had downloaded the 64-bit version of DevIL for my 64-bit Windows computer, but Microsoft Visual Studio (I was using Express) was the 32-bit version.
I downloaded the 32-bit version of DevIL and it worked!
I had the same problem. Seems like the pre-built binaries are swapped: I could link to the ones designated as non-unicode but they would utterly fail trying to use my non-unicode strings and return unicode strings. Try to build your own binaries from the source or use a previous version.
you should compare the function signatures that you see in your errors with the signatures you get with depends.exe (depedency walker). If they differ there may be a compiler-issue.
You may then try to compile with :
extern "C" {
// put your devils-include directives here ...
}
Nothing garantied of course , just a suggestion.
You may also check the 'Additional Library Directories' property on your 'Linker\General' page to see if (when used) it points to the right directory for your build (if you also added a path to the standard VC lib-directories their may be a conflict ,use either one but not both).
Another suggestion is to create a new project and do it all over again ,you may have made an ever so little mistake.
Good luck !
I had the same problem.
In my case it was problem with code in all devil`s .h files
#ifdef _WIN32
#if (defined(IL_USE_PRAGMA_LIBS)) && (!defined(_IL_BUILD_LIBRARY))
#if defined(_MSC_VER) || defined(__BORLANDC__)
#pragma comment(lib, "%LIBNAME%.lib")
#endif
#endif
#endif
after second #if code was deactivated.
i added #pragma comment(lib, "DevIL.lib") (and 2 more) into my main.h and linker errors dissapeared.
sorry for my bad english =[
I don't know if this question is still active, but since it has no chosen answer, I'll post this. I had what seems like the exact same problem with DevIL version 1.7.8. To fix it, I rolled back the version to 1.7.7 and used #undef _UNICODE before including the headers and #define _UNICODE afterwards. The project compiled without linker errors and worked without a hitch. On 1.7.8, the libraries seem to all be unicode, or at least they don't seem to support ascii very well. However, this may just be some weird compatibility issue with my computer as it does not seem to be a common problem
It might be that you have to declare some preprocessor variable to actually tell the compiler that you are importing symbol; I mean something related to __declspec( dllimport )
I did exactly as what Sarah said about the 32-bit or 64-bit Visual Studio versions. I'm on a 64-bit computer running a 32-bit Visual Studio 2012. I obtained this error when I used the x64 DevIL files. Getting the x86 version helped. Here's the basic steps I took to ensure it does run:
Right-click your project and go to Properties.
Under C/C++ > General, add DevIL's 'include' folder under Additional Include Directories.
Then under Linker > General, add DevIL's 'lib' folder (it should contain: DevIL.lib, ILU.lib and ILUT.lib) to Additional Library Directories. For x64 it may not contain a lib folder, it may be just the DevIL directory itself (for version 1.7.8 it was that case).
Also in Linker > Input, add those .lib file names into Additional Dependencies.
Now that should be all you need to do in the project properties. In the main.cpp you should include the appropriate header files and it should work when you initialize.
Example:
#include "IL/il.h"
#include "IL/ilu.h"
#include "IL/ilut.h"
int main(){
ilInit();
iluInit();
ilutRenderer(ILUT_OPENGL); //these are just to initialize
}