LNK 2028 - 2019 / Managed and Unmanaged C++ ? (VS 2008) - c++

I am trying to link an open-source library to one of my project. The library is unmanaged (named Tetgen) and my project is in managed C++.
My project recognizes the header and can use the functions defined in it. But I get a 2028 error each time it wants to access to some methods defined in the .cpp:
error LNK2028: unresolved token (0A000E20) "public: void __thiscall tetgenio::save_nodes(char const *)" (?save_nodes#tetgenio##$$FQAEXPBD#Z) referenced in function "public: virtual bool __thiscall ForwardModelingPlugin::CustomMeshVol3D::tesselate(void)" (?tesselate#CustomMeshVol3D#ForwardModelingPlugin##$$FUAE_NXZ)
I have tried to create a test function:
int tetgenio::Test(int i) {
return i;
}
...and another Testbis function, defined in the header. Testbis works, Test gives a 2028 error.
I have compared the .obj of my project and the .lib created, and for the lib there is:
save_nodes#tetgenio##QAEXPAD#Z
But in the .obj it is:
save_nodes#tetgenio##$$FQAEXPBD#Z in the .obj
It appears they are not the same.
Everything is compiled with /clr. I've tried creating both a .lib and a .dll, with same results either way.

That was because of a linker problem linking to different folders containing the same files.

Related

Link errors with GraphicsMagick

I downloaded and compiled GraphicsMagick, 1.3.23, Q16, x64, StaticMT version. I had to convert the Visual Studio 7 solution generated by GraphicsMagick's build utility to Visual Studio 2015 format. I linked my project to CORE_DB_magick_.lib and CORE_DB_Magick++_.lib.
When the linker ran, it produced unresolved external symbols when linking InitializeMagick() and DestroyMagick()
1>wtd.lib(WebController.obj) : error LNK2019: unresolved external symbol __imp_DestroyMagick referenced in function "public: __cdecl Wt::WebController::~WebController(void)" (??1WebController#Wt##QEAA#XZ)
1>wtd.lib(WebController.obj) : error LNK2019: unresolved external symbol __imp_InitializeMagick referenced in function "public: __cdecl Wt::WebController::WebController(class Wt::WServer &,class std::basic_string,class std::allocator > const &,bool)" (??0WebController#Wt##QEAA#AEAVWServer#1#AEBV?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std##_N#Z)
I can't understand why the symbols are not being linked. Any ideas?
Apparently, GraphicsMagick Static versions do not link properly.
What is your project type? I had a similar problem when trying to link the GraphicsMagick libraries to a DLL.
The clue here is that __imp is the function decoration for DLL imports, so you're trying to link with DLL functions.
The problem is that the header magick/common.h, when linked to a DLL project, reads the current configuration of the Visual Studio pre-processor environment to determine which mode the library is in, which is obviously wrong if you're trying to link static libraries into your DLL, for example. In this case, it defines MagickExport to __declspec(dllimport).
AFAIK this is a bug in the library. For proper static build support, magick/common.h needs to do something like read information from the magick/magick_config.h to determine what mode the library was actually built in and define MagickExport appropriately.
Since your library is statically linked, you can fix this by commenting out everything in the define:
#if defined(MSWINDOWS) && !defined(__CYGWIN__)
and replacing it with:
#define MagickExport
#define ModuleExport
#define MagickGlobal

Google Protocol buffer c++ link error when trying to use it in project

I am trying to get protocol buffer to work but linking problems occurs.
All the errors looks something along like this
error LNK2019: unresolved external symbol "public: bool __cdecl google::protobuf::MessageLite::ParseFromArray(void const *,int)" (?ParseFromArray#MessageLite#protobuf#google##QEAA_NPEBXH#Z) referenced in function "void __cdecl ReceiveRequest(bool *)" (?ReceiveRequest##YAXPEA_N#Z)
I am using visual studio pro 2013 and this is what I have done:
I compiled the protocol buffer and added the libprotobuf.lib to the project(Add->Existing Item->libprotobuf.lib)
I've added the header files in C/C++ -> General -> "Additional Include Directories"
I have tried to add the library all the different ways I know but I still get these linking errors...
Any idea what I might have done wrong?
// Eric
You need to include the generated protocol buffer .cc (and maybe the .h) file (ie the file with the specific getters and setters for your specific proto) in your VS project (ie there has to be a little icon for them in the solution explorer) or else visual studio won't generate the code for it and thus there's nothing to link to.
Make sure you have the correct libraries for your architecture (e.g. x86 / x64).
That has caught me out a few times.

constructor overloading Class get Linker Error Using DLL?

I have created Regular DLLs Dynamically Linked to MFC.
in before build dll in dll project i used add another class. this class provide more method of constructor overloading.
Then I build dll successfully after i used this lib and dll file include project and then go to build and get linker error when using constructor overloading class
My Class Name: Object
Error 11 error LNK2001: unresolved external symbol "public: __thiscall AvinashiAMF::Object::~Object(void)" (??1Object#AvinashiAMF##QAE#XZ) BuleCappServiceUseDynamicDllDlg.obj
Error 10 error LNK2001: unresolved external symbol "public: __thiscall AvinashiAMF::Object::Object(enum AvinashiAMF::ObjectType,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >)" (??0Object#AvinashiAMF##QAE#W4ObjectType#1#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z) BuleCappServiceUseDynamicDllDlg.obj
Error 12 fatal error LNK1120: 2 unresolved externals D:\Test Aplications\Visual Studio 2008\Projects\BuleCappServiceUseDynamicDll\Release\BuleCappServiceUseDynamicDll.exe
You're probably not exporting your class. See here http://msdn.microsoft.com/en-us/library/81h27t8c.aspx
Also, a nice tool to check for this kind of issues is depends.exe, included with Visual Studio, which allows you to check for exported and imported symbols.
There are two significantly different dynamic link libraries: implicitly linked and explicitly linked.
In short, implicitly linked is linked at the compile time. You need an import library to link with(.lib), header file with functions prototypes and properly usage of the dllexport and dllexport or use .def file.
dllexport/dllexport are easier to use in this case. System takes care of loading libraries (DLLs).
Explicit linking is a runtime linking. You do not need any import library or headers. You need to know what is the function signature. You have to explicitly call LoadLibrary and GetProcAddress to call the function.
To see how to properly create macros for export/import create empty Win32 project select DLL and make sure the Export symbols check box is checked.
In the main header file you will see explanation how to use macros for import/export.

Can't get rid of LNK 2019 error in Dll

I keep getting link 2019 errors on this ctor,ctor-copy ctor-assignment code that works in other projects with no errors. I am trying to include it in a plain DLL generated with the MFC support option checked. I'm in VS2010.
#include "stdafx.h"
#include "Name.h"
CNameBase::CNameBase()
{
IsGlobal = false;
UseShortName = true;
m_ShortName = NO_NAME_ID ;
m_Description = "";
pMe = this;
}
CNameBase::CNameBase( const CNameBase& ref )
{
m_Description = ref.m_Description;
IsGlobal = ref.IsGlobal;
m_LongName = ref.m_LongName;
m_ShortName = ref.m_ShortName;
UseShortName = ref.UseShortName;
pMe = ref.pMe;
}
CNameBase& CNameBase::operator=( const CNameBase& ref )
{
m_Description = ref.m_Description;
IsGlobal = ref.IsGlobal;
m_LongName = ref.m_LongName;
m_ShortName = ref.m_ShortName;
UseShortName = ref.UseShortName;
pMe = ref.pMe;
return *this;
}
The link errors are:
error LNK2019: unresolved external symbol "public: __thiscall CNameBase::CNameBase(void)" (??0CNameBase##QAE#XZ) referenced in function "public: __thiscall CName::CName(void)" (??0CName##QAE#XZ)
1>clayer.obj : error LNK2019: unresolved external symbol "public: __thiscall CNameBase::CNameBase(class CNameBase const &)" (??0CNameBase##QAE#ABV0##Z) referenced in function "public: __thiscall CName::CName(class CName const &)" (??0CName##QAE#ABV0##Z)
1>clayer.obj : error LNK2019: unresolved external symbol "public: class CNameBase & __thiscall CNameBase::operator=(class CNameBase const &)" (??4CNameBase##QAEAAV0#ABV0##Z) referenced in function "public: class CName & __thiscall CName::operator=(class CName const &)" (??4CName##QAEAAV0#ABV0##Z)
1>C:\devt\hftappb\Debug\CLayer.dll : fatal error LNK1120: 3 unresolved externals
The funny thing is this code works and has worked. The Ctors do what they're supporsed to do and have been in other solution. I stick the class in this file and suddenly get these errors that I am having dififculty resolving. I've compared project settings between this project and projects I know this code works in. What else could be causing this?
The cpp file that holds the implementation of the 3 functions doesn't compile/link into the exe/dll.
If you're using C++ namespaces, you need to make sure that both header and cpp use it. The namespace is part of the class name.
Thank you all for responding to my question. The question took the better part of a day to resolve but I wanted to share the answer with others since it can happen to anyone. The solution to the problem wasn't any code, it was a configuration issue. In VS2010, apparently, ALL files that your project references must be in the list of files that appear with the project in the Solution Explorer. I am not privy to the underlying architecture so I can't answer why this is required. On the surface, there's NO reason for it. If there are files #included into a project either explicity or implicitly through the Project paths and whose functions I reference from files that are included in the Project's list of files in Solution Explorer, functions in those files should be able to reference functions in files that aren't included in the project filelist in Sol Exp.
The rules for #include and other rules are convoluted enough in C++ without MSFT adding their layers of confusion. These types of errors would be easily prevented with better error reporting and/or UI management.
The rules I've learned are this
1. Never make more than 1 copy of the same file in the entire solution. Multiple copies of the same file are hard to keep symchronized and lead to confusion as to which one is being used.
2. All files being used in a project must be listed in that project but rule 1 should be respected.
Steps (approximate)
1. Create a project
2. Create a .h and .cpp with global functions (or in a class doesn't matter)
3. In the same parent directory as project 1, create project 2 There should be two project directories underneath one parent
4. In project 2 in File Explorer, create a subfolder called Common
5. Create a .h and .cpp in that folder and create a simple function that returns a value
6. Add those files to project 2 in Solution Explorer
7. In Project 1, in the .cpp, create a function that #includes the file for project 2 and calls that function in that file.
8. The compiler generates a 2019 and 2001 unresolved external link error. What it should do is report that those files are required to be in your project's file list.

How to Link to a .lib file in Visual C++ 2010? Without referencing the project?

I just have a problem that I have been trying to fix for the longest time.
I have a static library project in visual c++, and I want another project to be able to link to it. Up until now, I have simply been adding a reference to the static library project, which automatically links the library.
I want to be able to link to the library using only the header files and the .lib file. However, I get a "Unresolved external symbol" error.
I thought I was doing it right - I specified the include directory, the library directory, and went into the linker input properties and provided the lib as an additional dependency.
I am able to reference other static libraries this way (like SDL), so why do I get errors when I try to reference mine?
Thanks for the help.
Is the problem that its not referencing the actual lib file, or is something within the lib itself?
These are the error messages I get:
Error 2 error LNK2019: unresolved external symbol "public: void __thiscall XEngine::XCore::XScreen::init(class XEngine::XCore::XGame &)" (?init#XScreen#XCore#XEngine##QAEXAAVXGame#23##Z) referenced in function "void __cdecl XEngine::XEngineInit(class XEngine::XCore::XScreen &,class XEngine::XCore::XGame &)" (?XEngineInit#XEngine##YAXAAVXScreen#XCore#1#AAVXGame#31##Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.obj Pong
Error 3 error LNK2019: unresolved external symbol "public: __thiscall XEngine::XCore::XScreen::~XScreen(void)" (??1XScreen#XCore#XEngine##QAE#XZ) referenced in function "void __cdecl XEngine::XEngineInit(class XEngine::XCore::XGame &)" (?XEngineInit#XEngine##YAXAAVXGame#XCore#1##Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.obj Pong
Error 4 error LNK2019: unresolved external symbol "public: __thiscall XEngine::XCore::XScreen::XScreen(void)" (??0XScreen#XCore#XEngine##QAE#XZ) referenced in function "void __cdecl XEngine::XEngineInit(class XEngine::XCore::XGame &)" (?XEngineInit#XEngine##YAXAAVXGame#XCore#1##Z) C:\Users\Xander Masotto\Documents\Visual Studio 2010\Projects\Pong\Pong\source.obj Pong
Make sure that you are exporting the functions, classes, and variables in your library that you want exposed to other applications (i.e. your dll or exe). By default they are not exposed.
The ground work to do this is typically layed out when you create the project for your library.
#ifdef TESTLIB_EXPORTS
#define TESTLIB_API __declspec(dllexport)
#else
#define TESTLIB_API __declspec(dllimport)
#endif
With the code above generated during project creation there are only two more things for me to do to expose functions,classes, or variables:
1) Make sure that I have TESTLIB_EXPORTS defined as a preprocessor. Project settings: C++/Preprocessor/PreprocessorDefinitions
2) Use the TESTLIB_API define on each function,class, or variable i want exposed:
class TESTLIB_API Order {
void doSomething();
};