Reputation: 24041
I have a native C++ unit test project that is throwing a LNK2019 error for every function call in the project under test. Surprisingly though, IntelliSense works just fine!
The project under test is a static library (.lib) comprised of a single public static function (type and member names have been changed to protect the innocent):
Type.h
#pragma once
#include <string>
using namespace std;
namespace N
{
enum class ResultCode { Undefined, A, B, C};
class MyType
{
public:
static void GetResult(string id, string metadata, ResultCode result);
};
}
Type.cpp
#include "pch.h"
#include "Type.h"
namespace N
{
void MyType::GetResult(string id, string metadata, N::ResultCode result)
{
// implementation
}
}
My unit test project (a .dll) does not use a header file for the tests. I'm using the google test framework. Here is the source:
Test.cpp
#include <pch.h>
#include <gtest/gtest.h>
#include <gtesthelpers/gtesthelpers.h>
#include <MyType.h>
class MyTypeUnitTests : public testing::Test {};
TEST(MyTypeUnitTests, Foo)
{
std::string metadata;
N::ResultCode result = N::ResultCode::Undefined;
N::MyType::GetResult("1234", metadata, result);
ASSERT_TRUE(result == N::ResultCode::A);
}
When I compile MyType, everything is just fine. And when I wrote Test, IntelliSense provided me with the signature for GetResult. But when I compile:
Test.obj : error LNK2019: unresolved external symbol "public: static void __cdecl N:MyType:GetResult(class std::basic_string,class std::allocator >,class std::basic_string,class std::allocator >, enum N::ResultCode)" ... referenced in function ...
I have modified the test project properties so that:
I have also confirmed that under Project Dependencies for the test project, the project under test is checked. I also used undname to verify that the function name specified in the error matches the name in the .h and .cpp.
Finally I created a new static parameterless function in MyType, and tried calling that from the test (so as to rule out a problem with the enum parameter) but no dice. I have followed the instructions on the MSDN page that I linked to above, and I'm out of ideas.
How can I go about resolving this?
EDIT: Showing the namespace block in the cpp.
Upvotes: 0
Views: 1015
Reputation: 24041
I resolved this by adding a reference to my library in the test project properties at Configuration Properties > Linker > Input > Additional Dependencies. No path, just "Type.lib".
Upvotes: 0
Reputation: 1246
This is your problem:
using namespace N;
void MyType::GetResult(string id, string metadata, N::ResultCode result)
{
// implementation
}
You should actually wrap the definition into a namespace:
namespace N
{
void MyType::GetResult(string id, string metadata, N::ResultCode result)
{
// implementation
}
}
Upvotes: 1