Linker error when installing MessagePack implementation for C and C++ - c++

I'm following the instructions, which tell me to download msgpack 0.5.4 for C & C++.
On Windows, download source package from here and extract it. Open msgpack_vc8.vcproj or msgpack_vc2008 file and build it using batch build. It builds libraries in lib/ folder and header files in include/ folder.
You can build using command line as follows:
vcbuild msgpack_vc2008.vcproj
dir lib % DLL files are here
dir include % header files are here
vcbuild msgpack_vc2008.vcproj has been replaced by MSBuild msgpack_vc8.vcxproj. I used Visual studio 2012 to convert the project to have the correct .vcxproj for this. Batch build in Visual studio and running MSBuild gives the same result, so I will speak for both of them from this point on.
After the project is converted, I noticed the project was set to output to .lib, rather than .dll, so I altered that setting to match my needs. When compiling there was one small error:
...\microsoft visual studio 11.0\vc\include\stdint.h(8): error C2371: 'int8_t' : redefinition; different basic types
...msgpack-0.5.4\src\msgpack\sysdep.h(23) : see declaration of 'int8_t'
So I changed the line
typedef __int8 int8_t;
to
typedef signed __int8 int8_t;
which solves that minor issue. But then we arrive at where I'm at now. This linker error:
objectc.obj : error LNK2019: unresolved external symbol __imp__ntohl#4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohl#4
objectc.obj : error LNK2019: unresolved external symbol __imp__ntohs#4 referenced in function _msgpack_pack_array
unpack.obj : error LNK2001: unresolved external symbol __imp__ntohs#4
...\msgpack-0.5.4\Debug\MessagePack.dll : fatal error LNK1120: 2 unresolved externals
I've searched for parts of this error:
In sysdep.h:
#define _msgpack_be16(x) ntohs(x)
#define _msgpack_be32(x) ntohl(x)
In object.c:
case MSGPACK_OBJECT_ARRAY:
{
int ret = msgpack_pack_array(pk, d.via.array.size);
if(ret < 0) { return ret; }
msgpack_object* o = d.via.array.ptr;
msgpack_object* const oend = d.via.array.ptr + d.via.array.size;
for(; o != oend; ++o) {
ret = msgpack_pack_object(pk, *o);
if(ret < 0) { return ret; }
}
In unpack.c:
static inline int template_callback_array(unpack_user* u, unsigned int n, msgpack_object* o)
{
o->type = MSGPACK_OBJECT_ARRAY;
o->via.array.size = 0;
o->via.array.ptr = (msgpack_object*)msgpack_zone_malloc(u->z, n*sizeof(msgpack_object));
if(o->via.array.ptr == NULL) { return -1; }
return 0;
}
And that's about all I know. If there's another way of how to obtain the .dll, that would be helpful too. Thank you in advance. :)

You need to link the ws2_32.lib library since ntohl is a winsocket API function.
That should fix the problem!

Related

Trivial Windows DLL built with GHC 64-bit does not link

For a proof-of-concept, I want to link some trivial Haskell code into my Visual C++ application (using Visual Studio 2013). Building with GHC 7.8.3 32-bit works, but building with GHC 7.8.4 64-bit does not (notice the slight discrepancy in GHC version too).
There are 3 files: Grep.hs and StartEnd.c are built with GHC to form a DLL. main.cpp is built with Visual Studio 2013 and attempts to link in the DLL library.
I am building the DLL from this:
> ghc -shared -O -optc-O -o Grep.dll StartEnd.c Grep.hs
And from within Visual Studio, I simply link against Grep.dll.a and include C:\Program Files\MinGHC-7.8.4\ghc-7.8.4\lib\include, but linking fails with
1>main.obj : error LNK2001: unresolved external symbol _HsEnd
1>main.obj : error LNK2001: unresolved external symbol _freegrep
1>main.obj : error LNK2001: unresolved external symbol _grep
1>main.obj : error LNK2001: unresolved external symbol _HsStart
1>C:\Code\Grep\dist\Win32\Release\Grep.exe : fatal error LNK1120: 4 unresolved externals
The exact same process works when I build with 32-bit, but not 64-bit. What could I be doing wrong? (I am building a 64-bit app when attempting to link the 64-bit library.)
Source files:
Grep.hs
{-# LANGUAGE ForeignFunctionInterface #-}
module Grep where
import Foreign
import Foreign.C.String
import Data.List (isInfixOf)
filterlines f = unlines . filter f . lines
grep :: CString -> CString -> IO CString
grep i s = do
ii <- peekCString i
ss <- peekCString s
newCString $ (filterlines (isInfixOf ii)) ss
freegrep :: CString -> IO ()
freegrep s = free s
foreign export ccall grep :: CString -> CString -> IO CString
foreign export ccall freegrep :: CString -> IO ()
StartEnd.c
#include <Rts.h>
void HsStart()
{
int argc = 1;
char* argv[] = {"ghcDll", NULL}; // argv must end with NULL
// Initialize Haskell runtime
char** args = argv;
hs_init(&argc, &args);
}
void HsEnd()
{
hs_exit();
}
main.cpp
#include <HsFFI.h>
#include <Grep_stub.h>
#include <iostream>
extern "C" {
void HsStart();
void HsEnd();
}
int main(int argc, char* argv[])
{
HsStart();
HsPtr str;
str = grep("test", "This is a test\nwith many lines\nand it failed\nand the test passed");
if (str)
{
std::cout << (char*) str;
freegrep(str);
}
HsEnd();
return 0;
}
Thanks to the comments, I added the actual error output to the post. This caused me to notice that Visual Studio's confusing configuration management interface had bit me once again. It was still trying to build the Win32 version even though my configuration setting said x64. Long story short, it's working!

Dependencies and errors when using atoi

I was given the task to refactor a very old project, and in these current days i'm checking the dependencies of the executables because for some reasons, they changed since 2009, passing from 4 to 14. To be more specific, my job is to keep the dependencies as they were before 2009, but with the changes to the code occuring until today.
I tracked down the instruction that was causing the trouble. It'a function inside a library used by the project:
chain(str, pps)
char *pps;
char *str;
{
int pp = 0;
pp = atoi(pps);
// ic sunt leones.
If i comment or replace atoi with an assignment of an integer like 0, 1 or 3, the library compile fine, but the executable that is using this .lib gives me these errors:
nafxcw.lib(wincore.obj) : error LNK2001: unresolved external symbol __imp__InitCommonControls#0
nafxcw.lib(wincore.obj) : error LNK2001: unresolved external symbol __imp__DragAcceptFiles#8
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _ClosePrinter#4
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _DocumentPropertiesA#24
nafxcw.lib(appcore.obj) : error LNK2001: unresolved external symbol _OpenPrinterA#12
nafxcw.lib(filecore.obj) : error LNK2001: unresolved external symbol __imp__SHGetFileInfoA#20
nafxcw.lib(filecore.obj) : error LNK2001: unresolved external symbol _GetFileTitleA#12
If otherwise i use a different value for the assignment, like 2, 4 or every other integer, everything compile correctly and work.
Any advice? What's happening here? Why this strange behaviour?
EDIT: apparently the problem is not the atoi. If i use an home made function that do anything and accept a char* and return an int or replacing directly the second parameter of the function chain with an int and assigning it directly i still receive the same errors.
You can try this :
include ('limits.h');
int my_getnbr(char *str)
{
int i;
long nbr;
int neg;
neg = 0;
nbr = 0;
i = 0;
if (str[0] == '-')
{
neg = 1;
str++;
}
while (str[i] >= '0' && str[i] <= '9')
{
nbr = nbr * 10 + (str[i++] - '0');
if (nbr > INT_MAX)
return (0);
}
return (neg ? (int)nbr * -1 : (int)nbr);
}
It's just a home made atoi like.
Edit : INT_MAX by Alter Mann.
Edit bis : if (nbr > INT_MAX) by Alter Mann again :)
the only thing I can think of is that the simplified function is being compiled out of existence, and the linker is then helpfully assuming that there is no code to link with in the library, and so it is being dropped, unfortunately it being the only thing that is forcing includes of those MFC libraries.
I have no idea why its even requiring stuff like OpenPinter - which is included in winspool.h (via windows.h) except to say that it sounds like you have a nasty MFC-based mess to sort out. It was never much good WRT tidy builds due to its dependence on specific includes specified in specific order.

Calling MATLAB from C++ errors: unresolved external symbol

I encounter several errors when calling my MATLAB function from C++. The main idea is: firstly compile a MATLAB function and generate DLL file, and then include .h and .lib files in C++. Finally, write .cpp to test and call the function. Here's my detailed steps and please tell me where I'm wrong.
(Using MATLAB 2012b and Visual C++ 2008, Windows 7 64-bit)
In MATLAB:
mbuild -setup and mex -setup to set Visual Microsoft Visual C++ 2008 SP1 as the compiler.
Create MyAdd.m in folder C:\Users\WangYudong\Documents\MATLAB\MyAdd_M and the function is like:
function [c] = MyAdd(a, b)
c = a + b;
mcc -W cpplib:libMyAdd -T link:lib MyAdd to compile MyAdd.m and generate several files including libMyAdd.dll, libMyAdd.h, libMyAdd.lib and other files.
In C++
Select VC++ Directories → Include files to add E:\MATLAB\R2012b\extern\include.
Select VC++ Directories → Library files to add
E:\MATLAB\R2012b\extern\lib\win64\microsoft and
C:\Users\WangYudong\Documents\MATLAB\MyAdd_M.
Select Linker → Input → Additional Dependencies to add new entries:
mclmcr.lib
mclmcrrt.lib
libmx.lib
libmat.lib
libMyAdd.lib
Create a new MyAdd_test.cpp and put libMyAdd.dll, libMyAdd.h and libMyAdd.lib in the same folder. Add libMyAdd.h in Header Files, libMyAdd.h and libMyAdd.lib in Resource Files.
Code of MyAdd_test.cpp is like:
#include "mclmcr.h"
#include "matrix.h"
#include "mclcppclass.h"
#include "libMyAdd.h"
int main() {
double a = 6;
double b = 9;
double c;
// initialize lib
if( !libMyAddInitialize()) {
std::cout << "Could not initialize libMyAdd!" << std::endl;
return -1;
}
// allocate space
mwArray mwA(1, 1, mxDOUBLE_CLASS);
mwArray mwB(1, 1, mxDOUBLE_CLASS);
mwArray mwC(1, 1, mxDOUBLE_CLASS);
// set data
mwA.SetData(&a, 1);
mwB.SetData(&b, 1);
// use function: MyAdd
MyAdd(1, mwC, mwA, mwB);
// get data
c = mwC.Get(1, 1);
printf("c is %f\n", c);
// terminate the lib
libMyAddTerminate();
// terminate MCR
mclTerminateApplication();
return 0;
}
At last, the result is
Compiling...
MyAdd_test.cpp
Linking...
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclTerminateApplication_proxy referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddTerminate referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl MyAdd(int,class mwArray &,class mwArray const &,class mwArray const &)" (__imp_?MyAdd##YAXHAAVmwArray##ABV1#1#Z) referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _libMyAddInitialize referenced in function _main
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclGetMatrix referenced in function "public: __thiscall mwArray::mwArray(unsigned int,unsigned int,enum mxClassID,enum mxComplexity)" (??0mwArray##QAE#IIW4mxClassID##W4mxComplexity###Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppGetLastError referenced in function "public: static void __cdecl mwException::raise_error(void)" (?raise_error#mwException##SAXXZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _mclcppCreateError referenced in function "public: __thiscall mwException::mwException(void)" (??0mwException##QAE#XZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_addref referenced in function "public: __thiscall mwException::mwException(class mwException const &)" (??0mwException##QAE#ABV0##Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _ref_count_obj_release referenced in function "public: virtual __thiscall mwException::~mwException(void)" (??1mwException##UAE#XZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _error_info_get_message referenced in function "public: virtual char const * __thiscall mwException::what(void)const " (?what#mwException##UBEPBDXZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_getV_int referenced in function "public: class mwArray __cdecl mwArray::GetPromoted(unsigned int,...)" (?GetPromoted#mwArray##QAA?AV1#IZZ)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_set_numeric_mxDouble referenced in function "public: void __thiscall mwArray::SetData(double *,unsigned int)" (?SetData#mwArray##QAEXPANI#Z)
MyAdd_test.obj : error LNK2019: unresolved external symbol _array_ref_get_numeric_mxDouble referenced in function "public: __thiscall mwArray::operator double(void)const " (??BmwArray##QBENXZ)
C:\Users\WangYudong\Documents\Visual Studio 2008\Projects\MyAdd_C\Debug\MyAdd_C.exe : fatal error LNK1120: 13 unresolved externals
Actually, the work above is my test to call a custom MATLAB function from C++. My following work is to convert a MATLAB program to C++, which contains image processing functions like imread, edge, strel, etc. I've tried MATLAB Coder, but it can't convert MATLAB functions. So I try the method above. Is it an efficient way to convert those functions or should I implement them using OpenCV?
Before you start, make sure you have a supported compiler installed. That would be Visual C++ and possibly Windows SDK if you are using VS Express edition on a 64-bit Windows. Next you need to configure MATLAB by running these steps at least once:
>> mex -setup
>> mbuild -setup
Now given the following simple function:
MyAdd.m
function c = MyAdd(a,b)
c = a + b;
end
We want to build a C++ shared library using the MATLAB Compiler mcc:
>> mcc -N -W cpplib:libMyAdd -T link:lib MyAdd.m -v
This will produce a couple of files including a header file, a DLL, and an import library:
libMyAdd.h
libMyAdd.dll
libMyAdd.lib
Next we create a C++ program to test the above library:
MyAdd_test.cpp
#include "libMyAdd.h"
int main()
{
// initialize MCR and lib
if (!mclInitializeApplication(NULL,0)) {
std::cerr << "could not initialize the application" << std::endl;
return -1;
}
if(!libMyAddInitialize()) {
std::cerr << "Could not initialize the library" << std::endl;
return -1;
}
try {
// create input
double a[] = {1.0, 2.0, 3.0, 4.0};
double b[] = {5.0, 6.0, 7.0, 8.0};
mwArray in1(2, 2, mxDOUBLE_CLASS, mxREAL);
mwArray in2(2, 2, mxDOUBLE_CLASS, mxREAL);
in1.SetData(a, 4);
in2.SetData(b, 4);
// call function
mwArray out;
MyAdd(1, out, in1, in2);
// show result
std::cout << "in1 + in2 = " << std::endl;
std::cout << out << std::endl;
double c[4];
out.GetData(c, 4);
for(int i=0; i<4; i++) {
std::cout << c[i] << " " << std::endl;
}
} catch (const mwException& e) {
std::cerr << e.what() << std::endl;
return -2;
} catch (...) {
std::cerr << "Unexpected error thrown" << std::endl;
return -3;
}
// cleanup
libMyAddTerminate();
mclTerminateApplication();
return 0;
}
We could compile this program right from inside MATLAB using:
>> mbuild MyAdd_test.cpp libMyAdd.lib -v
>> !MyAdd_test
We could also compile it ourselves using Visual Studio. We start by creating a native console application, then set the project settings as follows:
From the menu, select "Project > Properties" and apply the settings to "All configurations" (both debug and release targets)
Under C/C++ properties, set the "Additional Include Directories" by adding both the directory containing the generated header file libMyAdd.h, in addition to the directory containing MATLAB's header files:
$matlabroot\extern\include
Similarly under "Linker", set the "Additional Library Directories". That would be the same directory as before containing libMyAdd.lib, as well as in my case:
$matlabroot\extern\lib\win32\microsoft
Then go to "Linker > Input" and add the following inside "Additional Dependencies":
libMyAdd.lib
mclmcrrt.lib
Finally under "Debugging > Environment", you might want to extend the PATH environment variable to include the directory containing the generated libMyAdd.dll file. That way you can directly hit F5 to compile run the program directly from inside VS. This will be something like:
PATH=%PATH%;C:\path\to\output\folder
If you do this kind of thing often, you could create a property sheet once, which could then be reused in other VC++ projects. See this answer for an example.

Linker error using openCV with fftw3 Visual Studio 2010

I'm having trouble trying to implement fftw3 with openCV in the same project.
I'm new to c/c++ and any help will be much appreciated.
The following is just a sample code i used:
#include "fftw3.h"
#include <stdlib.h>
int main(){
fftw_complex *in, *out;
fftw_plan p;
int N=8;
in = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
out = (fftw_complex*) fftw_malloc(sizeof(fftw_complex) * N);
p = fftw_plan_dft_1d(N, in, out, FFTW_FORWARD, FFTW_ESTIMATE);
for(int i=0; i<N; i++){
in[i][0]=i;
in[i][1]=0;
}
fftw_execute(p);
for(int i=0; i<N; i++){
printf("R->%f\tI->%f\n", out[i][0], out[i][1]);
}
system("pause");
fftw_destroy_plan(p);
fftw_free(in); fftw_free(out);
}
The code is compiling fine when its in its own project.
But when I'm trying to link the fftw3 and openCV together i get a linker error:
1>video.obj : error LNK2001: unresolved external symbol _fftw_destroy_plan
1>video.obj : error LNK2001: unresolved external symbol _fftw_execute
1>video.obj : error LNK2001: unresolved external symbol _fftw_plan_dft_2d
1>video.obj : error LNK2001: unresolved external symbol _fftw_malloc
1>video.obj : error LNK2001: unresolved external symbol _fftw_free
1>D:\C WorkSpace\Viedo_CV\Video test\Release\Video test.exe : fatal error LNK1120: 5 unresolved externals
1>
1>Build FAILED.
I double checked all the linker configurations and they seem to be fine(as i said all is working well when in a separate projects).
The openCV library is working fine.
Unfortunately I can't post my real code.
All the openCV i use is in:
#include <opencv\\cv.h>
#include <opencv\\highgui.h>
#include <windows.h>
Any help will be much appreciated.
I got it figured out, when I installed openCV I created an environmental variable to the openCV build location and added the openCV DLL's location to the Path variable.
What i didn't notice is that I added the x86 version of the DLL's to the Path variable and my project was setup as x64, the reason it worked in another project is because I copied the x64 DLL's to the project folder it self.
All worked fine after I edited the Path to the correct x64 DLL's location.

Open an HDF5 file error

I created an HDF5 file open function like the following:
int OpenHDF5(string sFileName)
{
// Check for valid HDF5 file
if (!H5File::isHdf5(sFileName.c_str()))
{
// Invalid HDF5 file
return -1
}
// Try block to detect exceptions raised by any of the calls inside it
try
{
// Turn off the auto-printing when failure occurs so that we can handle the errors appropriately
Exception::dontPrint();
// Now Open the file
H5File file( sFileName.c_str(), H5F_ACC_RDONLY );
}
// Catch failure caused by the H5File operations
catch( FileIException error )
{
error.printError();
return -1
}
return 0
}
No compiling error occurred, but failed to link with the following exceptions:
Linking...
Creating library F:\Tips\Debug\Tips.lib and object F:\Tips\Debug\Tips.exp
TwinSatObservation.obj : error LNK2001: unresolved external symbol "public: static class H5::FileCreatPropList const H5::FileCreatPropList::DEFAULT" (?DEFAULT#FileCreatPropList#H5##2V12#B)
TwinSatObservation.obj : error LNK2001: unresolved external symbol "public: static class H5::FileAccPropList const H5::FileAccPropList::DEFAULT" (?DEFAULT#FileAccPropList#H5##2V12#B)
F:\Tips\Debug\Tips.exe : fatal error LNK1120: 2 unresolved externals
I added the following libraries to "Addtional Dependencies" input box of the VS 2008 Linker
hdf5dll.lib
hdf5_hldll.lib
hdf5_cppdll.lib
hdf5_hl_cppdll.lib
Would you please tell me which library I forgot to add? Thank you very much!
As for hdf5-1.8.17 with either VS2010 or VS2015, defining H5_BUILT_AS_DYNAMIC_LIB as a preprocessor setting (Project > Properties > C/C++ > Preprocessor > Preprocessor Definitions) cures exactly the same symptom for me. Thanks to the original post.
Add HDF5CPP_USEDLL;_HDF5USEDLL_; in Preprocessor Definitions input box.