MSVC2019 Missing symbol names from static library - c++

I'm newbe with C++ please help me if someone can!
I've made a binary bigint object which work well.
I complied it to a static library and tried to include into an another program but it fails with errors like this:
combinations.obj||error LNK2019: unresolved external symbol "public: static void __cdecl BinBigInt::bifactorial(class BinBigInt const &,class BinBigInt &)" (?bifactorial#BinBigInt##SAXAEBV1#AEAV1##Z) referenced in function "unsigned __int64 __cdecl combi::nonrepCombination(char,char,class std::basic_string,class std::allocator >)" (??$nonrepCombination#D#combi##YA_KDDV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)|
If i copy paste the two code together everything is work well so the codes seems to be OK.
I read a lot of topics about the LNK2019 error and checked the compiler settings and Release vs. Debug version matching and so on but nothing helped.
At last I checked the symbols in the .lib file manually with MS dumpbin as guessed in some topic to check name manling problems but I found that a lot of (9) function name and all (7) of internally defined operators really not contained by the .lib file. (compiler just pop up 5 unresolved external symbol error those thats which I really tryed to used in the implementing file.)
It seems to be not just a name manling problem I absolutely not found those symbol names with dumpbin.
While lot of other functions which defined inside the object and operators (like Comparisson, bitwise etc.) which defined out of the object is contained by the .lib file.
I used: dumpbin /symbols binbigint.lib | findstr "function name" in a lot of version to check them.
I use Code::Blocks with MSVC2019 host and target both x64.
Someone have any guess what can couse that the lib didn't contain some symbols?
(The library code is about 3.000 row long so I didn't copy here...)

Well. I found the problem...
That was me... :(
Originally I've wrote the whole object declaration and definition inside the object scope in a .cpp file.
And it worked well. And I thought will be enough to delete the implementetion parts in the header and I can leaving the whole class code with the class header in the .cpp too and I forget include the .h to the .cpp so compiler not generated errors about it.
The fact that it works with Copy paste put me on the right track (that must be some problem with the .h+.cpp version) at the end.
Sorry for disturbing!
(This was my first class which I try to use as .lib)

Related

How could a function call cause an unresolved external in some places, but not others?

I've read many of the answers on SO regarding unresolved externals, but only one seemed close to my situation (Unresolved external symbol for some function calls but not all), but the answers don't help.
Here's my situation: I'm using VS 2015 and compiling a .dll that uses several static libraries. (All the libraries are part of my VS 'project' - meaning I'm in control of all the code - I'm not using libraries from other sources yet).
By the way, I'm using the term "library" pretty loosely here. These are all C++ 'projects' in my VS 'solution' that create static libs, but they're mostly meant for code organization. They're not meant to be used in other projects or by other coding groups.
I have a global function that can be called in the library code. The declaration is in a header file that I #include where I want to use the function (the header file uses both #pragma once, and #ifndef guards). The definition is in the .dll code.
Here's what I don't understand: If I use the global function in some of the classes defined in my libraries, I'll get an unresolved external error. But, I can comment out the line, and the the .dll will compile, link, with other places in the code, from the same "library", successfully using the global function, called in the same manner. I run the code with a simple console app and the places that use the global function do indeed get appropriate returns from it. So, the global function does get resolved correctly.
From what I've read about 'unresolved external' errors, it seemed to me that either the linker should be able to resolve a reference, or not. If not, then shouldn't any of the calls to the global function result in an unresolved external, and keep the code from linking?
I feel like I'm missing something fundamental here. I'm not quite sure how go about trying to solve this. Instinct says to compare the code where it works (global function call doesn't cause linker errors) with where it doesn't. But, I can't see a difference from a linking perspective. All of the files that use the global function have to include the header with the function declaration, which they do (or the compiler would complain).
Any ideas, suggestions or insights are welcome. If you made it this far, well, thanks for your time!
Just in case the actual error might be helpful, here it is:
WindogEngineLibd.lib(ColumnFinder.obj) :
error LNK2019: unresolved external symbol "class IUserPreferences & __cdecl global_user_prefs::GetUserPreferences(void)" (?GetUserPreferences#global_user_prefs##YAAEAVIUserPreferences##XZ) referenced in function "public: class std::vector<struct std::pair<class std::weak_ptr<class CDataColumn const >,class std::weak_ptr<class CDataColumn const > >,class std::allocator<struct std::pair<class std::weak_ptr<class CDataColumn const >,class std::weak_ptr<class CDataColumn const > > > > __cdecl CColumnFinder::EvaluateDualExpressions(class CColumnExpression,class CColumnExpression)const " (?EvaluateDualExpressions#CColumnFinder##QEBA?AV?$vector#U?$pair#V?$weak_ptr#$$CBVCDataColumn###std##V12##std##V?$allocator#U?$pair#V?$weak_ptr#$$CBVCDataColumn###std##V12##std###2##std##VCColumnExpression##0#Z)
Edit 1
I'm going to add some pseudo code to (hopefully) make my situation more clear.
FileA.h in static lib 1 (the header file I include where needed)
float GlobalFunction();
FileB.cpp in static lib 2 (.cpp file for one class that uses GlobalFunction)
#include FileA.h;
float ClassWidget::UseGlobalValue()
{
return GlobalFunction() * 2.0f; // no problem with this line
}
FileC.cpp in static lib 2 (.cpp file for another class that tries to use GlobalFunction, in the same library as ClassWidget)
#include FileA.h;
float ClassThingy::TryToUseGlobalValue()
{
return GlobalFunction() * -1.0f; // uncommenting this line will cause link error
}
In the .dll project, I include the same header file:
FileDll.h
#include FileA.h;
And then define the function:
FileDll.cpp
float GlobalFunction()
{
return 2.0f;
}
If I comment out the definition of 'GlobalFunction' in FileDll.cpp, then all of the calls to 'GlobalFunction' cause unresolved external errors when I compile the .dll. With it there, all of the errors are resolved (including the one in ClassThingy).
When I try to use my .dll in a console app, the call to GlobalFunction in ClassThingy causes a link error, while the call in ClassWidget does not.
I'm pretty certain this has to do with way the .dll is getting linked, but I can't figure out how. If I create a console app that uses the static libs directly, and have it define 'GlobalFunction' - everything is fine, no link errors. If the console app were my goal, I'd just do that. But, the .dll is my goal - the console apps are just for testing.
I looked into using the 'extern' keyword, but from what I can tell, that's superfluous for file scoped functions.
Thanks again for any help, insight.

Compiling C library in C++. Linking issue. MuPDF [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 5 years ago.
I tried multiple PDF lybrary and MUPDF is my last chance to build my own small PDF application. But here I also have problems. Before that I had problem with LNK4098: defaultlib 'MSVCRT', but I solved that to set for all MUPDF library /MDd version. Most errors is solved after that, but still I can't to solve this:
error LNK2001: unresolved external symbol "void __cdecl pdfapp_open(struct pdfapp_s *,char *,int)" (?pdfapp_open##YAXPEAUpdfapp_s##PEADH#Z)
//more 3 errors
As MuPDF written on C, I do this:
extern "C" {
#include <pdfapp.h>
#include <mupdf\fitz\context.h>
}
But I get this errors:
error LNK2001: unresolved external symbol pdfapp_open
//more 3 errors
There is only three functions what I use in my application. I checked libs and headers linking, I set my project Debug, x64, /MDd also but I get the same.
Honestly, I can't find this three functons in no one libs. I'm not a expert in programming but I know that functions strings should be finding in libs, doesn't? What does mean this errors in my case?
Either your linker is failing to find the import library for mupdf or you have a name mangling/decoration type issue.
Once you download and build mupdf, there should be an import library for you to link to. Are you linking to it? Look in your project linker settings under Linker >> Input >> Additional Dependencies. Have you added such an import?
If not, add it in. It shouldn't be hard to find. It is likely right next to the mupdf DLL that you downloaded (or built).
Assuming you have added it, your next step is to verify that you are properly finding it. Look at the build output. Are you getting a warning about it? No, change the Linker >> General >> Show Progress to "Display all progress messages" and build again. Is there a warning about the library or function?
Last possibility that I can think of is that the DLL is actually exporting its functions as C++ (i.e. with name decoration), not as extern "C". In that case and your extern "C" brackets don't belong there. Or perhaps vice versa (i.e. maybe you failed to use extern "C" in all places you #include it). I can see that your example uses it but is that all the places?
If you are building the mupdf.DLL be sure it is being built the same way and with the same calling convention as your client code.

LNK2005 already defined error on inclusion of C-type header file in C++ project [MSVC12]

I've hit a roadblock in attempting to integrate some third-party code in my project. I'm attempting to integrate fontstash, which is a header-only OpenGL text rendering solution. (https://github.com/memononen/fontstash) As a whole, I'm also using SDL, GLEW, AssImp, Lua 5.3/LuaBridge, and Bullet Physics. I've placed the fontstash header files in my vc/include directory. Compilation proceeds normally but linking fails miserably in a huge wall of...
c_main.obj : error LNK2005: "unsigned int __cdecl glfonsRGBA(unsigned char,unsigned char,unsigned char,unsigned char)" (?glfonsRGBA##YAIEEEE#Z) already defined...
c_main.obj : error LNK2005: "void __cdecl glfonsDelete(struct FONScontext *)" (?glfonsDelete##YAXPAUFONScontext###Z) already defined in...
...
c_main.obj : error LNK2005: _stbtt_FindMatchingFont already defined in...
c_main.obj : error LNK2005: _stbtt_GetFontNameString already defined...
It appears to simply iterate the entire list of functions provided via the fontstash header files. I've tried wrapping the headers in an extern "C" {} to no avail. I've tried including the files from the project directory as well. I'm at a loss as to why this would be happening and where to begin figuring out what would be causing it. As indicated in the topic title I'm using MSVC12/Win7, and I'm building for Windows and compiling for x86.
Additionally, I'm including the files more than once as the relevant code that utilizes fontstash is used in other locations. I've thought about this being the issue, but the provided header files from fontstash have inclusion guards so I fail to see why this would occur in that regard.
This is a common problem with the header files, that contain implementaton. When you use #include directive, compiler simply inserts .h file content instead of it. So when you use this header in different places of your project, you get several identical implementations of its methods and global variables. Since it has #ifdef or #pragma once compiler guard, it compiles just fine. But when linker is trying to unite all compiled obj files to one executable module, it gets several identical implementations. Since it could not know which one should be used, you get LNK2005 error. To solve this problem you could move implementations and globals into the separate cpp file and include it in the project. Other way would be to mark all header functions as inline, or use __declspec(selectany)

Unresolved external symbol referenced in function

I realize this is a hard one to answer without providing you with huge amounts of code (which I'll try to spare you).
Essentially i'm getting this error in class X, which #includes the class Y header. The class Y header has three definitions for getters
// Getters
static ID3D10Device* PDevice();
static ID3D10Buffer* PBuffer();
static ID3D10Buffer* IBuffer();
I get three identical errors, all occur in class X. so essentially the error is:
Unresolved external symbol ID3D10Device* PDevice() referenced in function (constructor of class X)
sorry if that's a bit vague. Any idea why this might be happening? I've googled it but I can only really make an educated guess as to what this error is.
First of all this is a linker error.
This linker error means that the mangled name PDevice et al is not found.
Can you make sure that you have an implementation of a function that matches the definition?
Also, maybe obvious but just check that you actualy have an implementation.
If your implementation is in an external lib, be sure you have included the other lib in your linker.
Hope that helps!
Make sure the files that contain the definition and implementation of class Y are added to the project, so that the linker finds the symbols in the Y.o file
Make sure you set the dependencies right (add the lib file). In Visual Studio you can do so by Properties -> Linker -> Input -> Additional Dependencies. In the textbox you can now enter the name of your .lib file.
Another reason this can happen is when both C and C++ source files are used to create a binary. The compiler uses a different naming mechanism for C symbols vs C++ symbols. Please read the "Adding C++ To The Picture" section in this great article. One reason for example is function overloading in C++. The symbol name of the function includes the function signature (argument types and their order). To quote from the article, "all of the information about the function signature is mangled into a textual form, and that becomes the actual name of the symbol as seen by the linker." So, when C code file needs to call a function defined in a C++ file, the C code's object file only mentions the name of that C++ function (doesn't include function signature). The C++ object file however contains the mangled name (which includes the function signature). Hence, the linker reports an "error LNK2019: unresolved external symbol FOO_CPP_FUNC referenced in BAR_C_FUNC". The solution suggested there is to add an extern "C" around the declaration & definition of the C++ function.

How can I resolve "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)
Closed 8 years ago.
I've got this MFC application I'm working on that needs to have an embedded database. So I went hunting for a slick, fast "embeddable" database for it and stumbled accross SQLite.
I created a DB with it, and I created a static library project with Visual Studio 2008. the library project will be used in another main project.
In the library project, I created a class DBClass with a method AddFeedToDB(CFeed f). The library project uses the .lib file from codeproject (cppsqlite3.lib).
When compiling the static library, no error is detected, but when I try to use the library project file in the main project, I get these type of errors:
error LNK2019: unresolved external symbol "public:void __thiscall
CppSQLite3DB::close(void)" (?close#CppSQLite3DB##QAEXXZ
referenced in function "public: int __thiscall
CTalkingFeedsDB::AddFeedToDB(class CFeed,char const*)" (?
AddFeedToDB#CTalkingFeedsDB##QAEHVCFeed##PDB#Z
What am I missing?
I know it is already 2 years since this question... but i run in the same situation here. Added all the header files... added the lib directories.. and keep having this error.
So i added manually the lib to the Configuration Properties -> Linker -> Input -> Aditional Dependencies
and all works for me.
It happened to me more than once that I thought symbol XXX (i.e. ?close#CppSQLite3DB##QAEXXZ) was in the import lib, while the actual symbol was __impXXX (i.e. __imp?close#CppSQLite3DB##QAEXXZ).
The reason for the linker error is then to be found in the compilation step: the compiler will generate the ?close#CppSQLite3DB##QAEXXZ symbol to be imported, where it should generate __imp?close#CppSQLite3DB##QAEXXZ. This often means that the function declaration itself didn't have __declspec( dllimport ). Which may be caused by some preprocessor symbol not being defined. Or the __declspec not being there at all...
Don't know if it is your case, but the imp prefix may mean that you are compiling a x64 library in a Win32 project.
You either need to link the codeproject SQLite lib to your executable, or to include the sources files in your project directly. (Which one did you do ?)
I would follow these steps:
think about what library or .obj file you expect the symbol to be exported by.
check whether it actually does export that very symbol (check character-wise). Sometimes, it's the calling convention differs.
check if the library you expect to contain the symbol is known to the linker - first check for the 'additional libraries', then check if the library is actually found (I mostly do this by using filemon.exe from sysinternals, and look for link.exe to open the lib file. )
After thinking a while, you may find that your library project will not export the sought for function. That function is in the database lib. You should add that lib to your main project. It's no use adding it to your static lib project.
The compiler and linker will not link one library into another (unless one is a DLL). You need to specify both libraries (cppsqlite3.lib and your own static library) in your main project.