How to link two projects in Visual Studio? - c++

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)

Related

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.

Ms Visual Studio 2012, "unresolved external symbol"

I am trying to compile a C++ application through Microsoft Visual Studio 2012. I have linked the Boost, Jni, Acml libraries to the application.
When I click on rebuild, the compilation does not finish.
This is the first error I get:
Error 28 error LNK2019: unresolved external symbol _sgesdd referenced
in function "void __cdecl testLapackDGESDD_EASY(void)"
(?testLapackDGESDD_EASY##YAXXZ) C:\Users\DavideChicco\Documents\Visual
Studio 2012\Projects\Solution\mainConsole.obj
Do you have any idea of what this is related to?
Thanks
Your compilation seems to have finished, but the error happens during linking: the definition of the function _sgesdd called in testLapackDGESDD_EASY is not found. So
- either this function is defined in an external dll, and you need to add the lib in your solution,
- or this function is supposed to be defined in your project, and you need to implement its definition.
aha, The compiler cant't find the function "_sgesdd", I recommend you that add the lib which is supplied by the author of DLL.

unresolved external symbol error even though the function exists char*

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.

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();
};

Can a CWinApp be placed in a DLL?

I'm updating some legacy apps to Visual Studio 10 and am in linker hell. All of these DLLs derive classes from CWinApp and use AfxGetApp() to get access to the object. When I link the DLLs I get unresolved externals that look like global static objects that would get pulled in by a normal app's main():
Shell.lib(SHELL.obj) : error LNK2001: unresolved external symbol "public: static struct CRuntimeClass const CException::classCException" (?classCException#CException##2UCRuntimeClass##B)
Shell.lib(SHELL.obj) : error LNK2001: unresolved external symbol "public: static struct CRuntimeClass const CFrameWnd::classCFrameWnd" (?classCFrameWnd#CFrameWnd##2UCRuntimeClass##B)
Shell.lib(SHELL.obj) : error LNK2001: unresolved external symbol "public: static class CRect const CFrameWnd::rectDefault" (?rectDefault#CFrameWnd##2VCRect##B)
My current combination of ignore default libraries and additional libraries (the method you can easily google to find the answer to linker hell) is: msvcprtd.lib,mfc100d.lib,mfcs100d.lib,libcmtd.lib
When I add nafxcwd.lib (the mfc library), these three external symbols resolve but I end up with a bunch of other duplicate symbols (requiring the use of /FORCE:MULTIPLE) and in end ___argc and ___argv become unresolved.
So the basic question is: Can you link a DLL containing a CWinApp in VS10? How do you setup the linker to do it?
Can you link a DLL containing a CWinApp in VS10?
When I create a new project in Visual Studio 10 and use the MFC DLL template, I found the template code itself includes a class derived from CWinApp. Of course the template compiles and links fine, so the answer to this question is a most definite yes.
I searched for definitions to those objects that your linker is complaining about and I found them in afx.h and afxwin.h, inside Visual Studio's include directory, packaged with the rest of the ATL and MFC code that is included with Visual Studio when you install it.
Are you using the express edition by chance? One of the few differences between the express edition and the pro edition of Visual Studio 2010 is that ATL and MFC applications won't compile in the express edition. You have to use the pro edition.