error LNK2019: unresolved external symbol "public: [duplicate] - c++

This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
error LNK2019 unresolved external symbol
(1 answer)
Closed 9 years ago.
Relearning C++ for the sake of using OpenCL. I have created a Helper Class called CheckDevice which has a bunch of boiler plate code for getting device stats.
CheckDevice.h
class Utils
{
public:
Utils(){};
~Utils(){};
template<class T>
static bool IsNull(T Object, char* name);
private:
};
CheckDevice.cpp
cl_command_queue Utils::CreateCommandQueue(cl_context context, cl_device_id *device)
{
cl_int err;
cl_device_id *devices;
cl_command_queue queue = NULL;
size_t deviceBufferSize = -1;
cl_kernel kernel = 0;
Utils::IsNull<cl_command_queue>(queue, "Utils::CreateCommandQueue::queue");
return queue;
}
main.cpp
void main()
{
cl_kernel kernel = 0;
Utils::IsNull<cl_kernel>(kernel, "clCreateKernel");
}
The question is when calling the function Utils::IsNull from within CheckDevice.cpp it works fine but when calling from main.cpp than I get the following in Visual Studios 2012
error LNK2019: unresolved external symbol "public: static bool __cdecl Utils::IsNull(struct _cl_kernel *,char *)" (??$IsNull#PAU_cl_kernel###Utils##SA_NPAU_cl_kernel##PAD#Z) referenced in function _main
1>C:\Users\Suri\Documents\Visual Studio 2012\Projects\HelloWorld\Debug\HelloWorld.exe : fatal error LNK1120: 1 unresolved externals
Any Help would be apperciated

You don't show the implementation of the IsNull template, but probably it is in CheckDevice.cpp. It should be placed in CheckDevice.h instead, the compiler needs to see it's definition to be able to instantiate it.

There is a calling convention problem on the main.cpp. When you are trying to make a function call on the main, you need first to create an instance of that class object, before you make a function call. You are missing that on your main.
So basically you need to create an instance of Utils and make a reference to it.
cl_kernel kernel = 0;
Utils *ut = new Utils();
ut->IsNull"<"cl_kernel">"(kernel, "clCreateKernel");

Related

Adding htmlhelp to project

I'm trying, without much luck, to add chm options to an existing project.
Main.cpp has this:
#include "HtmlHelp.h"
int OpenHelp(LPTSTR arg1)
{
HWND _Hhdl = HtmlHelp(GetDesktopWindow(), arg1, HH_DISPLAY_TOPIC, NULL);
return 1;
}
htmlhelp.lib is set in Linker/Input/additional Dependencies.
The error I'm getting is:
main.obj : error LNK2019: unresolved external symbol _HtmlHelpW#16 referenced in function "int __cdecl OpenHelp(wchar_t *)" (?OpenHelp##YAHPA_W#Z)
I'm new to C++ so I'm assuming my function is at fault somehow.
The idea was the function would be passed a sting in the "C:\Help.chm\::/Topic.html" format.

Linker error: "LNK2019: Unresolved external symbol" [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
error LNK2019: unresolved external symbol
(2 answers)
Closed 9 years ago.
I am getting linker errors and can not make out the source of the problem. I hope you can help me! The error occurs already when creating an object of the class ClMyClass.
The two errors:
Error 18 error LNK2019: unresolved external symbol "public: virtual __thiscall ClMyClass::~ClMyClass(void)" (??1ClMyClass##UAE#XZ) referenced in function "void __cdecl Test1(void)" (?Test1##YAXXZ)
Error 19 error LNK2019: unresolved external symbol "public: __thiscall ClMyClass::ClMyClass(void)" (??0ClMyClass##QAE#XZ) referenced in function "void __cdecl Test1(void)" (?Test1##YAXXZ)
//Test.cpp
#include "StdAfx.h"
#include "MyClass.hpp"
void Test1(){
ClMyClass oHandle;
}
//ClMyClass.hpp
class ClMyClass: public ClMyClassBase
{
public:
ClMyClass(void);
virtual ~ClMyClass(void);
//methods...
}
//ClMyClass.cpp
ClMyClass::ClMyClass(void):ClMyClassBase(),m_nCallbackError(ERROR_NOT_INITIALIZED), m_eState(eUnknown)
{
//initialize members
}
ClMyClass::~ClMyClass(void)
{
Unassign();
}
EDIT: Added .cpp file
You haven't defined the constructor and destructor that you declared in your class ClMyClass.
ClMyClass::ClMyClass(){
//implementation
}
ClMyClass::~ClMyClass(){
//implementation
}
That's usually done in a separate .cpp file that contains all the class' member functions' implementations.
In your implementation you have not implement the virtual functions. If you use virtual functions, then you must implement all functions which are virtual, even if you don't use them. Otherwise the linker will have the unresolved symbol error you are seeing.
If a function is not used in a class, then will not get an error, because there is no reference to this function. Thus the linker will not see it and doesn't complain. For a virtual function this is different, because they are always generated.

Why am I getting this error ? "unresolved external symbol" [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 9 years ago.
This is the error I have been getting the whole time and I've been trying to figure out how to fix it but have failed. I am asking if anyone can point me to the right direction.
WorldServer fatal error LNK1120: 2 unresolved externals
WorldServer error LNK2019: unresolved external symbol "public: class CItemElem * __thiscall CLinkedItemMgr::GetLinkedItem(unsigned long)" (?GetLinkedItem#CLinkedItemMgr##QAEPAVCItemElem##K#Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem#CDPSrvr##AAEXAAVCAr##KKPAEK#Z)
WorldServer error LNK2019: unresolved external symbol "public: int __thiscall CLinkedItemMgr::AddLinkedItem(class CItemElem *)" (?AddLinkedItem#CLinkedItemMgr##QAEHPAVCItemElem###Z) referenced in function "private: void __thiscall CDPSrvr::OnLinkedItem(class CAr &,unsigned long,unsigned long,unsigned char *,unsigned long)" (?OnLinkedItem#CDPSrvr##AAEXAAVCAr##KKPAEK#Z)
This is the .h
#ifndef __ITEM_LINK__H
#define __ITEM_LINK__H
class CLinkedItemMgr
{
private:
CLinkedItemMgr(){ m_dwLinkedItemCount = 0;};
~CLinkedItemMgr(){};
DWORD m_dwLinkedItemCount;
public:
map<DWORD,CItemElem*> m_mapLinkedItems;
static CLinkedItemMgr *GetInstance()
{
static CLinkedItemMgr instance;
return &instance;
}
int AddLinkedItem(CItemElem *pItem);
CItemElem *GetLinkedItem(DWORD dwIndex);
};
#endif
this is the .cpp
#include "stdafx.h"
#include "ItemLink.h"
int CLinkedItemMgr::AddLinkedItem(CItemElem *pItem)
{
if(!pItem)
return 0;
m_mapLinkedItems.insert(make_pair<DWORD,CItemElem*>(++m_dwLinkedItemCount,pItem));
return m_dwLinkedItemCount;
}
CItemElem *CLinkedItemMgr::GetLinkedItem(DWORD dwIndex)
{
map<DWORD,CItemElem*>::iterator it = m_mapLinkedItems.find(dwIndex);
if(it == m_mapLinkedItems.end())
return FALSE;
return it->second;
}
Your problem is in the cpp here.
#ifdef __ITEM_LINK
#include "ItemLink.h"
#ifdef __ITEM_LINK means "only process the code below if __ITEM_LINK is defined"
And in your case, it is not defined. It only gets defined when "ItemLink.h" is included, and "ItemLink.h" only gets included if it's already defined. You've prevented either from happening first.
Remove the #ifdef line.
It looks like a linking problem.
The compiler knows your class has a function called GetLinkedItem but can't find any definition of that function anywhere. Are you linking properly when compiling your executable?
I bet stopping the compiler before linking doesn't trigger any error.
(e.g. g++ -c ItemLink.cpp).
i'm going to ask the help of someone who's accustomed to visual studio to elaborate more :D
anyway, compiling requires three major steps:
1) applying preprocessor directives, parsing the source code, looking for syntax errors and the like
2) creating an object file from source code (something half-way between source code and executable)
3) linking all the object files making up your project in one executable
your compiling chain fails at the third step.
the compiler expects a certain function to be defined in some .cpp (that has become an object file at step 2 of compiling chain) but can't find it anywhere.
and it can't find it because of that #ifdef in the .cpp file, which tells the preprocessor NOT TO INCLUDE your definitions, since __ITEM_LINK is not defined
i see you changed the .cpp in your question by the way

Unresolved externals using C++ lambdas [duplicate]

This question already has answers here:
Why do I get "unresolved external symbol" errors when using templates? [duplicate]
(3 answers)
Why can templates only be implemented in the header file?
(17 answers)
Closed 9 years ago.
I am getting unresolved externals compile error with following code snippet.
acquire_gray(identity, []{});
samething happens when I try to use auto
auto acquire_callback = [](LPBITMAPINFOHEADER pbi, HANDLE hdib)
{
printf("Callback\n");
};
acquire_gray("", acquire_callback );
but when I pass in null it compiles
acquire_gray(identity, NULL);
This is structure of my program
driver.cpp
#include "bridge.h"
void TB_AcquireImagesStart(HANDLE hNamedPipe, const TB_Message request)
{
acquire_gray("", []{});
}
bridge.h
template<typename T>
void acquire_gray(const string_t& identity, T& callback);
bridge.cpp
template<typename T>
void acquire_gray(const string_t& identity, T& callback)
{
callback();
}
So the two exceptions that I am getting are
Error 12 error LNK1120: 1 unresolved externals
Error 11 error LNK2019: unresolved external symbol "void __cdecl acquire_gray<class <lambda_e125ff607fe0339bba6077ce9c14d586> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class <lambda_e125ff607fe0339bba6077ce9c14d586> &)" (??$acquire_gray#V<lambda_e125ff607fe0339bba6077ce9c14d586>####YAXABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##AAV<lambda_e125ff607fe0339bba6077ce9c14d586>###Z) referenced in function "void __cdecl TB_AcquireImagesStart(void *,class TB_Message)" (?TB_AcquireImagesStart##YAXPAXVTB_Message###Z)
So my question is what is wrong with my code, and how can I fix this, and why is auto not detecting my lambda type.
You can't put template definitions in .cpp (well you can, but it only makes sense if you're using them in that compilation unit). After compilation, only template instanciations exist.
This is just a common pitfall when using templates. You cannot (or at least should not) separate a template into header (.hpp) and source (.cpp) files. See here for details.

lnk2019 visual studio error

I am trying to make a simple UDP socket class to do some talking between a c++ program and a java program so I'm trying to make a socket class that handles all the UDP transmission but I can't get the program to compile because I get about 8 lnk2019 errors and I have no idea what they even mean. I mainly work in Java and only use c++ when I have to lol. I have 3 files the socket header and code. Also the Udp Socket code is from rFactor-Nesim so the socket code isn't written by me.
UdpSocket.cpp
#include "UdpSocket.hpp"
#include <stdio.h>
UdpSocket::UdpSocket(const char* host, int port)
: mHost(host), mPort(port)
{
}
UdpSocket::~UdpSocket(void)
{
}
void UdpSocket::Open()
{
if(WSAStartup(MAKEWORD(2, 0), &mWinsockData) != 0)
fprintf(stderr, "WSAStartup() failed");
if ((mSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
perror("socket() failed");
memset(&mSocketAddress, 0, sizeof(mSocketAddress));
mSocketAddress.sin_family = AF_INET;
mSocketAddress.sin_addr.s_addr = inet_addr(mHost);
mSocketAddress.sin_port = htons(mPort);
}
void UdpSocket::Close()
{
closesocket(mSocket);
WSACleanup();
}
void UdpSocket::Send(char* str, size_t length)
{
size_t result = sendto(mSocket, str, length, 0,
(struct sockaddr *) &mSocketAddress, sizeof(mSocketAddress));
if(result != length)
perror("sendto() sent incorrect number of bytes");
}
UdpSocket.hpp
#ifndef UDPSOCKET_HPP
#define UDPSOCKET_HPP
#include <WinSock.h>
class UdpSocket
{
public:
UdpSocket(const char* host, int port);
~UdpSocket(void);
void Send(char* str, size_t length);
void Open();
void Close();
private:
const char* mHost;
int mPort;
int mSocket;
struct sockaddr_in mSocketAddress;
WSADATA mWinsockData;
};
#endif // UDPSOCKET_HPP
and the main
#include "Socket/UdpSocket.hpp"
#include <iostream>
int Main(){
UdpSocket* testSocket = new UdpSocket("127.0.0.1", 27469);
testSocket->Open();
system("pause");
return 0;
}
Any help would be great. I'm not very strong with c++ but I have done a little bit
Console Ouput:
Error 1 error LNK2019: unresolved external symbol __imp__htons#4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 2 error LNK2019: unresolved external symbol __imp__inet_addr#4 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 3 error LNK2019: unresolved external symbol __imp__socket#12 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 4 error LNK2019: unresolved external symbol __imp__WSAStartup#8 referenced in function "public: void __thiscall UdpSocket::Open(void)" (?Open#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 5 error LNK2019: unresolved external symbol __imp__WSACleanup#0 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 6 error LNK2019: unresolved external symbol __imp__closesocket#4 referenced in function "public: void __thiscall UdpSocket::Close(void)" (?Close#UdpSocket##QAEXXZ) UdpSocket.obj SocketTest
Error 7 error LNK2019: unresolved external symbol __imp__sendto#24 referenced in function "public: void __thiscall UdpSocket::Send(char *,unsigned int)" (?Send#UdpSocket##QAEXPADI#Z) UdpSocket.obj SocketTest
Error 8 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup MSVCRTD.lib SocketTest
Error 9 fatal error LNK1120: 8 unresolved externals C:\Users\Brendan\Documents\Visual Studio 2008\Projects\SocketTest\Debug\SocketTest.exe SocketTest
It sounds like you're failing to link against Winsock - Ws2_32.lib
If you're building from the command line, add Ws2_32.lib to your link command line.
If you're building from Visual Studio, look for linker flags/settings inside a project configuration dialog.
when you have code in several implementation files, you need to compile all those implementation files and pass the resulting object code files to the linker, which combines them (and other stuff) into an executable
it's not enough to include the header of a module
c++ doesn't (yet) have any technical module concept, so including headers does not magically compile implementation files or pass object code files to the linker
this is not part of the c++ standard, but it's part of everyday tool usage
the linker is telling you that you failed to provide it with object code for your class member functions the Winsock library
that object code is provided by the library file, in visual c++ typically with filename extension ".lib"
in general, when you get a mysterious error, just look up the error number in the documentation
in visual studio that's as simple as pressing the F1 key