I ran into a behavior today that I don't completely understand. I jump right into a minimal code example and will explain along the way.
I have 2 Projects: A static c++ library and a console application.
Static Lib Project:
Library.h
#pragma once
namespace foo
{
int testFunc();
class StaticLibClass
{
public:
static int testMemberFunc();
};
}
Library.cpp
#include "Library.h"
using namespace foo;
// just some functions that don't do much
int testFunc()
{
return 10;
}
int StaticLibClass::testMemberFunc()
{
return 11;
}
Console Application Project:
main.cpp
#include "library.h"
using namespace foo;
void main()
{
// calling this function reslts in LNK2019: unresolved external symbol...
testFunc();
// this function works just fine
StaticLibClass::testMemberFunc();
}
As you can see the static member function of a class works just fine. The single testFunc however results in a linker error. Why is this?
The solution to the problem is to not use "using" in the Library.cpp file but also wrap it in the namespace like so:
Changes that fix the problem:
Library.cpp
#include "Library.h"
namespace foo
{
// just some functions that don't do much
int testFunc()
{
return 10;
}
int StaticLibClass::testMemberFunc()
{
return 11;
}
}