unresolved external symbol error even though the function exists char* - c++

I have two projects in one solution in Visual Studio. One of the projects (application) depends on another (library). The library has a method:
int foo(_TCHAR*);
It is properly defined in header and implemented. In the application I call the method like this:
int bar(_TCHAR* str) {
return foo(str);
}
The error I am getting is
error LNK2001: unresolved external symbol "public: int __cdecl
foo(char *)"
The method signature is exactly the same and the same type of variable is being used.

The reason behind the linker not being able to match the methods is the mismatched Character Set option in projects General Properties page.
The library project had this option set to Use Unicode Character Set and the application to Use Multi-Byte Character Set.
Unifying the Character Set in both projects fixed this error.

Related

How to link two projects in Visual Studio?

I have two projects in the same Visual Studio 2019 solution. My first project creates an exe whereas the second is a dynamic library. I've linked the second project to the first one, so now I can use class, structs, variables and functions that are declared and defined in the second project in the first one. But I can't call functions in the project 2 that are in the project 1. I do not know how can I link my first project to the second.
When I try to access to the project 1 when I'm in the second, linker errors appears
AlienEngineCore.obj : error LNK2019: external symbol "void __cdecl log(char const * const,int,char const *,...)" (?log##YAXQBDHPBDZZ) unresolved to which reference is made in functi¾n "void __cdecl LogToConsole(void)" (?LogToConsole##YAXXZ)

LNK2019 Error Header is included but not linked?

I have some classes and headers that are external to my program that i wish to use. I am using MSVC2013 and have specified the location of these files in the "Additional include directories" entry in the project settings window. Intellisense recognises the paths and provides the appropriate syntax highlight and my program compiles fine except at the end it throws LNK2019 errors for any usage of these classes. Is there another place in linker settings I need to specify to link external files or am i missing something obvious here.
Error messages:
MainWindow.obj : error LNK2019: unresolved external symbol "public: __cdecl QPropertyEditorWidget::QPropertyEditorWidget(class QWidget *)" (??0QPropertyEditorWidget##QEAA#PEAVQWidget###Z) referenced in function "public: __cdecl MainWindow::MainWindow(class QWidget *)" (??0MainWindow##QEAA#PEAVQWidget###Z)
Header files typically provide the API and/or data structures for some other code (most likely a library). With the headers you know about the "other" code, but you don't actually "have it" in your program.
Specifying where to find the headers is only half of the problem. You also need to specify where to find the object files (or libraries), and which objects or libraries to link against.

MFC CString Linker error between two projects

I have 2 projects in c++ (MFC)
One is a library project which im using in the second one (an executable one).
They work together great, until I call a function from the regular project that takes a CString as argument. I get a linker error like this
error LNK2019: unresolved external symbol "public: void __thiscall
CTextDisplay::SetText1(class ATL::CStringT<wchar_t,class StrTraitMFC_DLL<wchar_t,
class ATL::ChTraitsCRT<wchar_t> > >)" (?SetText1#CTextDisplay##QAEXV?$CStringT#_WV?
$StrTraitMFC_DLL#_WV?$ChTraitsCRT#_W#ATL#####ATL###Z) referenced in function
"public: void __thiscall CManualPane::SetBeadCountFor(int,double)"
(?SetBeadCountFor#CManualPane##QAEXHN#Z) C:\source\IQ-Project\IQ\ManualPane.obj IQ
The executable project has its character set to UNICODE but the Library has it set to Multy-Byte chatacter set. I really can't change them without getting ridiculous amounts of errors.
Any suggestions?
Edit: The reason we have different settings in these two projects is because the Executable project is basically an external project that my group didn't build or create. We just had to bring it in and use it. The library project is something we've all been working on for a couple years.
When you include the headers of the library project in the executable, there is likely a typedef which is used in the declaration of the function. Since the executable uses UNICODE, the declaration is now in UNICODE. However, the library implementation is still in MultiByte and so the definition doesn't match the declaration leading to the linker error.
Look into how these typedefs are being setup, and you might be able to do some special #define, #undef around the included header.
In the end it proved to be a better idea to avoid having unicode and multybyte projects in the same solution so I moved it all to unicode and went from there

Building and linking test code for Crypto++

I'm trying to write some simple test code for the Crypto++ library for a project. I have yet to manage to get my own code to build though. It compiles fine, the problem comes in linking. I'm still pretty new to Visual Studios, but I'm using VS10. The errors I'm getting are:
1>sec_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CryptoPP::DES::Base::ProcessAndXorBlock(unsigned char const *,unsigned char const *,unsigned char *)const " (?ProcessAndXorBlock#Base#DES#CryptoPP##UBEXPBE0PAE#Z)
1>sec_test.obj : error LNK2001: unresolved external symbol "public: virtual void __thiscall CryptoPP::DES::Base::UncheckedSetKey(unsigned char const *,unsigned int,class CryptoPP::NameValuePairs const &)" (?UncheckedSetKey#Base#DES#CryptoPP##UAEXPBEIABVNameValuePairs#3##Z)
I've tried everything I can find in the documentation in terms of linking. I've never linked against a DLL before, but these are the errors I get when I try to. If I try to do what I think is linking against the static library version, I get even more.
I should note, I'm trying to test pure DES at the moment. In particular, here are the API calls I'm making, just to be safe:
DES::Encryption DES_encrypt;
DES_encrypt.SetKey(key, 64);
DES_encrypt.ProcessAndXorBlock(plaintext, NULL, ciphertext);
DES::Decryption DES_decrypt;
DES_decrypt.SetKey(key, 64);
DES_decrypt.ProcessAndXorBlock(ciphertext, NULL, decrypted);
If anyone can help, or point me in the right direction, I'd be much obliged.
First thing to check is your project properties (right click on project, click Properties).
External libraries need to be specified in the Linker->Input->Additional Dependencies field, either by absolute path or in conjunction with the other VS path properties.
You can look in Linker->Command Line and check that the .lib files you want are actually being passed to the linker.
An alternate method is to turn off the Suppress Startup Banner option in the Linker->General options, and then check the build output to make sure it has what you want.
If you know that the libraries you want are being linked, another useful trick is to check the output of the strings command (in linux or cygwin) or dumpbin /HEADERS in the VS command prompt. You can look through the output of these commands for the symbol VS claims is missing to verify that it really is defined in the .lib file. Sometimes larger software packages have multiple .lib files, so this can help make sure you are linking the one that has the symbol you want.

Creating object by using new operator causes unresolved external symbol error C++ FTGL

I'm using FTGL library in my Microsoft Visual Studio 2012, C++ project. I finally managed to properly link it to my project as I can properly render a font by using:
FTGLPixmapFont font("C:/Windows/Fonts/Arial.ttf");
font.Render("Hello world");
Everything seems to be ok until I try to create an object by using new operator:
FTGLPixmapFont* font = new FTGLPixmapFont("C:/Windows/Fonts/Arial.ttf"); // This causes error
font->Render("Hello world");
The code above produces this error:
AppLayer.obj : error LNK2001: unresolved external symbol "public: virtual float __thiscall FTFont::Advance(unsigned short const *,int,class FTPoint)" (?Advance#FTFont##UAEMPBGHVFTPoint###Z)
1>AppLayer.obj : error LNK2001: unresolved external symbol "public: virtual class FTBBox __thiscall FTFont::BBox(unsigned short const *,int,class FTPoint,class FTPoint)" (?BBox#FTFont##UAE?AVFTBBox##PBGHVFTPoint##1#Z)
1>AppLayer.obj : error LNK2001: unresolved external symbol "public: virtual class FTPoint __thiscall FTFont::Render(unsigned short const *,int,class FTPoint,class FTPoint,int)" (?Render#FTFont##UAE?AVFTPoint##PBGHV2#1H#Z)
I have completely no idea what can be reason for this. I'd really appreciate any answers.
Thanks!
It looks like you forgot to link a library, or to include a file in the build.
This class inherits the class FTFont. Check that you correctly linked the library including this definition.
In visual, you can just link the list by adding the .lib file to the project like if it is a cpp.
If you link another project from the visual solution, check in the properties of your project if the dependance to the other project is set correctly.
best
Those specific linker errors happen if the "Treat WChar_T As Built in Type" property (found in C/C++ / Language in the property pages) is set to 'Yes' for the complication of the FTGL library and 'No' for the compilation of your application using the library.
The compiler preparing functions with "WChar_t const*" as the argument type in the FTGL library, but your program will be looking for "unsigned short *const", and so won't find any function with that signature.
To fix, change the property "Treat WChar_T As Built in Type" in your project so that it matches the setting in the FTGL library; clean and recompile and it should work.