LNK2019 unresolved external symbol after moving functions to another header file - c++

I had two array templates in one of my header files, everything worked well. Then I thought, I'd better get some more pedantism into my code, so I moved them all into another header file, mostly just for them (and for another function, that wants to use one of them). And then I got LNK2019 error every time I used functions from these templates in other header files.
Since everything was good before I pasted my code elsewhere, I assume the code is okay, it's just my lack of understanding. Basically, the question is: why do I get a linker error when I moved my function to another header file?
Here is an example of one of my errors:
Error LNK2019 unresolved external symbol "public: char __thiscall C2DArray::Get(int,int)" (?Get#?$C2DArray#D##QAEDHH#Z) referenced in function "public: char __thiscall SGame::GetRecordOutput(int,int)" (?GetRecordOutput#SGame##QAEDHH#Z) Mastermind C:\Users\Master\Documents\Visual Studio 2015\Projects\Mastermind\Mastermind\Menu.obj 1
I'm using Visual Studio 2015 if it matters.

Thank you for the reply. As it seems, I have resolved my problem. The cause was the class templats, which were not specialized (and had to be). All I had to do is include what types of these templates I wanted. Sorry for posting before doing more extensive research.

Related

CppSQLite throwing "LNK2019: unresolved external symbol" error

I'm trying to use CppSQLite wrapper for SQLite. I downloaded and compiled SQLite here: C:\sqlite\sqlite-autoconf-3310100. Then I downloaded CppSQLite to: C:\sqlite\cppsqlite. I then added these folders to include and library directories in MSVS project. Added the sqlite3.lib file to additional dependencies. As far as I'm aware that's all I have to do. I added #include "CppSQLite3.h" line. I created declared a CppSQLite3DB variable and MSVS says no problem, it know it comes from cppsqlite header. But when I run the project it throws LNK2019 error. Error code is below.
I'm aware of this question that is the same problem as mine but I tried all the answers there except the best answer. None of them worked. And I don't understand what to do in the best answer, I can't comment on it since I don't have enough rep, so I had to create a new question. Can anyone explain to me if that answer will work for me aswell? And if so, what am I supposed to do, can you explain me that answer simply? Thanks in advance!
LNK2019 unresolved external symbol "public: __cdecl CppSQLite3DB::CppSQLite3DB(void)"
(??0CppSQLite3DB##QEAA#XZ) referenced in function "bool __cdecl dbop::connectToDatabaseSQLITE(char
const *)" (?connectToDatabaseSQLITE#dbop##YA_NPEBD#Z)
https://github.com/neosmart/CppSQLite/
Get CppSQLite3.h and CppSQLite3.cpp files from here.
Add to your project.
You dont need to make CppSQLite3 seperate lib.
Adding files to project should be enouh

Confusion about dllexport/dllimprt

I've build an incredibly simple container (barely more than a struct) as a simple test of templates and libraries in c++. It holds an arbitrary type of data and keeps track of it's highest valued member and whether the data has changed since it was last accessed, and is contained in tiny .dll generated by the newest msvc compiler. All I want to do is link this library to another project in Qt Creator, which I've configured to use msvc as it's compiler ( I need to use Qt to interact with .ui files efficiently). I get an unresolved external symbol for each function in the object that is called, e.g. :
LNK2019: unresolved external symbol "__declspec(dllimport) public: __thiscall collectionContainer<int>::collectionContainer<int>(int *,short,long)" (__imp_??0?$collectionContainer#H##QAE#PAHFJ#Z) referenced in function _main
I'm not sure what to do here, as I was under the impression that that is the exact symbols I should expect to see when exporting a library.
The functions all have CONTAINER_API defined in the usual way that visual studio conditionally imports/exports functions. I'm sure I've just misinterpreted something, but I'm running out of ideas.
I wonder if this error has nothing to do with DLLs and it's just a case of not putting the template implementation in a header file. See here

Linking error: LNK2019 & LNK1120

First I had some OpenCL code and when I put it away in another function, I got this error, then I thought maybe its something with OpenCL, but now even my every normal code suddenly appears to report this error whenever I make new function and put main function code in those functions. I have rechecked that prototypes, functions calls and function definitions are exactly same and matches, also since I have this problem with those functions in which there are more arguments like upto 8 or 10 but I read that C/C++ functions supports more 50 or so arguments passing. The error information is:
ImgConv.obj : error LNK2019: unresolved external symbol "void __cdecl function(.............) (?conv_ser##YAXPBMPAM10HHH#Z) referenced in function _main
fatal error LNK1120: unresolved externals
I am reporting error that has lot of forum entries and solution information such as linking some library function or functions prototyping mistakes etc, I have read them but there is non related to my problem.
Check the linker input settings in your project configuration and make sure that the appropriate libraries are included (or add them to the project itself). Visual Studio doesn't do this automatically just because you add the source code to your project.
If 'conv_ser' is a function that you created make sure that the implementation for that function is available in the project. Double check to make sure that the arguments in the prototype (typically in the header) match that of the any implementation you have added.
Have you tried deleting all instances of void __cdecl function? Just search through your project and comment it out. See if your code will compile after this.

MSVS2010 linker error sadness - not entirely sure what is wrong

I am using a library of code from a tutorial for providing functionality for passing function points of non-static member functions to a function that expects a static function pointer, probably helps to know what I am suing, so here is the link http://www.codeproject.com/KB/cpp/thunk32.aspx This code uses the Boost library, which I have downloaded and set-up more or less everything from.
In the Thunk library, one of the header files has a section that reads
#define BOOST_PP_ITERATION_PARAMS_1 (3,(0,THUNK32_MAX_ARGS,"Thunk32_template.h"))
??=include BOOST_PP_ITERATE()
#undef BOOST_PP_ITERATION_PARAMS_1
but this gives me epic amounts of errors, which I can solve by changing it to
#define BOOST_PP_ITERATION_PARAMS_1 (3,(0,THUNK32_MAX_ARGS,"Thunk32_template.h"))
#include BOOST_PP_ITERATE()
#undef BOOST_PP_ITERATION_PARAMS_1
This code that is downloaded is included in my solution as a second project, which is able to compile and build happily. But my project that is using this code has issues linking, to save people asking, I get these error messages
1>WebCamera.obj : error LNK2019: unresolved external symbol "protected: __thiscall indev::Thunk32Base::Thunk32Base(void)" (??0Thunk32Base#indev##IAE#XZ) referenced in function "public: __thiscall indev::Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>::Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>(void)" (??0?$Thunk32#VWebCamera##$$A6AXPAUHWND__##PAUvideohdr_tag###Z#indev##QAE#XZ)
1>WebCamera.obj : error LNK2019: unresolved external symbol "protected: __thiscall indev::Thunk32Base::~Thunk32Base(void)" (??1Thunk32Base#indev##IAE#XZ) referenced in function "public: __thiscall indev::Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>::~Thunk32<class WebCamera,void __cdecl(struct HWND__ *,struct videohdr_tag *)>(void)" (??1?$Thunk32#VWebCamera##$$A6AXPAUHWND__##PAUvideohdr_tag###Z#indev##QAE#XZ)
1>WebCamera.obj : error LNK2019: unresolved external symbol _capCreateCaptureWindowA#32 referenced in function "public: bool __thiscall WebCamera::Init(struct HWND__ *)" (?Init#WebCamera##QAE_NPAUHWND__###Z)
I think this is trying to say that the constructor and destructor are not declared and that my WebCamera.Init()is messed up as well. I have made sure that the library that the Thunk32 project exports is included in my other project, but still I get these errors.
I would like to know if I have made the correct assumption that ??=include should have been changed to #include and if I have, what have I done wrong or failed to do that results in these linker errors. Or if you can provide me with a different way of being able to pass a function pointer to a non-static member function that would be awesome.
Thanks
??= is a "trigraph" sequence for the # character. according to the standard, trigraphs are supposed to be handled as one of the first steps in processing (in phase 1 - before the preprocessor handles directives),so:
??=include "whatever"
Should be equivalent to:
#include "whatever"
so you should be able to use that form (I wonder why the trigraph was put there in the first place - some sort of evil joke perhaps?)
However, trigraphs cause problems and confusion (probably more than they help), so compilers seem to be moving towards warning about them and/or defaulting to not handling them. The compiler in VS 2010 has trigraph processing turned off by default - you have to use the /Zc:trigraphs option to turn it on.
See Purpose of Trigraph sequences in C++? for more details.
Ah, Einar, good man. Doing flash and Sharepoint stuff these days, ouch. Norwegian, might explain the trigraphs.
Anyhoo, nothing complicated, you just forgot to tell the linker to look at some libraries. Right-click your project, Project Dependencies, tick the Thunk project. That makes sure that Thunk32.lib gets looked at and resolves the ctor and dtor.
Right-click again, Properties, Linker, Additional dependencies, add "winmm.lib". That resolves the capCreateCaptureWindow symbol.
Do you have a constructor and a destructor declared in the indev::Thunk32Base class that you forgot to define in its cpp file?
Ok, so I have managed to solve this now.
Michael Burr nicley said that ??= is basically the same as typing # but in a way that people who dont have the hash symbol can type it, see Purpose of Trigraph sequences in C++?
Hans Passant then got the ball rolling for me buy letting me know that I had not fully linked in stuff. I needed to right click on my main project, select 'Project Dependencies' and select my other project that has the thunk32 code. I also needed to tell my main project to look at where the Thunk project is saving the lib, which turned out to be in a folder in my documents (explain that one!). I also needed to add the Thunk32d.lib (note the 'd' because I was/am in debug mode. Hans said that I needed winmm.lib but it turned out (when googling the function that was giving me the error that I needed Vfw32.lib instead.
Thanks guys! I hope that by giving the full answer like this it can help some one else who has a similar problem.

Unresolved External Symbol [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is an undefined reference/unresolved external symbol error and how do I fix it?
I am working on wrapping a large number of .h and .lib files from native C++ to Managed C++ for eventual use as a referenced .dll in C#.
I have the .lib files linked in and everything has been going smoothly so far. Upon wrapping the latest .h file, I hit a snag when 2 functions came back with the link error:
error LNK2019: unresolved external symbol __imp__htonl#4 referenced in function
"public: void __thiscall Field::setCharacter(unsigned char,int)"
(?setCharacter#Field##QAEXEH#Z) myutils.lib
I have referenced myutils.lib in the linker options, so that shouldn't be the issue.
What's strange is that I have about 20 functions in this particular .h file and all of the rest are linking just fine except for 3 functions.
Any ideas?
The missing symbol is __imp__htonl#4, which is a C++ mangled name for htonl, which is a function that converts a long value from host to network order. The #4 is used to mangle the input parameters and is part of C++ support for overloaded functions to allow the linker to resolve the right function w/o name collisions.
Make sure that you are linked to the network library that you are referencing this symbol from. Presumably your package is using some special definition of this symbol, instead of the MACRO that it usually is.
Are you sure the signatures match? Be sure to checked for signed-ness and const-ness. Also, make sure functions aren't inlined.
I ran into this error when I compiled against a library and then changed the library before linking. Make sure your headers are the same ones provided by your library (not copied from another architecture, etc). Of course, make sure you're linking against ws2_32.lib (-lws2_32 for mingw/gcc).
Additionally, if you are using GCC/mingw you may want to take a look at this: MinGW linker error: winsock