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.
Related
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?
So I am using Xcode 6.1 on my Objective-C app.
My app uses the GameKit framework.
I tried to call external C++ methods from my GameViewController (which is Objective-C source.)
To make GameViewController.mm compile, I had to set the type to 'Objective-C++ Source' manually.
However, even though the source now compiles, it will fail to link with:
Undefined symbols for architecture x86_64:
"_OBJC_CLASS_$_GLKViewController", referenced from:
_OBJC_CLASS_$_GameViewController in GameViewController.o
"_OBJC_METACLASS_$_GLKViewController", referenced from:
_OBJC_METACLASS_$_GameViewController in GameViewController.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 can only get rid of the linker error by setting the type back to 'Objective-C Source' and removing the calls to the external C++ functions.
Why does the linker fail if my source is compiled as 'Objective-C++ Source' instead of 'Objective-C Source' type?
Note that I properly link against GameKit in the Build-phases.
OK.
It turns out to be a weird one, but:
I was linking to GameKit, and not GLKit.
The fix: link to GLKit as well.
Now, the strange part is that not linking GLKit causes no problems if the view controller is built as Objective-C source. It does cause problems if built as Objective-C++ source.
Strange, but true.
I have taken this example directly from a book on C++ (shortened it so it's easier to see what the problem is).
My class won't compile with g++. The class is:
class stack{
private:
int count;
public:
void init(void);
};
inline void stack::init(void){
count= 0;
}
~
As you can see, I'm trying to prototype my functions inside the class, then define them outside the class. The book did exactly what I am trying this, but it doesn't work. Where is the mistake? Is it my computer (I'm using a mac). The error I get is the question, but also here:
user-MacBook-Pro:cplusplus trevortruog$ g++ Stack2.cpp
Undefined symbols for architecture x86_64: "_main", referenced from:
start in crt1.10.6.o ld: symbol(s) not found for architecture x86_64 collect2: ld returned 1 exit status
The code compiles fine. It just doesn’t do anything useful since it’s missing a main function and therefore no executable can be generated from it.
This is not an error in the compiler but rather in the linker, which precisely complains about the lack of an entry point. You can see this from the error message:
ld: symbol(s) not found for architecture
The first thing, ld, is the name of the application which created the error message. ld is the linker application which is (internally) called by the actual compiler. Once that gets called, the code is already compiled.
Add a main function to solve the linker error.
As an added comment, the code uses bad practice. This is a sure hint that the programming book you’re using is bad. Unfortunately, bad teaching material is the bane of C++, which is a highly complex language even when taught correctly. Do yourself a favour and ditch the book in favour of a good one.
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).
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.