C++ templates not defined in header / linker error [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why can templates only be implemented in the header file?
I just came across something that I failed to understand.
I had a problem at the linking stage in the following case.
//header file
class A
{
template<class T>
std::weak_ptr<T> GetSomethingFromSomeWhere(const char* Id);
};
//cpp file
template<class T>
std::weak_ptr<T> A:GetSomethingFromSomeWhere(const char* id)
{
//A method with the right stuff inside and the right return statement
...
}
//Another class
class B
{
};
//main.cpp
int main ()
{
A a;
auto pB = a.GetSomethingFromSomeWhere<B>( "id" );
}
This didn't compile, during linking I have something of this kind :
Undefined symbols for architecture x86_64:
"std::__1::weak_ptr A::GetComponentFromName(char const*)", referenced from:
_main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I fixed it by defining the template method directly in the header file.
Should I always define template method in the header ? Why ?
I am on OSX and use clang++ with XCode if that can be of any help
Thanks

Template definition needs to be visible to the code using it. Otherwise linker errors will be generated.
There are different workarounds for situations like that:
Read This

Related

C++: Undefined symbols for architecture arm64 ... linker command failed [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed last month.
I have the following files,
my.h
extern int foo;
void print_foo();
void print(int);
my.cpp
#include <iostream>
#include "./headers/my.h"
void print_foo() {
std::cout << foo << '\n';
}
void print(int i) {
std::cout << i << '\n';
}
use.cpp
#include "./headers/my.h"
int main() {
foo = 7;
print_foo();
print(99);
}
building gives the following error,
/> g++ -W -std=c++11 -o output *.cpp
Undefined symbols for architecture arm64:
"_foo", referenced from:
print_foo() in my-e8b938.o
_main in use-318772.o
(maybe you meant: print_foo())
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Tried removing each definition and its references one by one and testing. It compiles fine when
void print(int)
and its references are alone. The other two defintions give a similar error shown above.
My environment: Mac M1 (Monterey), VSCode, g++ v.14
New to C++. What's going on? How do I correct?
extern int foo;
That does not define foo, it just declares it. You need exactly 1 .cpp file with int foo defined somewhere, and optionally initialized.

linker cannot find a C++ static member [duplicate]

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
When i write simple C++ code in X-code, it shows Linker Error.
Undefined symbols for architecture x86_64:
"Emp::id", referenced from:
Emp::Emp() in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
#include <iostream>
using namespace std;
class Emp
{
public:
static int id;
int sal;
Emp()
{
Emp::id =10; // When i comment this line its working fine.
};
};
int main(int argc, const char * argv[])
{
Emp Ram;
cout << Ram.sal ;
return 0;
}
You have declared id as a static variable. You then set it in every constructor call, which is probably not what you want to do.
For a 'fix', you can add the following line above main:
int Emp::id = 0;
However, you may not want that to be static. For more information on static class variables, see this page

C++ class static member initialisation [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
Closed 9 years ago.
I have the following code:
class employee {
public:
static int last_id;
...
};
int main() {
employee::last_id=0;
}
When i try to run it it gives the following error:
Undefined symbols for architecture x86_64:
"employee::last_id", referenced from:
_main in chap7-F3IpS1.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[Finished in 0.3s with exit code 1]
int employee::last_id=0;
int main() {
[...]
}
You only declared the static data member but not defined it. Write before main in the global namespace
int employee ::last_id;
It will be initialized by zero though your explicitly can specify the initializer.

Unable to initialize static map in class [duplicate]

This question already has answers here:
Undefined reference to static class member
(9 answers)
static variable link error [duplicate]
(2 answers)
boost::function static member variable
(1 answer)
Closed 9 years ago.
I am trying to initialise a static map of
map<string, int> in my program as follows:
testApp.h
class testApp(){
public:
void setup();
void update();
void renew();
static map<string, int> _someMap;
};
testApp.cpp
testApp::setup(){
_someMap["something"] = 1;
_someMap["something2"] = 2;
cout<<_someMap["something"]<<"\n";
}
I don't want to use boost for this short use of map and add source dependency for my code. I am not on C++11 and I don't have the constructor here in the program since the class is some framework's class. I am on Xcode and on doing the above in .cpp, I get the following error:
Undefined symbols for architecture i386:
"testApp::mapppp", referenced from:
testApp::setup() in testApp.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
-- >Additionally, let's say my map is private, for which I tried doing this in my class:
...
private:
static someVariable;
static void someFunction();
.cpp
testApp::setup(){
someFunction();
}
Error:
Undefined symbols for architecture i386:
"testApp::_someMap", referenced from:
testApp::someFunction() in testApp.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
You've declared the variable in the class definition, but it looks like you haven't defined it. Every static variable needs to be defined in exactly one translation unit. So add a definition to your source file:
map<string, int> testMap::_someMap;
If you like (and if you can't use a C++11 initialiser), you could avoid having to call the setup function by initialising the map from the result of a function instead:
map<string, int> make_map() {
map<string, int> map;
map["something"] = 1;
map["something2"] = 2;
return map;
}
map<string, int> testMap::_someMap = make_map();

Undefined symbols for architecture x86_64:

interface:
class rmKeyControl {
static map<char, function<char(char)>> sm_function_list;
public:
static bool addKeyAction(char, function<char(char)>);
};
implementation:
bool rmKeyControl::addKeyAction(char key, function<char(char)> func) {
if (!sm_function_list.count(key)) {
sm_function_list.insert(pair<char, function<char(char)>>(key, func));
return true;
} return false;
}
The full error message is:
Undefined symbols for architecture x86_64:
"control::rmKeyControl::sm_function_list", referenced from:
control::rmKeyControl::addKeyAction(char, std::__1::function) in rm_KeyControl.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
This seems to be a standard linker error for Xcode 4, but it seems to occur for all sorts of reasons, and it never elaborates. This error seems to indicate the presence of binary instructions that don't work on the x86_64 architecture, but that doesn't make sense in this context. Why am I getting this error?
Edit: I forgot to mention that rmKeyControl is in namespace control. I am using namespace control; in the implementation, although you cannot see it.
Static member is just declaration. Define it in the implementation/source file like-
// include interface header and then do -
map<char, function<char(char)>> rmKeyControl::sm_function_list;