boost function and bind external symbol - c++

I need to use bind and function in my program.But unfortunately vs2010 can't link my program.
I used following example from boost::bind documentation
#include <boost\bind.hpp>
#include <boost\function.hpp>
#include <functional>
class button
{
public:
boost::function<void()> onClick;
};
class player
{
public:
void play();
void stop();
};
button playButton, stopButton;player thePlayer;
void connect()
{
playButton.onClick = boost::bind(&player::play, &thePlayer);
stopButton.onClick = boost::bind(&player::stop, &thePlayer);
}
void main(int argc, char* argv[])
{
connect();
}
Error 1 error LNK2019: unresolved external symbol "public: void __thiscall player::stop(void)" (?stop#player##QAEXXZ) referenced in function "void __cdecl connect(void)" (?connect##YAXXZ)
i have tried the newest 32 and 64 verion of BoostPro and followed this tutorial http://www.youtube.com/watch?v=5AmwIwedTCM.All but vs still produces same error...
VS2010 project setting include/lib path
https://dl.dropbox.com/u/47585151/vs.png
however when I turned on
Linker->General->ShowProgress ->For Libraries Searched (/VERBOSE:Lib)
i noticed that VS is searching only for these libraries which are defined in
Linker->Input->Additional Dependencies
http://pastebin.com/BCpEt8Zq
is it possible to check which .lib boost need for boost::bind and boost::function under vs2010?

This problem has nothing to do with any boost library (both are header-only). Try to simply call start and stop from within connect and you should get the same error. Read it carefully, it tells you what is missing.

Related

Static variable link error in MSVC even though it is created in the cpp file

I am working on a cross-platform (Linux & Windows) library. One of my class has a static member defined in the header. And I created it in a CPP file like
namespace raisim {
std::function<void()> RaiSimMsg::fatalCallback_ = []() { exit(1); };
}
The header file
#ifndef RAISIM_MESSAGE_LOGGER_HPP
#define RAISIM_MESSAGE_LOGGER_HPP
#include <chrono>
...
namespace raisim {
class RaiSimMsg {
public:
...
void stream(const char *file, const int line, std::stringstream &msg, int severity) {
if (severity == RSEVERITY_FATAL)
fatalCallback_();
}
private:
static std::function<void()> fatalCallback_;
};
}
#endif //RAISIM_MESSAGE_LOGGER_HPP
This works perfectly with GCC and Clang in Linux. But MSVC is giving a link error. Does MSVC have its own rules for static members??
Full error log
1>anymal.obj : error LNK2001: unresolved external symbol "private: static class std::function<void __cdecl(void)> raisim::RaiSimMsg::fatalCallback_" (?fatalCallback_#RaiSimMsg#raisim##0V?$function#$$A6AXXZ#std##A)
1>C:\Users\ultrafrog\source\repos\raisim\build\benchmark\Debug\speed_test_anymal.exe : fatal error LNK1120: 1 unresolved externals
1>Done building project "speed_test_anymal.vcxproj" -- FAILED.
Jack Dingler's answer here
https://www.codeproject.com/Questions/585271/Aplusstaticplusmemberplusvariableplusexportpluserr
worked. So the issue is that the Windows linker needs to know that if it has to import or export the symbol. So we have to declare __declspec(dllexport) when we export (when we build the library) and __declspec(dllimport) for using it. We can switch that using a compilation flag. I am not sure why they designed the linker like this but anyway it works now.

boost::process throws LNK2019: unresolved external symbol

I've installed Boost using vcpkg. Now I'm trying a "Hello World" with Boost:process. Suppose I start with Boost commented out everything works fine
#include <iostream>
//#include <boost/process.hpp>
//#include <boost/process/windows.hpp>
//namespace bp = boost::process;
int main(int argc, char * argv[])
{
int result = std::system("echo Hello");
}
Now if I uncomment Boost but don't use it i.e. I still call
std::system("echo Hello");
it compiles but throws two link errors
"__declspec(dllimport) class boost::system::error_category const & __cdecl boost::system::system_category(void)" (__imp_?system_category#system#boost##YAABVerror_category#12#XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'native_ecat''(void)" (??__Enative_ecat#system#boost##YAXXZ)
"__declspec(dllimport) class boost::system::error_category const & __cdecl boost::system::generic_category(void)" (__imp_?generic_category#system#boost##YAABVerror_category#12#XZ) referenced in function "void __cdecl boost::system::`dynamic initializer for 'errno_ecat''(void)" (??__Eerrno_ecat#system#boost##YAXXZ)
I've tried adding Additional Library Dependencies for
C:\vcpkg-master\installed\x86-windows\lib
C:\vcpkg-master\packages\boost_x86-windows\debug\bin
C:\vcpkg-master\packages\boost_x86-windows\debug\lib
C:\vcpkg-master\packages\boost_x86-windows\lib
C:\vcpkg-master\packages\boost_x86-windows\bin
but none of those resolve the error.
Does anyone know where to find the correct link librries?
If I actually call Boost with
int result = bp::system("echo Hello");
there are 9 unresolved externals.
As far as I can see, it's an installer issue with VCPKG.
I did it manually, I added this directory to
$(SolutionDir)..\vcpkg\installed\x64-windows\lib
To
Project -> Properties-> Liker -> Additional Library Directories
and added this line to stdafx.h
#pragma comment(lib, "boost_system-vc140-mt-1_65_1.lib")
You need to link boost_system. How you achieve that in your setup is up to you. Usually on MSVC it's automatic.
See the relevant documentation which has several useful approaches with examples: http://www.boost.org/doc/libs/1_65_1/more/getting_started/windows.html#link-your-program-to-a-boost-library

C++ unresovled external symbol link error related with name mangling?

all
i created a static lib project with VS 2010 sp1, and simply defined a class with a member function (code snippet):
.h:
namespace puphttp{
class CRequester
{
public:
void RequesterUpdate();
};
}
.cpp:
#include "StdAfx.h"
#include "Requester.h"
#include <iostream>
using namespace std;
namespace puphttp{
void CRequester::RequesterUpdate()
{
cout<<"updating";
}
}
at last i linked the lib file and tried to invoke the following code:
puphttp::CRequester c;
c.RequestUpdate();
the following link error occur as i compile:
error LNK2001: unresolved external symbol "public: void __cdecl puphttp::CRequester::RequestUpdate(void)" (?RequestUpdate#CRequest#puphttp##QEAAXXZ)
then i used dumpbin to check the actual function name in my lib, it's:
(?RequestUpdate#CRequest#puphttp##QAEXXZ)
so the difference is QEAAXXZ vs QAEXXZ, i didn't have time to get into name mangling rule yet, so any quick answer? really much appreciated.

std::tr1::function gives linking error for x64 configuration in VS2008 SP1

Using std::tr1::function in x64 configuration, I get below linking error in VS2008SP1. The same code works correctly for Win32 configuration. Is this a bug in 64 bit compiler, or am I missing something?
#include <iostream>
#include <functional>
typedef std::tr1::function<void()> FnCallBack;
void test()
{
std::cout << "Test";
}
int main(int argc, char* argv[])
{
FnCallBack f = &test;
f();
return 0;
}
error LNK2019: unresolved external symbol "__declspec(dllimport) void __cdecl std::tr1::Xfunc(void)" (_imp_?_Xfunc#tr1#std##YAXXZ) referenced in function "public: void __cdecl std::tr1::_Function_impl0::operator()(void)const " (??R?$_Function_impl0#X#tr1#std##QEBAXXZ)
I met the same issue with you! and I checked my vs2008 version and project settings, looks all are OK. at Win32 configuration , there is no error but X64 I got the link error samed with you.
At last, I install vs2008 sp1 again (note: not re-install, just run vs2008 sp1 setup again). the link error disappeared.
I don't know why, but maybe you could try it to fix this issue.

Wrapping a C++ DLL with a managed class

I'm trying to wrap a unmanaged C++ DLL with managed C++ and I keep getting linking errors.
even though I include my library.lib in the project and include the correct header file.
This is the managed class:
#pragma once
#include "..\Terminal\Terminal.h"
public ref class ManagedTerminal
{
private:
Terminal * m_unTerminal;
public:
ManagedTerminal(void)
{
m_unTerminal = new Terminal();
}
};
and this is the unmanaged class:
#include "..\Core1.h"
#include "..\Core2.h"
__declspec(dllexport) class Terminal
{
private:
CoreObj m_core;
public:
Terminal();
void Init(char* path, char* filename);
void Start();
void Stop();
void Run();
Array<Report> GetSnapshot();
~Terminal(void);
};
and the errors I get are:
Error 5 error LNK2028: unresolved token (0A0000B3) "public: __thiscall Terminal::Terminal(void)" (??0Terminal##$$FQAE#XZ) referenced in function "public: __clrcall ManagedTerminal::ManagedTerminal(void)" (??0ManagedTerminal##$$FQ$AAM#XZ) ManagedTerminal.obj TerminalWrapper
Error 6 error LNK2019: unresolved external symbol "public: __thiscall Terminal::Terminal(void)" (??0Terminal##$$FQAE#XZ) referenced in function "public: __clrcall ManagedTerminal::ManagedTerminal(void)" (??0ManagedTerminal##$$FQ$AAM#XZ) ManagedTerminal.obj TerminalWrapper
can anybody tell me what's wrong?
thanks :)
You have to match all of the build settings -- specifically the calling conventions (CDECL vs. STDCALL) -- in order to have a successful link.
Since .NET 2.0, you have also had to link to the c-runtime dynamically, so make sure that both the .dll and the managed C++ project do this.
Basically, go into the properties dialog for both projects and make sure that things that affect the call are the same.