LNK2005 (already defined) - c++

I know you've seen 21728517 people asking for help with this one but after searching and reading about this I really can't figure this one out. I know this error, I've seen it before, however, this time, I can't seem to get around it.
I've also tried this checklist.
So, the errors:
Error 25 error LNK2005: "void __cdecl checkStatus(unsigned int &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,bool)" (?checkStatus##YAXAAIV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##_N#Z) already defined in DollarRecognizer.obj C:\Users\Rui Teixeira\Desktop\Current\Tese\SVN\TIFEE_Empty\TIFEE_Empty\main.obj TIFEE_Empty
Error 26 error LNK2005: "void __cdecl depth2rgb(unsigned short const *,unsigned short *,char *,int,int)" (?depth2rgb##YAXPBGPAGPADHH#Z) already defined in DollarRecognizer.obj C:\Users\Rui Teixeira\Desktop\Current\Tese\SVN\TIFEE_Empty\TIFEE_Empty\main.obj TIFEE_Empty
Error 27 error LNK2005: "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > __cdecl explode(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,char)" (?explode##YA?AV?$vector#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##std##V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##2#D#Z) already defined in DollarRecognizer.obj C:\Users\Rui Teixeira\Desktop\Current\Tese\SVN\TIFEE_Empty\TIFEE_Empty\main.obj TIFEE_Empty
So, the thing is, these are functions defined in "misc.h" with the proper #ifndef #define #endif. They are not defined elsewhere but I still get the LNK2005 of death. What am I doing wrong?
Thanks in advance.

Define the functions in misc.cpp, rather than in misc.h.
The problem is probably due to #includeing misc.h in multiple CPP files. the header guards prevent a header from being included multiple times in the same translation unit, but each CPP file is (typically) a separate translation unit. So those functions end up getting defined twice -- once in each translation unit.

Related

Unresolved external symbol x - using boost tests from one visual studio project testing code of another project in the same solution

I have a visual studio solution that contains two projects. They are both saved in a separate folder. The main project (in my case EquitCalculatorMontecarlo) contains a main function that can work by itself. The second project contains boost Tests that are supposed to test the main project.
My issue is that when I try to run the tests with the test explorer I get an error message from the linker:
Error LNK2019 unresolved external symbol "bool __cdecl eval_best_hand(class std::vector<class std::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > >,class std::allocator<class std::set<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,struct std::less<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > > > > > const &)" (?eval_best_hand##YA_NAEBV?$vector#V?$set#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##U?$less#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2#V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##std##V?$allocator#V?$set#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##U?$less#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2#V?$allocator#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###2##std###2##std###Z) referenced in function "public: void __cdecl card_evaluation1::test_method(void)" (?test_method#card_evaluation1##QEAAXXZ) Tests C:\Users\dickr\git\EquityCalculatorMontecarlo\Tests\Test.obj 1
I followed the instructions here: https://learn.microsoft.com/en-us/visualstudio/test/how-to-use-boost-test-for-cpp?view=vs-2019
The full code is visible here:
https://github.com/dickreuter/PokerEquityCalculator
but here a quick summary:
/Tests/Test.cpp
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE HandEvaluationTests
#include <boost/test/included/unit_test.hpp>
#include "../EquityCalculatorMontecarlo/Deck.h"
#include "../EquityCalculatorMontecarlo/Scoring.h"
BOOST_AUTO_TEST_CASE(card_evaluation1)
.....
/EquityCalculatorMontecarlo/Scoring.cpp
various function definitions
/EquityCalculatorMontecarlo/Scoring.h
using CardsWithTableCombined = std::set<std::string>;
using Score = std::vector<std::tuple<int, int>>;
bool eval_best_hand(const std::vector<CardsWithTableCombined>&);
std::tuple< std::vector<int>, std::vector<int>, std::string> calc_score(const CardsWithTableCombined&);
template<typename T>
std::vector<T> slice(std::vector<T> const& v, int m, int n)
{
if (m > v.size())
.....
/EquityCalculatorMontecarlo/Deck.h
various function declarations
/EquityCalculatorMontecarlo/Deck.cpp
various function definitions
What could be the problem that the test project cannot access my main EquityCalculatorMontecarlo project? I have also tried to select it as a dependency but nothing seems to help. I cannot run the tests.
Any suggestions are highly appreciated.
Definitions of eval_best_hand and its dependencies are present only in EquityCalculatorMontecarlo project that is an executable, you are just referring the function declaration of eval_best_hand from header files in your Tests project but not linking them. Either you make the definitions of whatever functions designated to be used in other projects (Executables) into a common static lib (Say Utils.lib), and link your projects against that static library or include all the .cpp files that contains definitions of necessary functions to Tests.vcxproj.
I think, simply adding Scoring.cpp and Deck.cpp to Tests.vcxproj will solve the linker errors.
You may also create a separate project namely "Utils.lib" with only Scoring.cpp and Deck.cpp source files and link that lib to whatever executable project that needs to consume those utility methods. This is basically to meet the purpose of reusing and easy maintenance. In this case your main project (EquityCalculatorMontecarlo) may only contain a main function and code to consume functions in Scoring.h and Deck.h(Definitions present in Utils.lib).

Crypto++ library link error using Visual Studio 2017

I'm trying to use the Crypto++ librairy in my project (windows application). Using it, include, compilation work fine, but impossible to deal with the link error
Here is some exemple of link errors, there is more, but don't think it's revelant to copy paste all of them
error LNK2019: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::encrypt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?encrypt#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23##Z) referenced in function "public: void __thiscall PStore::storeReversibleCrypt(wchar_t *,char *)" (?storeReversibleCrypt#PStore##QAEXPA_WPAD#Z)
error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::hashPassword(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?hashPassword#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23#0#Z)
fatal error LNK1120: 4 unresolved externals
Basically, I add the "Win32\Output\Release" directory of crypto++ to my linker's additionnal library directories properties, and also the main folder to my C/C++'s General's property "Additional Include Directories"
I've tried a lot of thinks, like adding the library as a new project (same errors), adding all cpp files to my project and compiling with it (not compiling), adding only .cpp files I was using (not realistic, too much), linking all different folder of the cryptopp610 releases (cryptdll, cryptlib, dll_output, Output, same errors), and now, I don't really know what more I can try. I makre also a lots of search, trying all solution I saw (don't remember all of them), still the same problems. I also try to create a new project to add crypto++ without long compilation or mysterious problem, but I also get linker error.
Do anyone got any advice to help me ? Anyway, thank's a lot, and pardon my English
EDIT: Need to add that on the new blank project, I get 63 unresolved external symbol, so I think I forgot to do some basic stuff, but can't figure out which
error LNK2019: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::encrypt(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?encrypt#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23##Z) referenced in function "public: void __thiscall PStore::storeReversibleCrypt(wchar_t *,char *)" (?storeReversibleCrypt#PStore##QAEXPA_WPAD#Z)
error LNK2001: unresolved external symbol "public: static class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl CryptoV2::hashPassword(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (?hashPassword#CryptoV2##SA?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##V23#0#Z)
The missing symbols are not from Crypto++. Crypto++ uses the CryptoPP namespace. The missing symbols are from the CryptoV2 namespace or class. I'm guessing that's another crypto library.
You were right in adding directories and a library to the linker settings. However, you need to do it for the CryptoV2 library (in addition to the Crypto++ library).
For completeness, it looks like these are missing:
std::string CryptoV2::encrypt(std::string);
std::string CryptoV2::hashPassword(std::string, std::string);
It could be Ws2_32.lib there is missing from the linker
add #include "dll.h" like stated in the Readme:
To use the Crypto++ DLL in your application, #include "dll.h" before including any other Crypto++ header files, and place the DLL in the same directory as your .exe file. dll.h includes the line #pragma comment(lib, "cryptopp") so you don't have to explicitly list the import library in your project settings.
https://github.com/weidai11/cryptopp/blob/master/Readme.txt#L136

PCL and OSG library conflict

So I am using PCL for point cloud stuff and OpenSceneGraph for visualization. PCL visualization is pretty bad so I made my own. Anyways whenever I try to use this statement
pcl::io::savePCDFileBinary<pcl::PointXYZRGBA>(fname2,*cloud);
things break and I get the following errors
osgDBd.lib(osg80-osgDBd.dll) : error LNK2005: "public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::close(void)" (?close#?$basic_ofstream#DU?$char_traits#D#std###std##QAEXXZ) already defined in Recorder.obj
2>osgDBd.lib(osg80-osgDBd.dll) : error LNK2005: "public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::`vbase destructor'(void)" (??_D?$basic_ofstream#DU?$char_traits#D#std###std##QAEXXZ) already defined in Recorder.obj
2>osgDBd.lib(osg80-osgDBd.dll) : error LNK2005: "public: bool __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::is_open(void)const " (?is_open#?$basic_ofstream#DU?$char_traits#D#std###std##QBE_NXZ) already defined in Recorder.obj
2>osgDBd.lib(osg80-osgDBd.dll) : error LNK2005: "public: void __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::open(char const *,int,int)" (?open#?$basic_ofstream#DU?$char_traits#D#std###std##QAEXPBDHH#Z) already defined in Recorder.obj
2>osgDBd.lib(osg80-osgDBd.dll) : error LNK2005: "public: __thiscall std::basic_ofstream<char,struct std::char_traits<char> >::basic_ofstream<char,struct std::char_traits<char> >(void)" (??0?$basic_ofstream#DU?$char_traits#D#std###std##QAE#XZ) already defined in Recorder.obj
2>E:\Google Drive\Research\PCL\build\HandTracker\Debug\HandTracker.exe : fatal error LNK1169: one or more multiply defined symbols found
I know that IO files in OSG and PCL are conflicting particularly ostream headers. My question is how do I fix this? I need to use this statement for intermediary files saves, logging, etc. so not using it is out of the question unless I want to use my own method which is kinda like reinventing the wheel.
Thanks for any help
http://forum.openscenegraph.org/viewtopic.php?t=8099
There are several solutions outlined there.

Strange linker error in cpp

When I add the following line to my code:
std::string sFrameTag
I get the following linker error:
Error 34 error LNK2005: "public: __thiscall std::basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct
std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string#DU?
$char_traits#D#std##V?$allocator#D#2##std##QAE#XZ) already defined in
VFPAnalyzerApi.lib(VFPEvaluation.obj) msvcprtd.lib
I'm sure sFrameTag is only defined once, I tried using other names for this variable to be sure. I have the following includes: stdio.h, time.h, string.
Can someone please guide it as to what causes this error?
I too get similar kind of errors when I tried to statically include all needed runtime libraries. Do check that when you are trying to import a library which is linked statically with /MD option you also need to use /MD.
Thanks
Niraj Rathi

Multiple problems linking mysqlpp_d.lib

Today I added a class which manages the connection to a MySQL Server. It will be multi-threaded, so I want to use mysql++. I downloaded the newest version and compiled it in debug mode without any errors. Once I added the compiled mysqlpp_d.lib to my solutions, and of course the other requirements too (mysql 5.0 include and lib), I got some linker errors.
Error 17 error LNK1169: one or more multiply defined symbols found C:\Users\root\Documents\Visual Studio 2010\Projects\C++\xxxx\binaries\xxxx.exe 1 1 xxxxx
Error 16 error LNK2005: "public: __thiscall std::basic_ostringstream<char,struct std::char_traits<char>,class std::allocator<char> >::basic_ostringstream<char,struct std::char_traits<char>,class std::allocator<char> >(int)" (??0?$basic_ostringstream#DU?$char_traits#D#std##V?$allocator#D#2##std##QAE#H#Z) already defined in xxxxxx.obj C:\Users\root\Documents\Visual Studio 2010\Projects\C++\xxxx\trunk\vc10\xxxxx\mysqlpp_d.lib(mysqlpp_d.dll) xxxxxxx
Error 15 error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __thiscall std::basic_ostringstream<char,struct std::char_traits<char>,class std::allocator<char> >::str(void)const " (?str#?$basic_ostringstream#DU?$char_traits#D#std##V?$allocator#D#2##std##QBE?AV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##2#XZ) already defined in client_context.obj C:\Users\root\Documents\Visual Studio 2010\Projects\C++\xxx\trunk\vc10\xxx\mysqlpp_d.lib(mysqlpp_d.dll) xxxxx
Error 14 error LNK2005: "public: void __thiscall std::basic_ostringstream<char,struct std::char_traits<char>,class std::allocator<char> >::`vbase destructor'(void)" (??_D?$basic_ostringstream#DU?$char_traits#D#std##V?$allocator#D#2##std##QAEXXZ) already defined in xxxxx.obj C:\Users\root\Documents\Visual Studio 2010\Projects\C++\xxx\trunk\vc10\xxxx\mysqlpp_d.lib(mysqlpp_d.dll) xxx
Is there a solution for these errors?
BTW: I compiled the whole mysql++ libary without changing any compile configurations.
Define MYSQLPP_NO_DLL in Project Properties->C/C++/Preprocessor
Try setting /NODEFAULTLIB parameter in project options -> Linker/Input