Template build error: Undefined symbols for architecture x86_64: - c++

I am trying to use a template class and when I compile it in one file in LWS it works:
(Link is dead)
~http://liveworkspace.org/code/a9c412a7e683439dfa35a9363749369d~
But when I try to compile it made-up of 3 files,
stack.h lines 4 to 21
stack.cpp lines 24 to 48
main.cpp lines 49 to end
When I try to compile those 3 files I get
Undefined symbols for architecture x86_64:
"Stack2<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::push(Node**, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)", referenced from:
_main in ccCoizCT.o
"Stack2<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::top(Node*&)", referenced from:
_main in ccCoizCT.o
"Stack2<std::basic_string<char, std::char_traits<char>, std::allocator<char> > >::pop(Node*&)", referenced from:
_main in ccCoizCT.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Yes I have included stack.h in a stack.cpp and main.cpp files

Sounds like you need to place the template definitions of stack back in the header file. Templates form a plan for code generation, so if the compiler can't see the entire template definition and only sees the declaration, code for that specific instantiations of the template will not be generated. It will simply trust the declaration and expect that at link time there exists an objects file with the instantiations of those templates. The solution to this is 1) keep the template definitions in the header file or 2) pre-generate the required definitions so the linker can find them at link time.
See here:
Template issue causes linker error (C++)

Related

clang++ linker undefined symbols for architecture x86_64

$clang++ main.cpp -o out
Got following error, the same error happens when changing to g++. I have tested on some simple simple c++ code, the command works fine. So it the problem in that PNG class file? However, the same files worked on my MacOS before, but suddenly failed today.
Undefined symbols for architecture x86_64:
"PNG::writeToFile(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in main-f6a06a.o
"PNG::PNG(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&)", referenced from:
_main in main-f6a06a.o
"PNG::PNG(unsigned long, unsigned long)", referenced from:
_main in main-f6a06a.o
"PNG::~PNG()", referenced from:
_main in main-f6a06a.o
"PNG::operator()(unsigned long, unsigned long)", referenced from:
_main in main-f6a06a.o
"PNG::width() const", referenced from:
_main in main-f6a06a.o
"PNG::height() const", referenced from:
_main in main-f6a06a.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Did you miss providing static/shared to the linker to resolve the linker errors for the methods mentioned? Also note that clang++ is a different compiler compared to g++ and hence need the static/shared libraries compiled prior by the same compiler you are using (clang++).
Try:
clang++ main.cpp -o out -lpng
Here's why: You are not providing the library that contains the implementation of your PNG library (libpng most likely). You need -lpng added to your compiler, so that it links with the relevant library. It may not be exactly -lpng in your particular case, but it's definitely a "missing library". Without knowing exactly what library you are trying to use (you didn't provide some source to "try the fix with").
As pointed out in the comment: you are using some kind of C++ wrapper on top, the above is probably not enough - but without knowing exactly which C++ wrapper on the png functionality you are actually using, it's hard to say what the command-line should look like.
Perhaps this

Error locating libraries when using gmp

So I have been writing a c++ program and in it it handles integers too large for the inbuilt long long. No problem, I'll just use a bignum library. So I get GMP, utter those three magic commands "./configure, make, make install" and start using it. I use #include "gmpxx.h" as specified in the docs for a c++ program. Compiling it, GPP finds an error in gmpxx.h as apparently gmp.h can't be located using angles and quotes should be used instead in the include statement in gmpxx.h. Upon changing the angles to quotes, however, I am surprised to discover that something has gone wrong, I think with 'make'. It says:
Undefined symbols for architecture x86_64:
"___gmpz_clear", referenced from:
__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::~__gmp_expr() in primality-3fd487.o
"___gmpz_cmp_si", referenced from:
__gmp_binary_equal::eval(__mpz_struct const*, long) in primality-3fd487.o
"___gmpz_init", referenced from:
__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::__gmp_expr() in primality-3fd487.o
"___gmpz_set_d", referenced from:
__gmp_expr<__mpz_struct [1], __mpz_struct [1]>::assign_d(double) in primality-3fd487.o
"___gmpz_tdiv_r_ui", referenced from:
__gmp_binary_modulus::eval(__mpz_struct*, __mpz_struct const*, long) in primality-3fd487.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does anyone know how to stop either of these errors occurring?

Linking Boost on iOS project. vtable for boost::filesystem::detail::utf8_codecvt_facet

I've managed to compile Swiften library for a iOS project, but now I'm having linking errors related to Boost library.
I've compiled Boost following these steps, the error I'm getting during linking is:
Undefined symbols for architecture arm64:
"vtable for boost::filesystem::detail::utf8_codecvt_facet", referenced from:
__GLOBAL__I_a in boost(path.o)
NOTE: a missing vtable usually means the first non-inline virtual member function has no definition.
ld: symbol(s) not found for architecture arm64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
I've read some articles which suggest including some compilation params, but not sure which params or where should I put them. I've also reached this thread with no solution. Anyone else faced this issue? Anyone succeeded compiling and running Swiften for iOS? Any tip on the right direction would be much appreciated.
UPDATE 1
It seems filesystem and program_options libs share utf8_codecvt_facet object, and one was overriding the other while building boost. Finally, I managed to compile the whole Swiften library, but when I try to use it I'm getting more linking errors :( I get this error when I declare a JID object on my main class:
Undefined symbols for architecture armv7:
"Swift::StringPrep::getPrepared(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
Swift::StringPrep::Profile)", referenced from:
Swift::JID::nameprepAndSetComponents(std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&,
std::__1::basic_string<char, std::__1::char_traits<char>,
std::__1::allocator<char> > const&, std::__1::basic_string<char,
std::__1::char_traits<char>, std::__1::allocator<char> > const&) in JID.o

C++ Multiple Classes Cause Architecture Error

I am working on a project that requires me to use a variety of classes.
For some reason, whenever I try compiling, I get an error that says:
Undefined symbols for architecture x86_64:
I originally was working on a class we can call User that had a declaration like this in user.h:
#ifndef USER_H
#define USER_H
...other include statements
class User
{
public:
User();
public and private members and functions
};
#endif
Definitions of the methods was housed in a separate file in the same directory, user.cpp.
Whenever I would try to compile, I would get an error saying there were undefined symbols for the constructor and destructor. I had both of these defined in the .cpp file, but I couldn't get the error to go away.
I include the User class in main with the following:
#include "user.h"
I managed to fix this by pasting all code from the user.cpp file into user.h, but now I am having issues with other classes whose code I am not allowed to modify.
Right now, I have a class called GMLReader in gmlreader.h with a method I am calling statically. However, even though I am importing it for my main, I am getting the following error code whenever I try to call it:
Undefined symbols for architecture x86_64:
"GMLReader::read(char const*, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::vector<std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&)", referenced from:
_main in cchnzOEh.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I am using the following to compile with sn.cpp housing my main:
g++ -g -Wall -o sn sn.cpp
Is there any reason an error like this would have occurred and how can I go about fixing it?
You need to compile and link user.cpp as well:
g++ -g -Wall -c user.cpp -o user.o
g++ -g -Wall -o sn sn.cpp user.o

C++ Undefined Symbol error in OSX10.7.4

The following call from the command line, clang++ vcfaltcount.cpp -o vcfac, produces the following error/warnings
>In file included from vcfaltcount.cpp:1:
./Variant.h:86:5: warning: control may reach end of non-void function [-Wreturn-type]
}
>Undefined symbols for architecture x86_64:
"split(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)", referenced from:
_main in ccGOdUhH.o
vcf::VariantCallFile::open(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccGOdUhH.o
"vcf::VariantCallFile::getNextVariant(vcf::Variant&)", referenced from:
_main in ccGOdUhH.o
"vcf::VariantCallFile::parseHeader()", referenced from:
vcf::VariantCallFile::openFile(std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)in ccGOdUhH.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
There are many other posts on "undefined symbols for such&such architecture," and I have attempted to use those suggestions to successfully compile.
I think the problem may arise from the header file, variant.h. In the API from which the source code came there was a corresponding variant.cpp file that defined the functions, which were only declared in variant.h. I moved those definitions into variant.h. Any thoughts on what I may be missing? I can provide the header if need be, but it is long. Thanks.