LNK2019 uresolved symbol - c++

I am struggling with
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl Book::Book(void)" (??0Book##QEAA#XZ) referenced in function main
main.obj:-1: error: LNK2019: unresolved external symbol "public: void __cdecl Book::update(class Order)" (?update#Book##QEAAXVOrder###Z) referenced in function main
From what I have read, it usually is because of missing selectors, but I can't see myself missing anything. Also, no static variables, parameters seem correct, ... going through the issue list below didn't help figure out the issue.
https://msdn.microsoft.com/en-us/library/799kze2z.aspx
#include <QCoreApplication>
#include <order.h>
#include <book.h>
#include <iostream>
#include <vector>
std::vector<Order> md_orders;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Order order;
order.setIsBid( true );
order.setSize( 500 );
order.setPrice( 10.00 );
double price = order.getPrice();
Book book;
book.update( order );
std::cout << price;
return a.exec();
}
book.h
#ifndef BOOK_H
#define BOOK_H
//#include <order.h>
#include <vector>
class Book
{
public:
Book();
Order getBest( double, bool );
void update( Order );
double getBestPrice( bool );
private:
std::vector<Order> _orders;
};
#endif // BOOK_H
book.cpp
#include "book.h"
#include <limits>
//#include <order.h>
#include <iostream>
int imax = std::numeric_limits<int>::max();
Book::Book()
{
}
void Book::update( Order order ) {
std::cout << order.getSize();
}
I am new to cpp. I seem to be doing the same with the book class as with the order class, where it works fine. This is very confusing. Any pointers?
So, out of curiosity, I created a new projected and added a class TestA and run it, all went fine. Then I created class TestB exactly the same way and it breaks with the same error for TestB, but not TestA
#include <QCoreApplication>
#include "testa.h"
#include "testb.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
TestA A;
TestB B;
return a.exec();
}
testa.h
#ifndef TESTA_H
#define TESTA_H
class TestA
{
public:
TestA();
};
#endif // TESTA_H
testb.h
#ifndef TESTB_H
#define TESTB_H
class TestB
{
public:
TestB();
};
#endif // TESTB_H
throws this
main.obj:-1: error: LNK2019: unresolved external symbol "public: __cdecl TestB::TestB(void)" (??0TestB##QEAA#XZ) referenced in function main
That is all on qt creator. It just seems very weird behaviour

In your main.cpp,
Notice how you have include book.h
Compiler will not look for book.h in local directory and couldn't find definition of book in standard lib location.
Instead of
#include <book.h> use #include"book.h"
Adding further.
You haven't defined below method in book.cpp and from main function you are trying to access them. Reason for all your pain.
Order getBest( double, bool );
double getBestPrice( bool );

You should do the following (provided you run compiler and linker manually):
Compile main.cpp
Compile book.cpp
Link both files into a single executable
All of above can be done in one command if your compiler allows it.
Since I'm not familiar with MSVC compiler, I'll show how I would do this with gcc:
gcc -c -o main.o main.cpp # Compiles main.cpp into main.o
gcc -c -o book.o book.cpp # Compiles book.cpp into book.o
gcc -o my_app main.o book.o # Creates the my_app executable from main.o and book.o
or
gcc -o my_app main.cpp book.cpp # Does all of above in one command

Related

Linker issue in Visual Studio C++

I'm trying to get started with C++ in VS 2017 (empty project template), but immediately ran into linker problems when adding 1 simple class, so I guess I'm missing something important...
My project looks like this:
test.h:
#include <iostream>
class test
{
public:
test();
~test();
std::string getInfo();
};
test.cpp:
#include "test.h"
test::test() {}
test::~test() {}
std::string getInfo() {
return "test";
}
And main.cpp:
#include <iostream>
#include <string>
#include "test.h"
int main(int argc, char **argv) {
test t;
std::cout << "output: " << t.getInfo() << std::endl;
return 0;
}
The linker error I get is the infamous LNK2019:
LNK2019 unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl test::getInfo(void)" (?getInfo#test##QEAA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##XZ) referenced in function main
Any ideas what I am doing wrong here?
Thanks!
In file test.cpp you need to properly specify the scope of the member function:
std::string test::getInfo() {
return "test";
}
Note the test:: before the getInfo()

How do I avoid `already defined` linking error with pugixml if two static libs contain pugixml objs?

So I have 2 static libs defined like this:
StaticLib1
// StaticLib1.h
#pragma once
class StaticLib1
{
public:
void doSomething1();
};
cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib1.h"
void StaticLib1::doSomething1()
{
pugi::xml_node node;
}
StaticLib2
// StaticLib2.h
#pragma once
class StaticLib2
{
public:
void doSomething2();
};
cpp:
// StaticLib1.cpp
#include "pugixml.hpp"
#include "StaticLib2.h"
void StaticLib2::doSomething2()
{
pugi::xml_node node;
}
Main
#include <iostream>
#include "StaticLib1.h"
#include "StaticLib2.h"
int main(int argv, char** argc)
{
StaticLib1 staticlib1;
StaticLib2 staticlib2;
staticlib1.doSemething1();
staticlib2.doSemething2();
getchar();
return 0;
}
Now, if I build this. I get a lot of linking errors. Here are the first few linking errors:
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(struct pugi::xml_attribute_struct *)" (??0xml_attribute#pugi##QAE#PAUxml_attribute_struct#1##Z) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "public: __thiscall pugi::xml_attribute::xml_attribute(void)" (??0xml_attribute#pugi##QAE#XZ) already defined in StaticLib1.lib(StaticLib1.obj)
3>StaticLib2.lib(StaticLib2.obj) : error LNK2005: "private: __thiscall pugi::xml_attribute_iterator::xml_attribute_iterator(struct pugi::xml_attribute_struct *,struct pugi::xml_node_struct *)" (??0xml_attribute_iterator#pugi##AAE#PAUxml_attribute_struct#1#PAUxml_node_struct#1##Z) already defined in StaticLib1.lib(StaticLib1.obj)
...
...
Now, I understand that this linking error is because there is a pugixml.obj inside StaticLib1.lib, and there is pugixml.obj inside StaticLib2.lib. But I don't understand why this would cause linking error with pugixml signatures. Why would they be defined twice? If I call staticlib1.doSomething1() shouldn't main not care if there are multiple definitions of pugi? Shouldn't staticlib1.doSomething1() handle all of that?
on the pugiconfig.hpp I have these specific settings:
#ifndef HEADER_PUGICONFIG_HPP
#define HEADER_PUGICONFIG_HPP
#define PUGIXML_WCHAR_MODE
#define PUGIXML_HEAD_ONLY
#include "pugixml.cpp"
#endif
So yes, from user0042advice, I realize it is better to compile a pugixml.lib on your own rather than having #include "pugixml.cpp" on the config. I'm working with legacy code so these surprises are there. Now, I've fixed my issue and made my company code slightly cleaner.

main.obj : error LNK2019: unresolved external symbol public: __cdecl

I'm trying to write a simple class in C++ with Qt5. I can't figure out this error comes from :
main.obj : error LNK2019: unresolved external symbol "public: __cdecl ItemModel::ItemModel(class std::basic_string,class std::allocator >)" (??0ItemModel##QEAA#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) referenced in function main
debug\AMWS.exe : fatal error LNK1120: 1 unresolved externals
Test.pro
QT += core
QT -= gui
CONFIG += c++11
TARGET = Test
CONFIG += console
CONFIG -= app_bundle
TEMPLATE = app
HEADERS += \
main.h \
itemmodel.h
SOURCES += \
main.cpp \
itemmodel.cpp
itemmodel.h
#ifndef ITEMMODEL_H
#define ITEMMODEL_H
#include <string>
class ItemModel
{
public:
ItemModel(std::string sku);
protected:
std::string SKU;
};
#endif // ITEMMODEL_H
itemmodel.cpp
#include "itemmodel.h"
using namespace std;
ItemModel::ItemModel(string sku) : SKU(sku)
{
}
main.h
#ifndef MAIN_H
#define MAIN_H
#include <QCoreApplication>
#include <iostream>
#include "itemmodel.h"
#endif // MAIN_H
main.cpp
#include "main.h"
#include <string>
using namespace std;
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
ItemModel product("dummy");
cout << "Great!!!" << endl;
return a.exec();
}
Try to delete the complete build folder (Makefiles included) and recompile.
Sometime Qt apps have weird linker errors due to makefiles not being updated by qmake when they should.

LNK2019 on a solution with a dll project

I am trying to create a solution which one project is the .exe and the other project is a simple dll. What i am trying to learn is how to link between two projects. I have searched stack-overflow and found really nice answers which I have followed, such as declaring the right header bath on:
Properties->Configuration Properties->C/C++->General->Additional Include Directories
Then setting the .lib on:
Properties->Configuration Properties->Linker->Input->Additional Dependencies
I used macros to generate that .lib file also. Here is the my simplified code:
The .exe:
cpp:
#include "stdafx.h"
#include "../ConsoleApplication2/HelloWorld.h"
int _tmain(int argc, _TCHAR* argv[])
{
hello_world hw;
hw.printHello();
getchar();
return 0;
}
The dll:
header:
#pragma once
#ifdef is_hello_world_dll
#define hello_world_exp __declspec(dllexport)
#else
#define hello_world_exp __declspec(dllimport)
#endif
class hello_world_exp hello_world
{
public:
hello_world();
~hello_world();
void printHello();
};
cpp:
#include "stdafx.h"
#include "HelloWorld.h"
#include <iostream>
hello_world::hello_world()
{
}
hello_world::~hello_world()
{
}
void printHello()
{
std::cout << "Hello World" << std::endl;
}
A note: The solution compiles fine when I don't call hw.printHello(); however when I do call it, the linker generates :
Error 1 error LNK2019: unresolved external symbol "__declspec(dllimport) public: void __thiscall hello_world::printHello(void)" (__imp_?printHello#hello_world##QAEXXZ) referenced in function _wmain C:\Users\usteinfeld\Desktop\Private\Students\Yana\ConsoleApplication1\ConsoleApplication1\ConsoleApplication1.obj ConsoleApplication1
This function is defined as a free function based on how you wrote it
void printHello()
It belongs to the class hello_world so you should scope it as such
void hello_world::printHello()
{
std::cout << "Hello World" << std::endl;
}

Unresolved external symbol with custom blocking queue implementation

I have created my own blocking queue and I'm having some trouble figuring out why I get a linker error (note this is a Qt app in Visual Studio 2010):
#ifndef BLOCKING_QUEUE_H
#define BLOCKING_QUEUE_H
#include <QObject>
#include <QSharedPointer>
#include <QWaitCondition>
#include <QMutex>
#include <queue>
namespace TestingNS
{
template<typename Data>
class BlockingQueue
{
private:
std::queue<QSharedPointer<Data>> _queue;
QMutex _mutex;
QWaitCondition _monitor;
volatile bool _closed;
public:
BlockingQueue();
void Close();
size_t Size();
void Empty();
bool IsClosed();
bool Enqueue(QSharedPointer<Data> data);
bool TryDequeue(QSharedPointer<Data>& value, unsigned long time = ULONG_MAX);
};
}
#endif //BLOCKING_QUEUE_H
The implementation is a bit longer, so I have a pastie for it: http://pastie.org/5368660
The program entry point looks like this:
#include <QtCore/QCoreApplication>
#include <QTimer>
#include <iostream>
#include "BlockingQueue.h"
using namespace std;
using namespace TestingNS;
class Item
{
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
BlockingQueue<Item> queue;
cout << "Press any key to exit!" << endl;
char in;
cin.get(in);
QTimer::singleShot(0, &a, SLOT(quit()));
return a.exec();
}
The linker error I get is:
Error 1 error LNK2019: unresolved external symbol "public: __thiscall TestingNS::BlockingQueue<class Item>::BlockingQueue<class Item>(void)" (??0?$BlockingQueue#VItem###TestingNS##QAE#XZ) referenced in function _main
I don't understand why the linker can't find the constructor (nor any other method from BlockingQueue). Any ideas?
It's template, you have to put the implementation inside BlockingQueue.h
For a while, the standard did
provide the keyword export to allow such a separate implementation file. But not many
vendors implemented it. C++11 discontinues that use of export but reserves the export
keyword for possible future use.)
Templates have to be used in conjunction with requests for particular
instantiations of templates.