static library linkage failure in visual c - c++

In visual c++, I created a static library with two files, myLib.h and myLib.cpp. I also have a console application project with the file testSequence.cpp that references this library.
Within myLib.h I have defined a class template<class prec> class sequence which has the function declaration prec *getPrimes(int numToGet) this function is then defined in myLib.cpp. However, when I build testSequence, there is a linking error, and it says error LNK2019: unresolved external symbol "public: int * __thiscall mathLib::sequence<int>::getPrimes(int)" (?getPrimes#?$sequence#H#mathLib##QAEPAHH#Z) referenced in function "char * __cdecl codeString(char *,char *,bool)" (?codeString##YAPADPAD0_N#Z)
So, yeah, help would be nice.

Read this for an explanation of the error.
Basically, what you're trying to do cannot be done. The compiler must be able to see the implementation of the class template when it tries to instantiate it for a given template type parameter. You need to move the implementations for all the member functions to the header file.

Related

Avoid use of dll referenced from lib

I'm writing CPP unit test and the code of one used lib file references a procedure in dll.
I would like to remove this lib and the dll from the project.
I have made replacement methods for methods from the lib file, but there is one method which is imported from dll:
error LNK2019: unresolved external symbol "__declspec(dllimport) class CX * __cdecl CreateX(wchar_t const *)" (__imp_?....) referenced in function "...."
Is there a way how to declare a local function to link this with my code instead of dll import?
Just create a stub implementation of the missing file in your project. This will remove the linker error.

constructor overloading Class get Linker Error Using DLL?

I have created Regular DLLs Dynamically Linked to MFC.
in before build dll in dll project i used add another class. this class provide more method of constructor overloading.
Then I build dll successfully after i used this lib and dll file include project and then go to build and get linker error when using constructor overloading class
My Class Name: Object
Error 11 error LNK2001: unresolved external symbol "public: __thiscall AvinashiAMF::Object::~Object(void)" (??1Object#AvinashiAMF##QAE#XZ) BuleCappServiceUseDynamicDllDlg.obj
Error 10 error LNK2001: unresolved external symbol "public: __thiscall AvinashiAMF::Object::Object(enum AvinashiAMF::ObjectType,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Object#AvinashiAMF##QAE#W4ObjectType#1#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) BuleCappServiceUseDynamicDllDlg.obj
Error 12 fatal error LNK1120: 2 unresolved externals D:\Test Aplications\Visual Studio 2008\Projects\BuleCappServiceUseDynamicDll\Release\BuleCappServiceUseDynamicDll.exe
You're probably not exporting your class. See here http://msdn.microsoft.com/en-us/library/81h27t8c.aspx
Also, a nice tool to check for this kind of issues is depends.exe, included with Visual Studio, which allows you to check for exported and imported symbols.
There are two significantly different dynamic link libraries: implicitly linked and explicitly linked.
In short, implicitly linked is linked at the compile time. You need an import library to link with(.lib), header file with functions prototypes and properly usage of the dllexport and dllexport or use .def file.
dllexport/dllexport are easier to use in this case. System takes care of loading libraries (DLLs).
Explicit linking is a runtime linking. You do not need any import library or headers. You need to know what is the function signature. You have to explicitly call LoadLibrary and GetProcAddress to call the function.
To see how to properly create macros for export/import create empty Win32 project select DLL and make sure the Export symbols check box is checked.
In the main header file you will see explanation how to use macros for import/export.

Unresolved external symbol due to VC9 compiler symbol name mismatch

I'm seeing the following error message when attempting to link
a library in one project against another in the same solution:
CPTemplate.obj : error LNK2019: unresolved external symbol "public: long __thiscall MPADOFieldList::GetField(wchar_t *,struct Field * *)" (?GetField#MPADOFieldList##QAEJPA_WPAPAUField###Z) referenced in function "public: virtual long __stdcall CCPTemplate::GetRootStorage(struct IMPRootStore * *)" (?GetRootStorage#CCPTemplate##UAGJPAPAUIMPRootStore###Z)
Using 'dumpbin /symbols' on the static library that I'm linking
against reveals a different symbol for the 'GetField' method:
?GetField#MPADOFieldList##QAEJPA_WPAPAUADOField###Z (public: long __thiscall MPADOFieldList::GetField(wchar_t *,struct ADOField * *))
Clearly the difference is 'Field' vs. 'ADOField'. 'Field' is defined
in a referenced header :
typedef interface ADOField Field;
The declaration of the 'GetField' method is as follows:
HRESULT GetField( BSTR bstrFieldName, Field** rpField );
This is almost certainly down to one of two things, eitherthe typedef being conditional, and for the lib it takes one branch while in the main project it takes the other. However, as the type resolves Field in the main project I have a more plausible theory. The header that contains the GetField declaration has a forward declaration for Field, the typedef however is not seen in this TU at all so it's assumed to be a type which will be defined elsewhere ( causing the first link ). In the library however the typedef is seen and correctly resolved to ADOField, causing the mismatch. The solution is to make sure that the typedef is seen in the TU that contains the definition for CCPTemplate::GetRootStorage.

Linkage Error with Inherited Class

I have static library and another program which uses it.
In the static library If I define header without inheretence it works fine.
class TcpCommunication
On the other hand If I use inheretence with a QT class,
class TcpCommunication:public QTcpServer
I'm getting linkage error when I compiling code which uses this static library.
>MStoDKAPId.lib(TcpCommunication.obj) : error LNK2019: unresolved external symbol "__declspec(dllimport) public: virtual __thiscall QTcpServer::~QTcpServer(void)" (__imp_??1QTcpServer##UAE#XZ) referenced in function "public: virtual __thiscall TcpCommunication::~TcpCommunication(void)" (??1TcpCommunication##UAE#XZ)
What can be the problem?
Thanks.
You need to add a reference to the library that contains the definition of the QTcpServer class.
Your IDE should have an option to specify link options, one of which will be the names of the libraries and another to specify search paths for libraries. Update these two to add the path / library of the QT framework.
The application using your static library also needs to link to QT

Linker error 'unresolved external symbol' : working with templates

I have a template based class [Allotter.h & Allotter.cpp]:
template <typename allotType> class Allotter {
public:
Allotter();
quint32 getAllotment(allotType*);
bool removeAllotment(quint32, int auto_destruct = 0);
private:
QVector<QPair<quint32, allotType*>> indexReg;
int init_topIndex;
};
and it's usage is shown as [ActiveListener.h & ActiveListener.cpp]:
class ActiveListener: public QObject {
Q_OBJECT
public:
ActiveListener();
private slots:
void processConnections();
void readFromSocket(int);
private:
QTcpServer* rootServer;
QSignalMapper* signalGate;
Allotter<QTcpSocket> TcpAllotter;
};
I am not showing the complete definitions, since it doesn't really matter. The problem is when I compile, all files compile properly. The files are in a VC++ project. Earlier when I did not use a template-based approach for Allotter, everything was compiling and linking fine. But now, I get this error:
1>ActiveListener.obj : error LNK2019: unresolved external symbol "public: __thiscall Allotter<class QTcpSocket>::Allotter<class QTcpSocket>(void)" (??0?$Allotter#VQTcpSocket####QAE#XZ) referenced in function "public: __thiscall ActiveListener::ActiveListener(void)" (??0ActiveListener##QAE#XZ)
1>ActiveListener.obj : error LNK2019: unresolved external symbol "public: unsigned int __thiscall Allotter<class QTcpSocket>::getAllotment(class QTcpSocket *)" (?getAllotment#?$Allotter#VQTcpSocket####QAEIPAVQTcpSocket###Z) referenced in function "private: void __thiscall ActiveListener::processConnections(void)" (?processConnections#ActiveListener##AAEXXZ)
The surprising thing is, that the constructor, ActiveListener::ActiveListener() does not make any reference at all Allotter<QTcpSocket>::Allotter(). The second reference however does exist. But I don't understand why the linker isn't able to resolve this external symbol.
The build output just before the errors appear is:
1>Moc'ing ActiveListener.h...
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>ActiveListener.cpp
1>Allotter.cpp
1>moc_ActiveListener.cpp
1>main.cpp
1>Generating Code...
1>Linking...
I don't understand if any of this is relevant, mostly because all this used to work perfectly before. It's just that after I use templates a problem is caused.
Any help will be appreciated. Thanks a lot.
You cannot split templates into .h and .cpp files - you need to put the complete code for the template in the .h file.
Generally speaking it is considered best practice to write your template code entirely inside header files. There is an important technical reason for this: when you instantiate a template, the C++ compiler needs to generate code from that template that is specific to the template parameters that you have specified. If your template code is placed entirely in your headers, this is done for you automatically.
It is definitely possible to write template code the way that you have, with the implementation placed in cpp files. If you do this, however, you are required to explicitly instantiate the template instance that you intend to use.
In your case, you need to add the following line to a .cpp file in your project:
template class Allotter<QTcpSocket>;
Since you can't place template implementation in .cpp files, it is considered a good practice to use .inl files for the template implementation and include them from the template headers.