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

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
I get this error, but I don't know how to fix it.
I'm using Visual Studio 2013.
code
-----DateUtils.h
#pragma once
#include <string>
class DateUtils
{
public:
DateUtils();
~DateUtils();
static time_t str2time_t(const std::string&, const std::string&);
};
-----ForexUtils32.h
#include <string>
// The following ifdef block is the standard way of creating macros which make exporting
// from a DLL simpler. All files within this DLL are compiled with the FOREXUTILS32_EXPORTS
// symbol defined on the command line. This symbol should not be defined on any project
// that uses this DLL. This way any other project whose source files include this file see
// FOREXUTILS32_API functions as being imported from a DLL, whereas this DLL sees symbols
// defined with this macro as being exported.
#ifdef FOREXUTILS32_EXPORTS
#define FOREXUTILS32_API __declspec(dllexport)
#else
#define FOREXUTILS32_API __declspec(dllimport)
#endif
// This class is exported from the ForexUtils32.dll
class FOREXUTILS32_API CForexUtils32 {
public:
CForexUtils32(void);
// TODO: add your methods here.
};
extern FOREXUTILS32_API int nForexUtils32;
FOREXUTILS32_API int fnForexUtils32(void);
/******************** Add Begin *************************/
FOREXUTILS32_API time_t str2time(const std::string&, const std::string&);
/******************** Add End *************************/
-------DateUtils.cpp
#include "stdafx.h"
#include "DateUtils.h"
#include <sstream>
#include <iomanip>
using namespace std;
DateUtils::DateUtils()
{
}
DateUtils::~DateUtils()
{
}
time_t str2time_t(const string& datetimeIn, const string& formatIn) {
struct tm tm_time;
// For C++11
istringstream iss(datetimeIn);
iss >> get_time(&tm_time, formatIn.c_str());
time_t time = mktime(&tm_time);
return time;
}
------ForexUtils32.cpp
// ForexUtils32.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "ForexUtils32.h"
#include "DateUtils.h"
// This is an example of an exported variable
FOREXUTILS32_API int nForexUtils32=0;
// This is an example of an exported function.
FOREXUTILS32_API int fnForexUtils32(void)
{
return 42;
}
// This is the constructor of a class that has been exported.
// see ForexUtils32.h for the class definition
CForexUtils32::CForexUtils32()
{
return;
}
/******************** Add Begin *************************/
FOREXUTILS32_API time_t str2time(const std::string& datetime, const std::string& format)
{
time_t t = DateUtils::str2time_t(datetime, format);
return t;
}
/******************** Add End *************************/
Error message:
Error 1 error LNK2019: unresolved external symbol "public: static
__int64 __cdecl DateUtils::str2time_t(class std::basic_string,class
std::allocator > const &,class std::basic_string,class std::allocator > const &)"
(?str2time_t#DateUtils##SA_JABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z) referenced in function "__int64 __cdecl str2time(class
std::basic_string,class
std::allocator > const &,class std::basic_string,class std::allocator > const &)"
(?str2time##YA_JABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##0#Z) D:\visual
studio
2013\Projects\32bit\ForexUtils32\ForexUtils32\ForexUtils32.obj ForexUtils32
How can i fix this error ?Help me Please(I'm not good at english very much. Thanks)

The first line of the function definition
time_t str2time_t(const string& datetimeIn, const string& formatIn) {
have to be
time_t DateUtils::str2time_t(const string& datetimeIn, const string& formatIn) {
(Add the class name to which the member function belongs)

Related

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.

C++ | 3x Linker Error LNK2028, 2019, 1120 | Constructor

I am getting 3 linker errors when i launch my windows desktop c++ clr project. I am using the Event Stream Client API by Mobotix (http://developer.mobotix.com/).
Errors:
error LNK2028: not listed token (0A000429) ""public: virtual __thiscall ie::MxPEG::MxPEG_SinkVideo::~MxPEG_SinkVideo(void)" (??1MxPEG_SinkVideo#MxPEG#ie##$$FUAE#XZ)", wich is linked in function ""public: __thiscall SinkVideo::SinkVideo(char const *)" (??0SinkVideo##$$FQAE#PBD#Z)". C:\Users\Hecom\Documents\Visual Studio 2012\Projects\HKS\HKS\SinkVideo.obj HKS
error LNK2019: link to not listed external symbol ""public: virtual __thiscall ie::MxPEG::MxPEG_SinkVideo::~MxPEG_SinkVideo(void)" (??1MxPEG_SinkVideo#MxPEG#ie##$$FUAE#XZ)" in function ""public: __thiscall SinkVideo::SinkVideo(char const *)" (??0SinkVideo##$$FQAE#PBD#Z)". C:\Users\Hecom\Documents\Visual Studio 2012\Projects\HKS\HKS\SinkVideo.obj HKS
error LNK1120: 2 not listed externs C:\Users\Hecom\Documents\Visual Studio 2012\Projects\HKS\Debug\HKS.exe HKS
... i translated this error codes into english because i`m running the german version of Visual Studio.
When i comment out the following constructor in my SinkVideo.cpp, there are no more errors:
SinkVideo::SinkVideo (const char *outFile) {
}
as you see, the constructor is empty. so, why causes this errors?
could this be an error in the API?
Header-File (SinkVideo.h):
#include <Windows.h>
#include "MxPEG_SinkVideo.hpp"
#include "MxPEG_Image.hpp"
#include "MxPEG_Defines.hpp"
using namespace ie::MxPEG;
class SinkVideo : public MxPEG_SinkVideo {
public:
SinkVideo (const char *outFile);
protected:
virtual
MxPEG_ReturnCode doConsumeVideo(MxPEG_Image *buffer);
private:
FILE * fVideoOut;
};
Source-File (SinkVideo.cpp):
#include "SinkVideo.h"
#include <assert.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
SinkVideo::SinkVideo (const char *outFile) {
// the constructor which causes the errors
}
MxPEG_ReturnCode SinkVideo::doConsumeVideo(MxPEG_Image *buffer) {
return er_Success;
}
Here is the code of the MxPEG_SinkVideo.hpp header file in the API:
#ifndef MXPEG_SRC_MXPEG_SINK_HPP_
#define MXPEG_SRC_MXPEG_SINK_HPP_
#include "MxPEG_Image.hpp"
namespace ie { namespace MxPEG
{
/*
* Interface of the Vido Sink.
*
* Pass an instance of this to the constructor when creating an instance of MxPEG_SDK(). The SDK will pass all decoded video frames to the doConsumeVideo()
* function of that instance.
*
*/
class MxPEG_SinkVideo
{
protected:
public:
virtual
~MxPEG_SinkVideo();
MxPEG_ReturnCode consumeVideo(MxPEG_Image *buffer);
protected:
friend class MxPEG_SinkSynchronized;
/*
* Receives each video frame right after it was decoded. Either YUV or RGB is possible (see MxPEG_Image.hpp and MxPEG_SDK_API.hpp)
*
* doConsumeVideo needs to take care of deleting the buffer once it is no longer needed.
*/
virtual
MxPEG_ReturnCode doConsumeVideo(MxPEG_Image *buffer) = 0;
};
} } /* namespace ie::MxPEG */
#endif /* MXPEG_SRC_MXPEG_SINK_HPP_ */

Unresolved external symbol referenced in function error

I've looked at other people's solutions to this problem and none of them, unfortunately, have solved my issue. I was having this error in my dev project, so I started from scratch, followed this tutorial to the letter, including the file names and locations, and I am still getting
1>unittest1.obj : error LNK2019: unresolved external symbol "public:
static int __cdecl HelloWorld::getTwo(void)"
(?getTwo#HelloWorld##SAHXZ) referenced in function "public: void
__thiscall UnitTest1::UnitTest1::TestMethodGetTwo(void)" (?TestMethodGetTwo#UnitTest1#1#QAEXXZ)
1>unittest1.obj : error
LNK2019: unresolved external symbol "public: int __thiscall
HelloWorld::getN(void)const " (?getN#HelloWorld##QBEHXZ) referenced in
function "public: void __thiscall
UnitTest1::UnitTest1::TestMethodGetN(void)"
(?TestMethodGetN#UnitTest1#1#QAEXXZ)
The code is in the tutorial I linked but I will copy it below. I don't understand what I could be doing wrong at this point - my test project depends on my build project, my definitions for these functions are in the class.
HelloWorld.h
#pragma once
class HelloWorld
{
int n = 10;
public:
static int getTwo();
int getN() const;
};
HelloWorld.cpp
#include "stdafx.h"
#include "HelloWorld.h"
int main()
{
return 0;
}
int HelloWorld::getTwo()
{
return 2;
}
int HelloWorld::getN() const
{
return n;
}
unittest1.cpp (in test project)
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../HelloWorld/HelloWorld.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestMethodGetTwo)
{
Assert::AreEqual(2, HelloWorld::getTwo());
}
TEST_METHOD(TestMethodGetN)
{
HelloWorld hw = HelloWorld();
Assert::AreNotEqual(0, hw.getN());
}
};
}
For some reason the linker can't find my definitions, and I'm out of ideas about why that might be.

Linker unresolved external symbol for inline function

I'm developing a C++ solution with Visual Studio 2015.
I have a cpp source file and header file hpp with this declaration.
Header:
#ifndef MyLib__FREEFUNCTIONS__INCLUDE__
#define MyLib__FREEFUNCTIONS__INCLUDE__
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;
// Check if 'str' is null, empty or consists only of white-space characters.
inline bool IsNullOrWhiteSpace(string str);
// More functions
[ ... ]
#endif
And Source code:
#include "FreeFunctions.h"
inline bool IsNullOrWhiteSpace(string str)
{
return (str.empty() || (str.find_first_not_of(' ') == string::npos));
}
I use this function in a class:
#include "ConvertToOwnFormat.h"
#include "FreeFunctions.h"
ConvertToOwnFormat::ConvertToOwnFormat()
{
}
ConvertToOwnFormat::~ConvertToOwnFormat()
{
}
vector<Entry> ConvertToOwnFormat::ReadCatalogue(string path)
{
if (!IsNullOrWhiteSpace(path)
{
[ ... ]
}
}
And I get the following error in ConvertToOwnFormat::ReadCatalogue:
Error LNK2019 external symbol "bool __cdecl IsNullOrWhiteSpace(class
std::basic_string,class
std::allocator >)"
(?IsNullOrWhiteSpace##YA_NV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
unresolved referenced by the function "public: class std::vector > __cdecl
ConvertToOwnFormat::ReadCatalogue(class std::basic_string,class std::allocator >)"
(?ReadCatalogue#ConvertToOwnFormat##QEAA?AV?$vector#VEntry##V?$allocator#VEntry###std###std##V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##3##Z) MyProjectLib D:\Fuentes\Repos\MyProject\MyProjectLibConsoleTest\ConsoleMyProjectLib\Lib.lib(ConvertToOwnFormat.obj) 1
You have to put declaration of methods within the header. inline tells to compiler that it should replace call of function with the core of function. So, it need it at compilation time for every unit of compilation which use it
#ifndef MyLib__FREEFUNCTIONS__INCLUDE__
#define MyLib__FREEFUNCTIONS__INCLUDE__
#include <iostream>
#include <vector>
#include <string>
#include <sstream>
// Check if 'str' is null, empty or consists only of white-space characters.
inline bool IsNullOrWhiteSpace(std::string str)
{
return (str.empty() || (str.find_first_not_of(' ') == std::string::npos));
}
// Others functions, prototype, ...
#endif
or remove inline in both source and header files
It's totally out of topic but never put a using namespace inside a header: a header should offer something but should not impose something like namespace. See also "using namespace" in c++ headers

error LNK2005: quest_tree::enter_one(class quest_tree::quest_node * &,class std::basic_string<char,struct std::char_traits<char>

I've seen many posts on LNK2005 error, but decided to ask my own anyway.
Here is the error code:
1>setup_quest_tree.obj : error LNK2005: "private: void __thiscall quest_tree::enter_one(class quest_tree::quest_node * &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?enter_one#quest_tree##AAEXAAPAVquest_node#1#ABV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) already defined in mainFunction.obj
1>setup_quest_tree.obj : error LNK2005: "void __cdecl setup_quest_tree(void)" (?setup_quest_tree##YAXXZ) already defined in mainFunction.obj
1>C:\Users\Timothy\Documents\Visual Studio 2008\Projects\ttbag\Debug\TTBAG.exe : fatal error LNK1169: one or more multiply defined symbols found
I'm trying to get the program to compile but am running into linker errors while doing so, probably because I've included quest_tree.h twice, but when I got rid of one of the declarations of quest_tree.h in setup_quest_tree.cpp I run into this error:
1>c:\users\timothy\documents\visual studio 2008\projects\ttbag\ttbag\setup_quest_tree.cpp(8) : error C2065: 'quest_tree' : undeclared identifier
There are many files so I am only including the ones for my project that are related to the error.
setup_quest_tree.cpp:
#ifndef SETUP_QUEST_NODES_CPP
#define SETUP_QUEST_NODES_CPP
#include <string>
#include "quest_tree.h"
void setup_quest_tree() {
quest_tree quest_tree_obj; //start out with two quest nodes
std::string welcome_message = "debug-welcome message";
quest_tree_obj.enter(welcome_message);
}
#endif
setup_quest_tree.h:
#ifndef SETUP_QUEST_TREE_H
#define SETUP_QUEST_TREE_H
#include "quest_tree.h"
#include "setup_quest_tree.cpp"
//function declarations
void setup_quest_tree (quest_tree &quest_tree_obj);
#endif /* SETUP_QUEST_TREE_H */
mainFunction.cpp (just the include statements):
#define DEBUG_LINES_ON
#include <iostream>
#include <fstream>
#include <time.h>
#include "weather.h"
#include "item.h"
#include "map.h"
#include "person.h"
#include "location.h"
#include "bag.h"
#include "equipped_items.h"
#include "global_vars.h"
#include "setup_quest_tree.h"
int main() { ...
quest_tree.h:
#ifndef QUEST_TREE_H
#define QUEST_TREE_H
#include <string>
#include <cstdlib>
class quest_tree {
private:
// the basic node of the tree. Do way to read from file?
class quest_node {
private:
quest_node *quest_nodes; // pointer to array of quests that activate upon quest activation
public:
//std::string word; // we will replace this with our own data variable
std::string note_to_player; //note that is shown upon quest activation
/*
quest_node(){ // default constructor
note_to_player = "";
}
*/
quest_node(short int num_nodes = 2){
quest_nodes = new quest_node[num_nodes]; // problem: not declared in quest_tree but rather in quest_node
note_to_player = "";
}
friend class quest_tree;
};
// the top of the tree
quest_node * root;
// Enter a new node into the tree or sub-tree
void enter_one(quest_node *&node, const std::string& note_to_player);
public:
quest_tree() {root = NULL;} // constructor
// Add a new note_to_player to our tree
void enter(std::string& note_to_player) {
enter_one(root, note_to_player);
}
};
void quest_tree::enter_one(quest_node *&new_node, const std::string& note_to_player)
{
// see if we have reached the end
if (new_node == NULL) {
new_node = new quest_node;
for (short int index = 0; index < (sizeof(new_node->quest_nodes)/sizeof(new_node->quest_nodes[0])); index++) { // initialize quest_nodes
new_node->quest_nodes[index] = NULL;
}
new_node->note_to_player = note_to_player;
}
if (new_node->note_to_player == note_to_player)
return;
/*
if (new_node->note_to_player < note_to_player)
enter_one(new_node->right, word);
else
enter_one(new_node->left, word)
*/
}
#endif /* QUEST_TREE_H */
You have included the implementation in the setup_quest_tree.h header file
#include "setup_quest_tree.cpp"
and included it in several translation units.
To fix this, at least in setup_quest_tree.cpp just include the declarations from setup_quest_tree.h, and remove that #include "setup_quest_tree.cpp" statement from setup_quest_tree.h should fix your linker errors.
You have to provide exclusively one definition (implementation) for your class (see also this answer for "Is is a good practice to put the definition of C++ classes into the header file?").
If you put it there, just since you don't know how to add the setup_quest_tree.cpp to your program, check this Q&A please to learn more about the linking process.
Here's the relevant section from the current c++ standard
3.2 One definition rule [basic.def.odr]
1 No translation unit shall contain more than one definition of any variable, function, class type, enumeration
type, or template.