error LNK2019: unresolved external symbol CheckLr referenced in function - c++

legitmate_relationship.dll : Basically this dll is consumed for one of the unit test project unittest_legitimate_relationship.
As soon as I build unittest_legitimate project, giving some linker errors.
error LNK2019 : unresolved external symbol CheckLr referenced in function "private: bool __cdecl LrTester::LrTest()
here is function defalcation which i am going to call in my unit test project,and which is declared in "legitmate_relationship.h " header file
#ifdef __cplusplus
extern "C"
{
#endif
DLL_IMPORT (bool_t)
CheckLr(STD_HANDLE hPrincipal, STD_HANDLE hCollectionIn, STD_HANDLE hCollectionOut);
#ifdef __cplusplus
}
function is defined like this in file "lrrule.cpp"
DLL_EXPORT (bool_t)
CheckLr(STD_HANDLE hCollectionIn, STD_HANDLE hCollectionOut, STD_HANDLE hPrincipal)
{
}
In order to make use of checklr function in unittest_legitimate project, I have added the .lib file reference in addional dependence . So that the function definition should be available for project.
If I am compiling unittest-legitimate project, I am getting linker errors as mentioned above.
call made to checklr() in lrtest.cpp :
CheckLr((STD_HANDLE)permissionCollectionIn, (STD_HANDLE)permissionCollectionOut, (STD_HANDLE)principal);
I am not sure how to resolve such kind of linker errors.Thanks in advance.

1.Three things to check, the legitimaterelasionship.dll project and unittest_legitimate-relationship.exe project should be the same:
MT/MD
x86/x64
cplusplus/c
2.The legitimaterelasionship.dll project generates a legitimaterelasionship.lib file. The unittest_legitimate-relationship.exe project should link to this legitimaterelasionship.lib file.

Related

Symbols in lib not being included

I have been updating a big working C++ Windows application in Visual Studio 2022 into several static libs.
The idea is to share part of the code to an external developer, and not share the complete C++ files of the project. Only part of the code would be shared, and the rest would be .lib's and .h's.
The structure of the entire solution is:
External LIB
Library 1 LIB - Referencing .h's from the External LIB
Application EXE - Using the above libs
When building the application EXE, the link fails with thousands of errors of missing symbols from the External LIB. "LNK2001 unresolved external symbol "__declspec(dllimport) public: bool __thiscall ..."
1>Common.lib(LocalizedString.obj) : error LNK2001: unresolved external symbol "__declspec(dllimport) public: bool __thiscall cocos2d::Value::isNull(void)const " (__imp_?isNull#Value#cocos2d##QBE_NXZ)
1> Hint on symbols that are defined and could potentially match:
1> "__declspec(dllimport) public: bool __thiscall cocos2d::Data::isNull(void)const " (__imp_?isNull#Data#cocos2d##QBE_NXZ)
The EXE is configured to include all the libs (configured in VS project settings: Additional Dependencies).
All the projects are being built with __cdecl (/Gd) and Multi-threaded Debug DLL (/MDd).
If I add to the EXE code some dummy declarations of symbols that are being referred into the "unresolved external symbol" error, Visual Studio adds those symbols and I can see the number of errors decreasing. But this is not a good solution...
It looks like somehow Visual Studios is adding all the symbols automatically.
I believe this whole issue may be related with the following. When building my libs I get the following "warning LNK4006: XXXX already defined in XXXXX.lib(xxhash.obj); second definition ignored".
I have tried different project settings, like "Link Library Dependencies: Yes". But nothing seems to fix it.
I'm stuck with this for two days... Can someone save me?
Found the problem. There was an hidden define in the external lib adding the dllimport to every external class.
#if defined(CC_STATIC)
#define CC_DLL
#else
#if defined(_USRDLL)
#define CC_DLL __declspec(dllexport)
#else /* use a DLL library */
#define CC_DLL __declspec(dllimport)
#endif
#endif

gtest linking error in VS2015: unresolved externals for functions defined in cpp file

I have created a sample project in VS2015, having one header file and one cpp file. Content of my files are:
Header.h
int fun2(int a);
Source.cpp
#include "Header.h"
void main()
{
fun()2;
}
int fun2(int a) {
return ++a;
}
Steps followed:
Download Google test (gtest-1.7.0)
Compile gtest into a static library
Create a unit test project
Make a test case
The above setup is working fine and able to test the function, if defined in header file of given project. But if the function is defined in Source.cpp file, as written above then compilation is failing with the below linking errors:
Error LNK2019 unresolved external symbol "int __cdecl fun2(int)" (?fun2##YAHH#Z) referenced in function "private: virtual void __thiscall a_b_Test::TestBody(void)" (?TestBody#a_b_Test##EAEXXZ)
Error LNK1120 1 unresolved externals
Please let me know how resolve this error or how to do the unit testing of a function defined in cpp file using gtest?
I have followed the steps given on this link.

Unable to build the Test Project that used Google test

I built the google test project.
I added the include directories in General -> Addition Include Directories.
I added the library directory to the Linker -> Additional Library directories
I added gtest_maind.lib and gtestd.lib to Linker -> Input -> Additional Dependencies
I followed instructions in an another chain to change the Code Generation -> Runtime Library to Multi-threaded Debug (/MTd)
Note: I am using Visual Studio 2010
In spite of doing all the above I still get the following link error
Error 1 error LNK2019: unresolved external symbol "public: __thiscall
Utility::~Utility(void)" (??1Utility##QAE#XZ) referenced in function "private: virtual
void __thiscall UtiltyTest_test1_Test::TestBody(void)" (?
TestBody#UtiltyTest_test1_Test##EAEXXZ) C:\Users\<username>\Documents\Visual Studio
2010\Projects\Calc\CalTest\UtilityTest.obj
Am I missing something here. Could someone help me solve this error.
Are you trying to test any .c files?
Because if you are you should try this:
#ifdef __cplusplus
extern "C" {
#endif
void cFunctionCalledFromCppFile();
#ifdef __cplusplus
}
#endif

Unresolved externals when compiling unit tests for Visual C++ 2012

I want to create unit tests for a Visual C++ project. I tried following these MSDN instructions. I've found pages where they differentiate between unmanaged/mixed/pure code, but I don't fully understand those concepts. My code doesn't use .NET and would likely compile under MinGW with a few code adjustments.
My main project builds an executable, so I followed the steps under To reference exported functions from the test project. For starters I got different project options:
I went with Native Unit Test Project. I added a reference to my main project and I set Include Directories to $(SolutionDir)\Cubes;$(IncludePath). I wrote my code and got this when compiling:
1>Creating library C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.lib and object C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.exp
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: __thiscall Room::Room(void)" (??0Room##QAE#XZ) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency#LayoutTest#CubesTest##QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: __thiscall Layout::Layout(class Room *,int)" (??0Layout##QAE#PAVRoom##H#Z) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency#LayoutTest#CubesTest##QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Layout::add(int,int,class Room *)" (?add#Layout##QAEXHHPAVRoom###Z) referenced in function "public: void __thiscall CubesTest::LayoutTest::NumOfRoomsConsistency(void)" (?NumOfRoomsConsistency#LayoutTest#CubesTest##QAEXXZ)
1>LayoutTest.obj : error LNK2019: unresolved external symbol "public: void __thiscall Layout::clear(int,int,bool)" (?clear#Layout##QAEXHH_N#Z) referenced in function __catch$?NumOfRoomsConsistency#LayoutTest#CubesTest##QAEXXZ$0
1>C:\Users\Pieter\Dropbox\Unief\TTUI\TTUIproject\Cubes\Debug\CubesTest.dll : fatal error LNK1120: 4 unresolved externals
If I'm not mistaken, this means that the compiler finds the header files, but not the source files. What am I missing?
Here is a step-by-step description on how to add an EXE as an unit-test target.
The key point is to "export" the functions/classes you want to test... You can download the complete sample here: http://blog.kalmbachnet.de/files/CPP_UnitTestApp.zip (I did not change any project settings, so all changes you can see in the source-code; of course, some parts can be made in the project settings).
Create a Win32 Application (Console or MFC or Windows, does not matter); I created a console project called CPP_UnitTestApp:
Add a function you want to test (you can also add classes). For example:
int Plus1(int i)
{
return i+1;
}
Add a header file for the functions you want to test: CPP_UnitTestApp.h
Put the declaration of the methods into the header file, and also export these functions!
#pragma once
#ifdef EXPORT_TEST_FUNCTIONS
#define MY_CPP_UNITTESTAPP_EXPORT __declspec(dllexport)
#else
#define MY_CPP_UNITTESTAPP_EXPORT
#endif
MY_CPP_UNITTESTAPP_EXPORT int Plus1(int i);
Include this header file in the main-cpp (here CPP_UnitTestApp.cpp) and define the EXPORT_TEST_FUNCTIONS before including the header:
#define EXPORT_TEST_FUNCTIONS
#include "CPP_UnitTestApp.h"
Now add a new project (Native unit test project: UnitTest1)
Include the header and the lib to the "unittest1.cpp" file (adopt the paths as you want):
#include "..\CPP_UnitTestApp.h"
#pragma comment(lib, "../Debug/CPP_UnitTestApp.lib")
Go to the project settings of the test project add add a reference to the "UnitTest1" project (Project|Properties|Common Properties|Add New Reference...: Select under "Projects" the "CPP_UnitTestApp"-Project)
Create the unit test function:
TEST_METHOD(TestMethod1)
{
int res = Plus1(12);
Assert::AreEqual(13, res);
}
Run your unit test ;)
As you can see, the main point was to export the function declaration! This is done via __declspec(dllexport) even if it is an EXE.
As I said, the demo project can be downloaded here: http://blog.kalmbachnet.de/files/CPP_UnitTestApp.zip

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