Understanding a "Undefined symbols for architecture" error - c++

Ok, I'm new to c++ so I'm trying to understand what information I can get from the error message.
Here is the error message
Undefined symbols for architecture x86_64:
"PieceClothing::PieceClothing(int)", referenced from:
ClothesInventory::getPieceOfClothing(long) in ClothesInventory.o
ClothesInventory::insertIntocloset(std::basic_string, std::allocator >)in ClothesInventory.o
"PieceClothing::PieceClothing()", referenced from:
ClothesInventory::ClothesInventory()in ClothesInventory.o
ClothesInventory::ClothesInventory(std::basic_string, std::allocator >)in ClothesInventory.o
std::map, std::allocator > >::operator[](long const&)in ClothesInventory.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
Here is what I understand:
- There are two errors;
- One that has to do with getPieceOfClothing and insertIntocloset;
- Other in the the constructors maybe about a map and/or iterator I have there.
Just to clarify, I'm not attaching the code because the point of the question is to understand all the information I can get just from the message.
Thanks for any help.

The errors are actually about the constructors:
PieceClothing::PieceClothing(int)
PieceClothing::PieceClothing()
and they're saying no symbols were found for them. This is usually a sign of either:
they weren't implemented
they're implemented but the file in which the implementation lies is not compiled
you're referencing them from a different module that doesn't link with the module that defines them
The other details in the error list just state where the constructors are called. For example, if you have:
ClothesInventory::getPieceOfClothing(long)
{
PieceClothing p;
}
you're referencing the constructor because you attempt to create an object of that type.
How this works can be broken down in 2 parts:
1) The compiler checks the header file that define the class and sees whether a default constructor is available. It find the constructor so it gives its ok.
2) The linker comes into action. It looks for symbols that match your calls in object files and referenced libraries. This is where it goes wrong for you.

The message is telling you that it cannot find a definition for the declared constructors PieceClothing::PieceClothing(int) and PieceClothing::PieceClothing() so you need to check whether you've written them and, if so, whether the object file that contains them forms part of the link.
If your linker output is verbose, it should show you which object files are being linked.

Related

Undefined symbols for io_context: linking error for the latest boost library

My code used to copmile well previously, until boost library got updated with changes in asio.
In my code, I define a variable: boost::shared_ptr<Face> face(boost::make_shared<ThreadsafeFace>(io_service)); which, as can be seen, takes io_service for the constructor. Face and ThreadsafeFace are a library classes, my app links to.
The problem occurs at linking stage of my binary, where I get undefined symbols error:
Undefined symbols for architecture x86_64:
"ndn::ThreadsafeFace::ThreadsafeFace(boost::asio::io_context&)", referenced from:
boost::detail::sp_if_not_array<ndn::ThreadsafeFace>::type boost::make_shared<ndn::ThreadsafeFace, boost::asio::io_context&>(boost::asio::io_context&&&) in ndnrtc_client-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)
As can be seen, linker can't find a constructor for ThreadsafeFace that takes boost::asio::io_context& argument. And it won't -- because library does not provide one. The only one library does provide -- is with io_service argument.
Now, I don't quite understand, where does this constructor definition come from, as neither my code, nor library's code have this definition.
This makes me think that with new boost (I'm linking against 1.67 using homebrew, macOS), io_service gets replaced by io_context automatically (during preprocessing?), thus leading to the problem.
I tried providing -DBOOST_ASIO_ENABLE_OLD_SERVICES when compiling my code, but it didn't help either.
Shall I downgrade boost version until library gets updated?
UPDATE
I've ran clang for preprocessing (clang++ -E ...) and found this in the output:
# 21 "/usr/local/include/boost/asio/io_service.hpp" 2 3
namespace boost {
namespace asio {
typedef io_context io_service;
}
}
Which confirms that all io_service variables will in fact be io_context and guarantee headaches.
"Which confirms that all io_service variables will in fact be io_context and guarantee headaches"
In fact that guarantees no headaches. Typedefs are aliases: they're exactly the same. So io_service becomes just another way to refer to the same type, even if the spelling happens to be different in some spots. This is actually what you need.
Reading the message:
Undefined symbols for architecture x86_64:
"ndn::ThreadsafeFace::ThreadsafeFace(boost::asio::io_context&)", referenced from:
boost::detail::sp_if_not_array::type boost::make_shared(boost::asio::io_context&&&) in ndnrtc_client-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)
This tells you that ndn::ThreadsafeFace does provide the required constructor, because it's referenced from your code. If it weren't provided, it would have been a compile error, not a link error.
So your problem is different. You either lack a linker input, or the library object you link against was compiled /differently/ in such a way that it doesn't provide the definition of the constructor that is declared when you include the header that declares ThreadsafeFace.
Usually, this happens if namespaces change, or when you (ab)used compiler defines to change the meaning of the code (did you perhaps mess around with something like #define io_context io_service? Because that is a recipe for headaches).
Note linker errors can even result when you use different compiler versions/flags when compiling your code versus when compiling the library.
For far more troubleshooting tips see: What is an undefined reference/unresolved external symbol error and how do I fix it?

At a loss ... Can't figure out why XCode 6.1 won't compile my C++ program

Although XCode is not flagging any errors before compile time, it brings up 4 when I actually compile it. They are
Undefined symbols for architecture i386:
"HtmlProcessor::HtmlProcessor()", referenced from:
_main in main.o
"HtmlProcessor::~HtmlProcessor()", referenced from:
_main in main.o
"DocTree::_hp", referenced from:
DocTree::setTree(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&) in HtmlProcessor.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I have searched the web high and low for answers. Most of them mention changing the Architectures settings. Right now I have
Architectures: Universal (32/64-bit Intel) (x86_64, i386)
Base SDK: Latest OFX (OS X 10.9)
Build Active Architecture Only: No
Supported Platforms: OSX
Valid Architectures: i386
and I've fiddled around with everything to try and get my damn program to compile. I don't even care what the target architecture is ..... I'm making this program for my own amusement and want it to run on my machine, a MacBook Pro running OS X 10.9.4. I just want this damn console program to work. You would think that XCode would have default configurations for your program to run on your machine.
Here are the source files: https://www.dropbox.com/sh/yu7tblwj14eoq1l/AAC8PfDi6la3CjE167Iz1C0da?dl=0
Nobel Prize to the Stack Overflow guru who bails me out of this one.
You declared a static class member, but you did not define it in any module:
class DocTree {
//...
static HtmlProcessor _hp;
//...
};
This needs to have this in one and only one module:
HtmlProcessor DocTree::_hp;
You are declaring the functions in the header file but do not define (implement) them in the .cpp file. I couldn't find the definition for the constructor and destructor in the .cpp file, although you have the declaration in the header. The linker then complains as it is not able to find the needed object code to create the instance of HtmlProcessor.
So make sure that you either declare the ctor as empty, like
HtmlProcessor(){}
or remove the declaration altogether,
or use =default (if you use C++11).
Same for the static declaration of DocTree::_hp;, you need to define it somewhere.

Linker Command Fail in Xcode with Pure Virtual Objects

I'm a n00b so correct me on anything.
I've been working on this for a couple days and have done research but can't seem to solve the issue. This is for a programming class that mainly uses Visual Studio and many of my fellow classmates didn't have a problem. Although, I'm on Xcode so maybe it has something to do with that. Basically, I'm creating a Pure Virtual objects called Geometric_Object with child classes Circle.h and Rectangle.h, however when I run the code I get the following error:
Undefined symbols for architecture x86_64:
"GeometricObject::GeometricObject()", referenced from:
Circle::Circle() in main.o
Rectangle::Rectangle() 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'm beyond lost. Because I don't have enough reputation points I can't post all the links so I had to compress them as one on Dropbox. Hopefully someone can bypass this for me so nobody is afraid to unzip the contents.
https://dl.dropboxusercontent.com/u/82764116/Xcode.zip
The problem is exactly what the error says. You haven't defined GeometricObject::GeometricObject(). You also haven't defined many other methods in GeometricObject, like the destructor, getColor, setColor etc.
I'm not sure where you think the definitions for these functions are, but they aren't in the code you've linked to.
You have defined (for instance) Rectangle::Rectangle(). Just define GeometricObject::GeometricObject() in the same way (and all the other missing definitions).

How can I resolve single symbol link error when dynamically linking XCode project to lib4cxx library?

I'm writing in C++ under XCode 4.6 on Mountain Lion. I'm trying to add and use the Apache log4cxx library. I installed the library this morning via Brew. I'm linking against liblog4cxx.dylib. I'm getting a link error that just one symbol can't be found:
Undefined symbols for architecture x86_64:
"log4cxx::Logger::forcedLog(log4cxx::helpers::ObjectPtrT
const&, std::__1::basic_string,
std::__1::allocator > const&, log4cxx::spi::LocationInfo const&)
const", referenced from:
I know it's finding the library file because if I remove it, I get lots more undefined symbol errors relating to log4cxx.
relevant code is basically:
#include <log4cxx/logger.h>
static LoggerPtr logger(log4cxx::Logger::getLogger("foo.bar.Baz"));
void foo(int p1, int p2)
{
LOG4CXX_WARN(logger, "blah blah blah");
}
Creating the logger object inside the function, either as static or not, doesn't change the behavior. Also, linking with the static library, with or without defining LOG4CXX_STATIC in my project, does not change the behavior.
Looking at the macro I'm calling, I see that this symbol is the actual method that performs the log operation. If take out the logging call but leave in the code that defines the logger object, the code links fine as you might expect.
What do I need to do to have this last symbol resolve?
TIA!
I traced my issue down to compiling the library in a non C++11 compiler, but then my target project was a C++11 compiler.
I was able to compile log4cxx in a C+11 compiler by viewing the changes to log4cxx in the development git repo, which mainly consisted of inserting static_casts, as in this update:
http://apache-logging.6191.n7.nabble.com/C-11-does-not-allow-char-literals-with-highest-bit-set-unless-cast-td34908.html
I suppose the few incompatible routines came up undefined, which is why we were getting confused with only a few seemingly random undefines. (Or I was anyway)

Portaudio C++ bindings: symbol not found in MemFunCallbackStream

Recently I discovered there are C++ bindings for PortAudio, so to keep things nice and Object-Oriented I'm converting from the normal PortAudio C-functions to the C++ bindings. However, I ran into trouble with the callback function.
I try to create a stream in the following way:
stream = new portaudio::MemFunCallbackStream<OutputChannel>(params, *this, &OutputChannel::output);
This call is made in a method of the OutputChannel-class. This same class contains the method which should function as a callback, hence the 'this' I'm passing to the MemFunCallbackStream-method.
However when building, the linker gives an error:
Undefined symbols for architecture x86_64:
"_Pa_OpenStream", referenced from:
portaudio::MemFunCallbackStream<OutputChannel>::open(portaudio::StreamParameters const&)in outputchannel.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
I'm sure the PortAudio library is loaded, since some other (diagnostic) methods do work.
What may be causing this error?
Argh, I feel stupid. Somehow, the library was no longer linked to my application, although it was before. So the error was due to a missing library after all. Problem solved.