C++ Dll linking Curl - c++

I am trying to create a dll which uses the curl library for a very simple function. Everything works just fine, the only problem is, that the curl linking does not seem to work properly.
I use the same linking, preprocessordefines and include directories like in my executable project where it works just fine so i am pretty sure it´s not about my linking or binary files of the libary.
Are there any special properties to link a libary to a dll?
My minimal sample code:
C++ Mainfile:
#include "main.h"
#include <Windows.h>
#include <curlpp/cURLpp.hpp>
#include <curlpp/Easy.hpp>
#include <curlpp/Info.hpp>
#include <curlpp/Options.hpp>
#include <curlpp/Exception.hpp>
namespace CurlDll
{
void CallHost::Try()
{
curlpp::Easy request;
request.setOpt(new curlpp::options::UserAgent("Mozilla/4.0"));
request.setOpt(new curlpp::options::AutoReferer(true));
request.setOpt(new curlpp::options::FollowLocation(true));
request.setOpt(new curlpp::options::Url("http://xml.utrace.de"));
request.perform();
MessageBox(0,"lololowwwwwwwwwwwl", "wqgqwwwwwgq", MB_OK |MB_ICONINFORMATION);
}
}
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason)
{return 1;}
Header file:
#ifdef MAIN_EXPORTS
#define MAIN_API __declspec(dllexport)
#else
#define MAIN_API __declspec(dllimport)
#endif
#include <iostream>
namespace CurlDll
{
class CallHost
{
public:
static MAIN_API void Try();
};
}
i get following linking errors #drescherjm
(47 , i will just post a few, i think that shouls be enough)
ERROR 2 error LNK2001: unresolved external Symbol
"__imp__WSAStartup#8".
ERROR 11 error LNK2001: unresolved external Symbol
"__imp__WSAGetLastError#0".
ERROR 33 error LNK2001: unresolved external Symbol
"__imp__setsockopt#20".

The linker errors are telling you that the linker cannot find definitions for these functions: WSAStartup, WSAGetLastError, setsockopt. These functions are defined in the import library Ws2_32.lib. You need to supply that import library to the linker.
This information is given in the documentation for the functions. For instance, the documentation for WSAStartup. At the bottom of the documentation topic is a table listing requirements. Note the required library, Ws2_32.lib.

The symbols WSAStartup, WSAGetLastError and setsocketopt are part of the Windows API, in Ws2_32.lib (e.g. see http://msdn.microsoft.com/en-gb/library/windows/desktop/ms742213(v=vs.85).aspx )
You should include ws2_32.lib as an additional library when you link your DLL. If you're using Visual Studio, it's likely that the search path should already find it; just add it as an additional library.
So actually, I suspect you're not using the exact same linker options compared to your .exe
If you're building a .exe or a .dll, the linker needs to ensure it can resolve ALL known symbols at link time.

Related

Creating a C++ .Net Core wrapper for native library results in error LNK2028

I'm trying to create a managed (.net core) C++/CLI wrapper for a native library (Srt) but when referencing methods in the native lib I'm getting 2 build errors. I've referenced the headers srt.h from the native library, tried calling a method named srt_cleanup but get this on build:
error LNK2028: unresolved token (0A00000B) "extern "C" int __cdecl srt_cleanup(void)" (?srt_cleanup##$$J0YAHXZ) referenced in function "public: void __clrcall HeliosMediaStreamCliSrt::SrtReceiver::Stop(void)" (?Stop#SrtReceiver#HeliosMediaStreamCliSrt##$$FQE$AAMXXZ)
error LNK2019: unresolved external symbol "extern "C" int __cdecl srt_cleanup(void)" (?srt_cleanup##$$J0YAHXZ) referenced in function "public: void __clrcall HeliosMediaStreamCliSrt::SrtReceiver::Stop(void)" (?Stop#SrtReceiver#HeliosMediaStreamCliSrt##$$FQE$AAMXXZ)
Sample:
// pch.h
#ifndef PCH_H
#define PCH_H
#include <srt.h>
#include <stdio.h>
#include <tchar.h>
#include <memory>
#include <map>
#include <stdlib.h>
#endif //PCH_H
// SrtReceiver.h
#pragma once
using namespace System;
namespace TestSrt {
public ref class SrtReceiver {
public:
void SrtReceiver::Stop();
private:
};
}
// SrtReceiver.cpp
#include "pch.h"
#include "SrtReceiver.h"
namespace TestSrt {
void SrtReceiver::Stop() {
srt_cleanup();
}
}
Project Configuration:
General -> Configuration Type: Dynamic Library (.dll)
Advanced -> Common Language Runtime Support: .NET Core Runtime Support (/clr:netcore)
Advanced -> .NET Core Target Framework: .NET 5.0
C/C++ -> General -> Common Language Runtime Support: NetCore
I'm pretty rusty with C++ so I'm unfamiliar with the build system and its resulting in confusing errors. I've never tried doing anything with .net core in c++ before as C# is more my speed. This looks like something to do with build properties and the compiler is looking for the wrong internal method names. How would I go about fixing this?
I simply forgot to include the paths to the .lib files in Linker -> Input -> Additional Dependencies: /path/to/srt.lib
Simple, but spent hours of google searching to no avail. Hopefully this helps out other a C++ beginners as I didn't come across it.

Create A C++ Library That References A Third Party Library [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 4 years ago.
I am trying to create a C++ library that utilizes a third party library. My custom library compiles fine. However, when I try to test it out with a console application, the console application gives me errors from building. My setup is below:
Platform - Visual Studio Community 2017
//MyLib.h
#include <ThirdPartyLib.h>
namespace MyLib
{
class MyLibClass
{
public:
static void SomeFunction();
};
}
//MyLib.cpp
#include MyLib.h
void MyLib::MyLibClass::SomeFunction()
{
ThirdPartyLib::ThirdPartyFunction();
}
//MyConsoleApplication.cpp
#include "..\MyLib\MyLib.h"
#pragma comment(lib,"..\\Debug\\Mylib.lib")
int main()
{
MyLib::SomeFunction();
return 0;
}
My custom library is able to compile fine. When I try to compile the console application, I get a bunch of errors about the third party library like the one below.
LNK2019: unresolved external symbol 'public virtual _thiscall ThirdPartyLib::Foo::~Foo(void)' referenced in function 'private void _thiscall MyLib::MyLibClass::SomeFunction(void)'
I have given my console application the location of where it can find the third party library as well. Can anyone help?
You haven't included ThirdParyLib.lib in you program, have you? What you are getting are linker errors complaining that it cannot find the functions definitions of the functions in ThirdPartyLib.h header file.
Try this :
#pragma comment(lib,"..\\Debug\\ThirdPartyLib.lib")
Please note that ThirdPartyLib.lib is placed in the debug directory as per above example.

Protobuf - Refuses to link vs2013 or vs2015

As the title states I cannot get protobuf to link successfully.
Here is the small test program. note the AddressBook class was generated usin gthe protoc compiler that was built when i compiled protobuf.Additionally, this proto file is part of googles protobuf examples that is included with the source.
#define PROTOBUF_USE_DLLS
#include <iostream>
#include "addressbook.pb.h"
#pragma comment(lib, "libprotocd.lib")
#pragma comment(lib, "libprotobufd.lib")
#pragma comment(lib, "libprotobuf-lited.lib")
int main(int argc, const char* argv[])
{
tutorial::AddressBook ab;
return 0;
}
Here are the errors I am receiving.
LNK2001: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const * const google::protobuf::internal::empty_string_" (?empty_string_#internal#protobuf#google##3PEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##EB)
LNK2001: unresolved external symbol "__int64 google::protobuf::internal::empty_string_once_init_" (?empty_string_once_init_#internal#protobuf#google##3_JA)
Some assertions:
Same code running on linux.. With the exception of -lprotoc -lprotobuf -lprotobuf-lite vs. the #pregma stuff
Happens both in VS2013 and VS2015
Tried both 3.0-Release and latest code from git. Same issue.
I have tried this with both the debugtarget as well as the release (when using release removed trailing d from library name) target
Made sure that both the additional include and library directories were set in the VS project. linker --> General --> Additional Library Directories is set to the place where the compiled .dll are located`
At this point I pretty much turned my attention to google to see what I could find. I ran across the following.
2015 not fully supported. It was old, but figured I would try vs2013
protobuf dynamic linking Yes. When building with cmake I enabled shared library support and have the #define in my program
disable inline expansion
Still, not able to progress past the above errors. Any help is greatly appreciated.

Qr read and generation under QT creator using leadtools

I want to buy license for QR codes read and generating from Leadtools but first I want to try their demo tools. I'm using MSVC 2013 x64 compiler. I think I did everything as follows in documentation:
Copied all dll's to my project directory (where build and release folder are located)
Copied Include and Lib folders to my project directory and add this lines to .pro file.
LIBS += -L$$PWD/Lib/CDLLVC12/x64/ -lLtkrn_x
INCLUDEPATH += $$PWD/Include
PRE_TARGETDEPS += $$PWD/Bin/CDLLVC12/x64/Ltkrnx.dll
include and #define LTV19_CONFIG, here is my code:
#define LTV19_CONFIG
#include <iostream>
#include <Ltkrn.h>
#include <ClassLib/LtWrappr.h>
using namespace std;
int main( ){
if( LT_KRN == LBase::LoadLibraries( LT_KRN, LT_DLGKRN))
cout << "success" << endl;
L_TCHAR licenseFile[] = L"d:\\temp\\TestLic.lic";
L_TCHAR key[] = L"xyz123abc";
LSettings::SetLicenseFile( licenseFile, key);
return 0;
}
Ask leadtools support, but they don't have much experience with working with QT...
When I tries to build application I get following errors:
LNK2019: unresolved external symbol "__declspec(dllimport) public: static unsigned int __cdecl LBase::LoadLibraries(unsigned int,unsigned int)" (__imp_?LoadLibraries#LBase##SAIII#Z) referenced in function main
LNK2019: unresolved external symbol "__declspec(dllimport) public: static int __cdecl LSettings::SetLicenseFile(wchar_t *,wchar_t *)" (__imp_?SetLicenseFile#LSettings##SAHPEA_W0#Z) referenced in function main
For following methods documentation says that I only need one dll/lib package (ltkrn). How to fix it? Still I don't get differences between static and dynamic linkage and this could be the problem.
If your linker accepted the 64-bit Ltkrn_x.lib, this suggests the problem is related to how you're using LEADTOOLS and not to QT. That's why I'm posting this as suggested reply instead of a note.
When programming using LEADTOOLS with C++, you normally use one of 2 sets of headers and LIBs:
Either include L_Bitmap.H (or a bunch of headers that includes LtKrn.H) and use the Ltkrn_x, Ltfil_x, etc. set of LIB files.
Or include ClassLib\LtWrappr.h and use only one LIB file, which in your case is Ltwvc_x.lib
Although in both cases you would be using many of the same DLL files such as Ltfilx.dll and Ltkrnx.dll, the reason you don't need their LIB files when using LtWrapper is that the ClassLibrary performs late (on demand) loading of these DLLs at run-time instead of referencing their LIB files at link time.
That's also why you need to call LBase::LoadLibraries() and specify the DLLs you need before your code uses these DLLs.
So to summarize, please try this:
Remove #include "Ltkrn.h"
Remove the linker reference to Ltkrn_x.lib (although you'll need the DLL)
Keep #include "ClassLib/LtWrappr.h"
Add a linker reference to Ltwvc_x.lib

Linker problem on VS2005 with VC++

Here's the scenario:
Platform:
VS2005 and language is VC++
Situation:
There's just 1 assembly CMPW32. It has 2 projects:
1 is a DLL project called CMPW32 and the 2nd one is an .exe project called Driver
They both share the same Debug folder under the main assembly folder.
I have been able to successfully export a few functions from the DLL. The Driver project accesses 1 of these exported functions. (First of all I am not if functions need to be exported for projects in the SAME assembly to be able to use them. I can just include the header files and use the functions I think.)
Following is are a few lines of code from some files which you might find useful to analyze my problem:
//main.cpp file from the Driver project which is meant to generate Driver.exe
#pragma comment(lib, "winmm.lib")
#include <CM.h>
#include "conio.h"
#include "CMM.h"
#include "CMF.h"
#define C_M_F _T("c:\\CannedMessages.en-US")
int_tmain (int argc, TCHAR* argv [])
{
CMM myobjModel;
CMF::Read (CANNED_MESSAGES_FILE, myobjModel);
getch();
}
//CMM.h file
#ifndef C_M_M
#define C_M_M
#include "CMD.h"
#include "CMC.h"
#include "CM.h"
#define _C_M_DLL
#include "CMP.h"
class CM_DLL_API CMM
{ //some code here...
}
//CMF.h
#ifndef C_M_F
#define C_M_F
#include "CMM.h"
#define _C_M_DLL
#include "CMP.h"
class CM_DLL_API CMF
{ //some code here...
}
//CMP.h
#ifndef C_M_P
#define C_M_P
#include "CMD.h"
#define C_M_B_F _T("CannedMessages.")
#ifdef _C_M_DLL
#define CM_DLL_API __declspec( dllexport )
#else
#define CM_DLL_API __declspec( dllimport )
#endif
extern "C"
{
//list of functions to be exported..
}
ERRORS on building the solution:
Error13 error LNK2019: unresolved external symbol "public: __thiscall CMM::~CMM(void)" (??1CMM##QAE#XZ) referenced in function _wmain main.obj
Error15 fatal error LNK1120: 2 unresolved externals C:\"somepath here which I cant disclose"\Projects\CMPW32\Debug\Driver.exe
Please Note: If I choose to build only the CMPW32 DLL project, there are no errors and the CMPW32.dll file gets generated in the debug folder with the correct functions getting getting exported.
However there seems to be some linking problem that is pretty evident and I don't know what's going on. I have included every required file and also have entered the required .lib in the input of the "Project Settings". The paths have been set correctly too.
It would be really helpful if someone could help me out with this. Please lemme know if additional information required.
Thanks,
Viren
Looks like your Driver.exe project does not include the CPP source files of the CMM class, likely CMM.cpp.
or
You have declare a destructor for CMM class in your .H file (CMM.H) and forgot to implement it in the .CPP file (CMM.CPP).