How to solve LNK2019 error: unresolved external character - function?

I get this error, but I donโ€™t know how to fix it.

I am using Visual Studio 2013. I made the solution name MyProjectTest. This is the structure of my test solution:

The structure

-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 new at this; it is a simple program and it works without errors. I read on the Internet and was interested in unit test, so I created a test project:

File> Create> Project ...> Installed> Templates> Visual C ++> Test> Native Unit Test Project>

Name: UnitTest1 Solution: Add to solution Then the location automatically switches to the path of the current open solution This is the folder structure of the solution:

Folder structure

I just edited the unittest1.cpp file:

 #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 character. I know that the implementation of the multiple function is missing. I tried to delete the function.cpp file, and I replaced the declaration with a definition, and it started. But writing a declaration and definition in the same file is not recommended. How can I fix this error without doing this? Should I replace #include "../MyProjectTest/function.cpp" with unittest.cpp? (I am not very good at English.)

+55
c ++ testing error-handling lnk2019
Nov 10 '13 at 4:47
source share
9 answers

One option is to include function.cpp in your UnitTest1 project, but this may not be the most ideal solution structure. The short answer to your problem is that when creating the UnitTest1 project UnitTest1 compiler and linker have no idea that function.cpp exists, and also does not have any binding containing the definition of multiple . The way to fix this is using link libraries.

Since your unit tests are in a different project, I assume that you intend to make this project a standalone unit testing program. Using the functions you are testing located in another project, you can build this project either in a dynamically or in a statically linked library. Static libraries are linked to other programs at build time and have a .lib extension, and dynamic libraries are linked at runtime and have a .dll extension. For my answer, I prefer static libraries.

You can turn your first program into a static library by changing it in the project properties. On the tab "General" there should be an option for which the project should be installed in an executable file ( .exe ). You can change this to .lib . The .lib file will be created in the same place as the .exe .

In the UnitTest1 project UnitTest1 you can go to its properties, and on the Linker tab in the Additional library catalogs category, add the path to create MyProjectTest . Then, for additional dependencies, on the Linker - Input tab, add the name of your static library, most likely MyProjectTest.lib .

This should allow your project to build. Note that by doing this, MyProjectTest will not be a stand-alone executable unless you change your build properties as needed, which would be less than ideal.

+47
Nov 10 '13 at 5:47
source share

In the Visual Studio decision tree, right-click on the project "UnitTest1", then "Add" โ†’ "Existing Element" โ†’ select the file ../ MyProjectTest / function.cpp

+31
Nov 10 '13 at 5:09
source share

Since I want my project to compile into a standalone EXE, I linked the UnitTest project to the function.obj file generated from function.cpp, and it works. Right-click on the project "UnitTest1"> "Configuration Properties"> "Linker"> "Enter"> "Additional Dependencies"> "Add \ MyProjectTest \ Debug \ function.obj"

+10
Jun 05 '15 at 13:17
source share

it turned out that I used .c files with .cpp files. renaming .c to .cpp solved my problem.

+7
Jun 04 '16 at 19:39
source share

I just ran into this problem in Visual Studio 2013. Apparently, now there are two projects missing in one solution and dependency installation. You need to add a link to the project between them. For this:

  • Right-click on a project to find a solution.
  • Click Add => Links ...
  • Click "Add New Link"
  • Check the boxes for the projects on which this project relies.
  • Click OK
+6
Dec 22 '15 at 21:07
source share

Another way to get this linker error (as I was) is to export an instance of the class from dll, but they did not declare this class 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, although basically I exported only an instance of the Book class called book above, I had to declare the book class as the export / import class, otherwise calling book.WordCount() in another dll caused a communication error.

+2
Oct 05 '17 at 19:21
source share

I just discovered that LNK2019 occurs at compile time in Visual Studio 2015, if you forget to provide a definition of the declared function inside the class.

The linker error was very cryptic, but I narrowed down what was missing by reading this error and providing a definition outside the class to clear it up.

+1
Oct 10 '17 at 18:10
source share

It works for me if I add this line below to the .vcxproj in the itemGroup cpp file that is associated with the header file.

 <ClCompile Include="file.cpp" /> 
0
Aug 29 '17 at 13:23
source share

This happened to me, so I decided to share my solution as easily:

Check the character set of both projects in Configuration Properties โ†’ General โ†’ Character Set

My UnitTest project used the default character set Multi-Byte , while my libraries used Unicode .
My function used the TCHAR parameter. As a result, in my lib, my TCHAR was converted to WCHAR , but it was char * my UnitTest: the character was different because the parameters were not the same after all.

0
Feb 28 '18 at 15:44
source share



All Articles