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?
A symbol is declared but not defined Unless i and g are defined in one of the files included in the build, the linker generates LNK2019. You can fix the errors by including the source code file that contains the definitions as part of the compilation. Alternatively, you can pass .
Answer. Unresolved external references occur when the symbol for a function or global variable is referenced in a program, but none of the object files or libraries specified in the link step contain a definition for that symbol.
Linker errors occur when g++ tries to combine all of your .o files into an executable file. Linker errors CANNOT be fixed by guarding header files or by changing which header files are included in your . cpp file. non-aggregate type -- classes and structs are generically called "aggregate" types.
The LNK1120 message comes last, and shows the unresolved symbol error count. You don't need to fix this error. This error goes away when you correct all of the LNK2001 and LNK2019 linker errors before it in the build output.
One option would be to include function.cpp
in your UnitTest1
project, but that may not be the most ideal solution structure. The short answer to your problem is that when building your UnitTest1
project, the compiler and linker have no idea that function.cpp
exists, and also have nothing to link that contains a definition of multiple
. A way to fix this is making use of linking libraries.
Since your unit tests are in a different project, I'm assuming your intention is to make that project a standalone unit-testing program. With the functions you are testing located in another project, it's possible to build that project to either a dynamically or statically linked library. Static libraries are linked to other programs at build time, and have the extension .lib
, and dynamic libraries are linked at runtime, and have the extension .dll
. For my answer I'll prefer static libraries.
You can turn your first program into a static library by changing it in the projects properties. There should be an option under the General tab where the project is set to build to an executable (.exe
). You can change this to .lib
. The .lib
file will build to the same place as the .exe
.
In your UnitTest1
project, you can go to its properties, and under the Linker tab in the category Additional Library Directories, add the path to which MyProjectTest
builds. Then, for Additional Dependencies under the Linker - Input tab, add the name of your static library, most likely MyProjectTest.lib
.
That should allow your project to build. Note that by doing this, MyProjectTest
will not be a standalone executable program unless you change its build properties as needed, which would be less than ideal.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With