I get this error, but I don't know how to fix it.
I'm using Visual Studio 2013. I made the solution name MyProjectTest This is the structure of my test solution:
-function.h
#ifndef MY_FUNCTION_H
#define MY_FUNCTION_H
int multiple(int x, int y);
#endif
-function.cpp
#include "function.h"
int multiple(int x, int y){
return x*y;
}
-main.cpp
#include <iostream>
#include <cstdlib>
#include "function.h"
using namespace std;
int main(){
int a, b;
cin >> a >> b;
cout << multiple(a, b) << endl;
system("pause");
return 0;
}
I'm a beginner; this is a simple program and it runs without error. I read on the Internet and became interested in the unit test, so I created a test project:
Menu File ? New ? Project... ? Installed ? Templates ? Visual C++ ? Test ? Native Unit Test Project ?
Name: UnitTest1
Solution: Add to solution
Then the location auto-switched to the path of the current open solution.
This is the folder structure of the solution:
I only edited file unittest1.cpp:
#include "stdafx.h"
#include "CppUnitTest.h"
#include "../MyProjectTest/function.h"
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(TestEqual)
{
Assert::AreEqual(multiple(2, 3), 6);
// TODO: Your test code here
}
};
}
But I get:
error LNK2019: unresolved external symbol.
I know that the implementation of function multiple is missing. I tried to delete the function.cpp file and I replaced the declaration with the definition, and it ran. But writing both declaration and definition in the same file is not recommended.
How can I fix this error without doing that? Should I replace it with #include "../MyProjectTest/function.cpp"
in file unittest.cpp?
This question is related to
c++
testing
error-handling
lnk2019
In the Visual Studio solution tree, right click on the project 'UnitTest1', and then Add ? Existing item ? choose the file ../MyProjectTest/function.cpp.
Another way you can get this linker error (as I was) is if you are exporting an instance of a class from a DLL file, but have not declared that class itself as import/export.
#ifdef MYDLL_EXPORTS
#define DLLEXPORT __declspec(dllexport)
#else
#define DLLEXPORT __declspec(dllimport)
#endif
class DLLEXPORT Book // <--- This class must also be declared as export/import
{
public:
Book();
~Book();
int WordCount();
};
DLLEXPORT extern Book book; // <-- This is what I really wanted, to export book object
So even though primarily I was exporting just an instance of the Book class called book
above, I had to declare the Book
class as export/import class as well otherwise calling book.WordCount()
in the other DLL file was causing a link error.
Check the character set of both projects in Configuration Properties ? General ? Character Set.
My UnitTest project was using the default character set Multi-Byte while my libraries were in Unicode.
My function was using a TCHAR as a parameter.
As a result, in my library my TCHAR was transformed into a WCHAR, but it was a char* in my UnitTest: the symbol was different because the parameters were really not the same in the end.
It turned out I was using .c files with .cpp files. Renaming .c to .cpp solved my problem.
I just discovered that LNK2019
occurs during compilation in Visual Studio 2015 if forgetting to provide a definition for a declared function inside a class.
The linker error was highly cryptic, but I narrowed it down to what was missing by reading through the error and provided the definition outside the class to clear this up.
In my case, set the cpp file to "C/C++ compiler" in "property"->"general", resolve the LNK2019 error.
For me it works if I add this line below in .vcxproj
in the itemGroup
cpp file, which is connected to the header file.
<ClCompile Include="file.cpp" />
Since I want my project to compile to a stand-alone EXE file, I linked the UnitTest project to the function.obj file generated from function.cpp and it works.
Right click on the 'UnitTest1' project ? Configuration Properties ? Linker ? Input ? Additional Dependencies ? add "..\MyProjectTest\Debug\function.obj".
In Visual Studio 2017 if you want to test public members, simply put your real project and test project in the same solution, and add a reference to your real project in the test project.
See C++ Unit Testing in Visual Studio from the MSDN blog for more details. You can also check Write unit tests for C/C++ in Visual Studio as well as Use the Microsoft Unit Testing Framework for C++ in Visual Studio, the latter being if you need to test non public members and need to put the tests in the same project as your real code.
Note that things you want to test will need to be exported using __declspec(dllexport)
. See Exporting from a DLL Using __declspec(dllexport) for more details.
I just ran into this problem in Visual Studio 2013. Apparently now, having two projects in the same solution and setting the the dependencies is not enough. You need to add a project reference between them. To do that:
Source: Stackoverflow.com