How can I solve the error LNK2019: unresolved external symbol - function?
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.
LNK2019: unresolved external symbol error in Visual Studio C++
'unresolved external symbol'
means that you're not linking with required library.
Go to Properties -> Linker -> Additional Library dependencies
and add path to OpenCV libs.
How can I resolve error LNK2019: unresolved external symbol?
It happened to me more than once that I thought symbol XXX
(i.e. ?close@CppSQLite3DB@@QAEXXZ
) was in the import lib, while the actual symbol was __impXXX
(i.e. __imp?close@CppSQLite3DB@@QAEXXZ
).
The reason for the linker error is then to be found in the compilation step: the compiler will generate the ?close@CppSQLite3DB@@QAEXXZ
symbol to be imported, where it should generate __imp?close@CppSQLite3DB@@QAEXXZ
. This often means that the function declaration itself didn't have __declspec( dllimport )
. Which may be caused by some preprocessor symbol not being defined. Or the __declspec
not being there at all...
Unresolved external symbol in object files
This error often means that some function has a declaration, but not a definition.
Example:
// A.hpp
class A
{
public:
void myFunc(); // Function declaration
};
// A.cpp
// Function definition
void A::myFunc()
{
// do stuff
}
In your case, the definition cannot be found. The issue could be that you are including a header file, which brings in some function declarations, but you either:
- do not define the functions in your cpp file (if you wrote this code yourself)
- do not include the lib/dll file that contains the definitions
A common mistake is that you define a function as a standalone and forget the class selector, e.g. A::
, in your .cpp file:
Wrong: void myFunc() { /* do stuff */ }
Right: void A::myFunc() { /* do stuff */ }
How To Fix Error LNK2019 : unresolved external symbol ...
I suggest you go through a tutorial to brush up on your array basics. http://www.cplusplus.com/doc/tutorial/arrays/
You syntax for calling the functions is incorrect. Also, your functions prototypes do not match.
float data(float x);
and
float data(float x[5][3])
{
Also, when calling the function, don't specify the dimensions.
float someFloat = data( a );
Related Topics
What Is the Best Modern C++ Approach to Construct and Manipulate a 2D Array
Defining Global Constant in C++
Move or Named Return Value Optimization (Nrvo)
Initialise Eigen::Vector with Std::Vector
What Is Wrong with Using Inline Functions
Should a Move Constructor Take a Const or Non-Const Rvalue Reference
Empirically Determine Value Category of C++11 Expression
Area of Rectangle-Rectangle Intersection
How to Set Breakpoint at the Very Beginning of Program Execution
Void_T "Can Implement Concepts"
What Is a Void Pointer and What Is a Null Pointer
What Is Return Type of Assignment Operator
Operator= and Functions That Are Not Inherited in C++
How to Test a String for Letters Only
Why Is It Not Good to Use Recursive Inheritance for Std::Tuple Implementations