How to use class from a different project in C++ with Qt? - c++

There's this class MailHandler in the solution MailTest, which I use to send signals to the class MailCom in the solution MailResponse. My problem revolves around creating an object of the MailHandler class in the MailCom class, which is in a different project, and doing operations from here. What is the most efficient way of establishing the connection without modifying the MailTest project into a lib or DLL?
I need to link with a correct path, but I'm not sure at which settings are important in this case. This is the current error I'm getting when trying to instantiate the MailHandler object:
1> Creating library ..\..\bin\pvc6d.lib and object ..\..\bin\pvc6d.exp
1>CMailCom.obj : error LNK2019: unresolved external symbol "public: class QStringList __thiscall MailHandler::ReturnAllFunctions(void)" (?ReturnAllFunctions#MailHandler##QAE?AVQStringList##XZ) referenced in function "public: __thiscall pvc::hardwarecom::CMailCom::CMailCom(class pvc::data::CData &,class pvc::data::CRudderServo *,class pvc::data::CPropulsionManager *,class QGraphicsScene *)" (??0CMailCom#hardwarecom#pvc##QAE#AAVCData#data#2#PAVCRudderServo#42#PAVCPropulsionManager#42#PAVQGraphicsScene###Z)
1>CMailCom.obj : error LNK2019: unresolved external symbol "public: __thiscall MailHandler::MailHandler(class QObject *)" (??0MailHandler##QAE#PAVQObject###Z) referenced in function "public: __thiscall pvc::hardwarecom::CMailCom::CMailCom(class pvc::data::CData &,class pvc::data::CRudderServo *,class pvc::data::CPropulsionManager *,class QGraphicsScene *)" (??0CMailCom#hardwarecom#pvc##QAE#AAVCData#data#2#PAVCRudderServo#42#PAVCPropulsionManager#42#PAVQGraphicsScene###Z)
1>..\..\bin\pvc6d.exe : fatal error LNK1120: 2 unresolved externals

If you want to use a class of a external project, you can try to #include its header file (MailHandler.h, MailHandler.hxx etc.). I assume thats what you did. Obviously there is also a according source file (MailHandler.cpp, MailHandler.cxx). You can try to add this file to your current project. If you are lucky it compiles and links and your unresolved externals go away. More likely the source file has other dependencies and does not compile/link right away.
This is rather a hack - not a robust long term solution.
It's better to copy the file with its dependencies into your project
It's best to extract the needed classes in a separate project (i.e creating a lib or dll) that your project and that the other solution 'MailTest' are using as a independent client.

You are missing the correct libraries. The linker does not know about the functions called, add LIBS += -Lpath/to/lib -lthelib to your pro file.

Related

lnk2001 error in VS trying to use the Bullet physics library

I'm new to C++, Visual Studio (2019) and Bullet (2.89).
I've been tying to build the Hello_World.cpp from Bullet for the past few days but I'm stuck on these 5 linking errors:
1>Hello_World.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall btCollisionShape::getBoundingSphere(class btVector3 &,float &)const " (?getBoundingSphere#btCollisionShape##UBEXAAVbtVector3##AAM#Z)
1>Hello_World.obj : error LNK2001: unresolved external symbol "public: virtual float __thiscall btCollisionShape::getAngularMotionDisc(void)const " (?getAngularMotionDisc#btCollisionShape##UBEMXZ)
1>Hello_World.obj : error LNK2001: unresolved external symbol "public: virtual float __thiscall btCollisionShape::getContactBreakingThreshold(float)const " (?getContactBreakingThreshold#btCollisionShape##UBEMM#Z)
1>Hello_World.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall btConvexShape::project(class btTransform const &,class btVector3 const &,float &,float &,class btVector3 &,class btVector3 &)const " (?project#btConvexShape##UBEXABVbtTransform##ABVbtVector3##AAM2AAV3#3#Z)
1>Hello_World.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall btSphereShape::calculateLocalInertia(float,class btVector3 &)const " (?calculateLocalInertia#btSphereShape##UBEXMAAVbtVector3###Z)
There are other people who had this or a similar problem but got no answer or it didn't work for me.
setup:
I used premake to set up the VS solution, Bullet is organized in static Libraries (as projects with source code that gets compiled to lib files) and I am now trying to link them with an application-project.
all projects are using static linking (for debug).
I've referenced all bullet projects to my project.
I've even staticly linked to the lib files from bullet (and the linker can apparently see these files, I even tried giving the absolute path)
As far as I can tell the functions exist and have the same signature in the .cpp and .h files
If you need more details let me know.
(One lead I possibly got is something about different bt_double_precision? But that might not be it and the only think I could find was floating point model using different precision. But making that the same didn't do anything)
Thanks in advance!
EDIT:
I don't think this answer applies to me
What is an undefined reference/unresolved external symbol error and how do I fix it?
EDIT:
Maybe worth mentioning: The file structure of the project is really weird (dictated by premake):
solution and project files are in: bullet3-2.89\build3\vs2010
high level header files(these are the ones used by Hello_World that include other headers): bullet3-2.89\src
source and header files are subdirectories of bullet3-2.89\src
libraries: bullet3-2.89\bin
but all headers use the correct relative paths in their include and I have set the project's include and library directories to those locations
I found a workaround. Just gonna hijack one of the example projects from Bullet and use that instead. They seem to be better at VS settings than me :D
Anyway, thanks for your suggestions.
Had the same issue where just a few of the functions didn't link.
This is most likely due to the Bullet library being built with BT_USE_DOUBLE_PRECISION, meaning all btScaler will be of type double.
But the library is included without BT_USE_DOUBLE_PRECISION, meaning the program will try to link with the float versions of the functions.
But they don't exists since the built version uses doubles.

VC++ Problems using gumbo-query (linking error)

I've got a VC++ project, and I'm attempting to use the extension of the gumbo-query library, found here. This library wraps/extends Google's gumbo-parser found here. The following is the exact steps I've taken - I'm not very familiar with importing libraries, so I've done what I do to use the Boost libraries:
In Visual Studio (VS Community 2013), under the project settings -> Configuration Properties -> C/C++ -> General I have put the path to a folder that contains all of the source files from both of the projects linked above. Specifically, I placed the .c, .cpp and .h files from the src folder of both projects and referenced these in my projects settings as an include directory.
Following the example file for the project that extends Google's gumbo-parser (found here), I added these two lines to import the library:
#include "Document.h"
#include "Node.h"
At this point, my solution compiles fine. However, continuing to follow the example file, adding the first variable declaration:
CDocument d;
Causes a linker error, as follows:
1>Main.obj : error LNK2028: unresolved token (0A0003B7) "public: __thiscall CDocument::CDocument(void)" (??0CDocument##$$FQAE#XZ) referenced in function "private: void __clrcall MyApplication::Main::worker_DoWork(class System::Object ^,class System::ComponentModel::DoWorkEventArgs ^)" (?worker_DoWork#Main#MyApplication##$$FA$AAMXP$AAVObject#System##P$AAVDoWorkEventArgs#ComponentModel#4##Z)
1>Main.obj : error LNK2028: unresolved token (0A0003B8) "public: virtual __thiscall CDocument::~CDocument(void)" (??1CDocument##$$FUAE#XZ) referenced in function "private: void __clrcall MyApplication::Main::worker_DoWork(class System::Object ^,class System::ComponentModel::DoWorkEventArgs ^)" (?worker_DoWork#Main#MyApplication##$$FA$AAMXP$AAVObject#System##P$AAVDoWorkEventArgs#ComponentModel#4##Z)
1>Main.obj : error LNK2019: unresolved external symbol "public: __thiscall CDocument::CDocument(void)" (??0CDocument##$$FQAE#XZ) referenced in function "private: void __clrcall MyApplication::Main::worker_DoWork(class System::Object ^,class System::ComponentModel::DoWorkEventArgs ^)" (?worker_DoWork#Main#MyApplication##$$FA$AAMXP$AAVObject#System##P$AAVDoWorkEventArgs#ComponentModel#4##Z)
1>Main.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall CDocument::~CDocument(void)" (??1CDocument##$$FUAE#XZ) referenced in function "private: void __clrcall MyApplication::Main::worker_DoWork(class System::Object ^,class System::ComponentModel::DoWorkEventArgs ^)" (?worker_DoWork#Main#MyApplication##$$FA$AAMXP$AAVObject#System##P$AAVDoWorkEventArgs#ComponentModel#4##Z)
1>..{omitted}..\MyApplication.exe : fatal error LNK1120: 4 unresolved externals
This error appears to occur no matter where I put a CDocument instantiation.
What can I do to rectify this? VS seems to think the includes are fine, and moreso when I put in CDocument d; it lights up to show it recognises the CDocument type.
As always, it's clear I've been a JS developer for too long. I really did need to compile a .lib file. Thank you to Christian for reminding me of this.
The original gumbo-parser project by Google includes a VS project. I opened this and compiled it, fixing all of the project settings problems so that I could import it into my project, which I did via the Configuration Properties -> Linker -> Input -> Additional Dependencies setting for my VS project.
Next, I added the extra source files from the wrapper library gumbo-query, where I had to fix a naming conflict between parser.h/parser.cpp from the original gumbo-parser project and the Parser.h/Parser.cpp file in the wrapper library. I also changed all references to #include <gumbo.h> to #include "gumbo.h"
Eventually, I got a gumbo.lib file containing both the original and the wrapper libraries, and this imported into my project and I now seem to be able to successfully use the functions.

XDispatch C++ Unresolved Externals

I'm work with GCD in C++ with xdisptach, libdispatch in Visual Studio 2012 on Windows 7.
I am declaring a class with a global variable that is a dispatch queue. Other functions in the class call the queue's function. Everything compiles fine, except when i instantiate the queue in the constructor.
xdispatch::queue* dispatch_queue;
AsyncNode()
{
dispatch_queue = new xdispatch::queue(Name);
}
When dispatch_queue = new xdispatch::queue(Name); is commented out, it all compiles fine. Otherwise i get the following errors.
Error 50 error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall xdispatch::queue::~queue(void)" (__imp_??1queue#xdispatch##UAE#XZ) referenced in function "public: virtual void * __thiscall xdispatch::queue::`scalar deleting destructor'(unsigned int)" (??_Gqueue#xdispatch##UAEPAXI#Z)
Error 49 error LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall xdispatch::queue::queue(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (__imp_??0queue#xdispatch##QAE#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function "public: __thiscall AsyncNode::AsyncNode(void)" (??0AsyncNode##QAE#XZ)
Error 53 error LNK2001: unresolved external symbol "public: virtual void __thiscall xdispatch::object::resume(void)" (?resume#object#xdispatch##UAEXXZ)
Error 51 error LNK2001: unresolved external symbol "public: virtual void * __thiscall xdispatch::queue::native(void)const " (?native#queue#xdispatch##UBEPAXXZ)
Error 52 error LNK2001: unresolved external symbol "public: virtual struct dispatch_queue_s * __thiscall xdispatch::queue::native_queue(void)const " (?native_queue#queue#xdispatch##UBEPAUdispatch_queue_s##XZ)
This is the main site for xdispatch, but i can't find anything in terms of a forum or help with xdispatch in particular. There are many for objective-c.... :/
http://opensource.mlba-team.de/xdispatch/docs/current/index.html
First of all, the behavior regarding deleting the "problematic" line is normal - when you're defining a pointer you're not invoking any function, and that is the reason you are not getting the "unresolved external errors".
When you're initializing the variable by calling the constructor, you then run into problems because you're trying to call a function which is not available at link time.
When working with external libraries using DLLs, you must link to the appropriate import libraries, usually supplied as "lib" files. In your case, there is a folder named "lib" in the zip package. Also, the DLLs must availble at run-time - by putting them in the folder of the executable, or adding them to the PATH environment variable.
In order to link to a library, follow these steps (taken from MSDN):
Open the project's Property Pages dialog box. For details, see Setting Visual C++ Project Properties.
Click the Linker folder.
Click the Input property page.
Modify the Additional Dependencies property.

I have a third party lib, I have error LNK2019: unresolved external ... How to investigate to fix it

I have a third party libs. (msvc10) a MT/MD (Static cfgs's) and dynamic DLL cfg.
I have qt + msvc10 express + win sdk.7
Ok , I use the existing examples offered, (using the libs) I can't compile ..... I have 4 unresolved external errors of the same lib.
(But I have zero errors for the others)
I have not support for these lib...... (but they are legal, I am a member without rights)
Which are the steps to investigate a possible fix? Where I have to look ?
Thanks.
Edit 1:
The errors was:
TD_ExamplesCommon.lib(ExHostAppServices.obj) : error LNK2019: unresolved external symbol __imp__RegEnumValueW#32 referenced in function "public: virtual bool __thiscall ExHostAppServices::ttfFileNameByDescriptor(class OdTtfDescriptor const &,class OdString &)" (?ttfFileNameByDescriptor#ExHostAppServices##UAE_N ABVOdTtfDescriptor##AAVOdString###Z)
TD_ExamplesCommon.lib(ExHostAppServices.obj) : error LNK2019: unresolved external symbol __imp__RegCloseKey#4 referenced in function "public: virtual bool __thiscall ExHostAppServices::ttfFileNameByDescriptor(class OdTtfDescriptor const &,class OdString &)" (?ttfFileNameByDescriptor#ExHostAppServices##UAE_N ABVOdTtfDescriptor##AAVOdString###Z)
TD_ExamplesCommon.lib(ExHostAppServices.obj) : error LNK2019: unresolved external symbol __imp__RegQueryValueExW#24 referenced in function "public: virtual bool __thiscall ExHostAppServices::ttfFileNameByDescriptor(class OdTtfDescriptor const &,class OdString &)" (?ttfFileNameByDescriptor#ExHostAppServices##UAE_N ABVOdTtfDescriptor##AAVOdString###Z)
TD_ExamplesCommon.lib(ExHostAppServices.obj) : error LNK2019: unresolved external symbol __imp__RegOpenKeyExW#20 referenced in function "public: virtual bool __thiscall ExHostAppServices::ttfFileNameByDescriptor(class OdTtfDescriptor const &,class OdString &)" (?ttfFileNameByDescriptor#ExHostAppServices##UAE_N ABVOdTtfDescriptor##AAVOdString###Z)
..\exe\OdaQtApp.exe : fatal error LNK1120: 13 unresolved externals
During this post I have received a solution: I have to link with Advapi32.lib...
My question is : how can I know this ?
I have tried the dependencyywalker, but it cant use the .lib's....
During this post I have received a solution: I have to link with Advapi32.lib... My question is : how can I know this?
When you get an "unresolved external" error from the linker, that means that it was looking for a match for a function or variable name that some object file needs and the linker was unable to find that name defined in one of the object files or libraries.
Start by looking at the first of these errors (I've reformatted it a bit to make it slightly more readable - I encourage yo to do the same next time you come across one of these):
TD_ExamplesCommon.lib(ExHostAppServices.obj) : error LNK2019: unresolved external symbol
__imp__RegEnumValueW#32 referenced in function
"public: virtual bool __thiscall ExHostAppServices::ttfFileNameByDescriptor(
class OdTtfDescriptor const &,class OdString &)"
(?ttfFileNameByDescriptor#ExHostAppServices##UAE_N ABVOdTtfDescriptor##AAVOdString###Z)
There's a lot of stuff in that error message (much of it may look like garbage). Fortunately, much of it can be ignored in most cases. The most important item is that the linker is looking for the symbol __imp__RegEnumValueW#32 The name has some gunk on it, but fortunately it's pretty recognizable anyway.
the __imp__ prefix indicates it's looking for a DLL import. In nearly all cases that can be ignored for your purposes.
the #32 suffix is something the Microsoft compiler adds to function names for certain calling conventions. It's also generally not important for your purposes (for the record it indicates that the function expects 32 bytes of argument data)
So we're left with the fact that the linker is looking for RegEnumValueW. That looks a lot like the name of a Win32 API.
If you look at the docs for RegEnumValueW (or RegEnumValue, since many Win32 APIs have both an A and a W variant to handle ANSI/UNICODE builds) we'll find in the documentation this bit of information:
Requirements
Minimum supported client Windows 2000 Professional
Minimum supported server Windows 2000 Server
Header Winreg.h (include Windows.h)
>> Library Advapi32.lib
DLL Advapi32.dll
Unicode and ANSI names RegEnumValueW (Unicode) and
RegEnumValueA (ANSI)
That's how you know you need advapi32.lib.
So in the future, when you get an "unresolved external" error from the linker, just ignore most of the gunk in the error message and concentrate on the symbol it says it can't find - that should lead you to the library, object file or other item you might be missing.
Just for the record, advapi32.lib will be needed by most Windows applications of any complexity.
you can try to use dependencywalker to see the list of dependencies for your dlls and see what it's missing.
have you entered the *.lib file in the linker options? (input --> additional dependencies"), and in addition the path to the .lib in libraries directories option?

Error LNK 2019 unresolved external symbol from header file from my professor

So I'm pretty new to C++ programming and am taking a class on it but am having some trouble with an assignment because I am getting an error I can't seem to fix. The assignment is to create a composite class out of a number of classes using a string class the professor gave us. The problem I'm having is that I'm getting an unresolved external symbol error from the header file he gave us and I don't know why. I added the folder containing the header file and the associated cpp file using Properties -> C/C++ -> General -> Additional Include Directories and then used #include in the header file for my class but I'm getting 2 errors when I try to build it, the program compiles just fine. It's strange that I'm only getting 2 errors when there are many more functions in WCS_String that don't return an error.
Here are the error codes I'm getting:
1>Name.obj : error LNK2019: unresolved external symbol "public: virtual __thiscall WCS_String::~WCS_String(void)" (??1WCS_String##UAE#XZ) referenced in function __unwindfunclet$??0Name##QAE#XZ$0
1>Name.obj : error LNK2019: unresolved external symbol "private: void __thiscall WCS_String::LocalCopy(char const *)" (?LocalCopy#WCS_String##AAEXPBD#Z) referenced in function "public: __thiscall WCS_String::WCS_String(char const *)" (??0WCS_String##QAE#PBD#Z)
If anyone could help me it would be greatly appreciated as I have run into a dead end with my limited programming knowledge. If you need any more information in order to help me please just ask.
If I were you I would just copy all the professor files to the project directory, and add both header and source files to it.
If you have .o or .obj files associated with your WCS_String class, you need to tell your linker where to find them.