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.
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?
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.
Hi so I'm trying to compile a Fortran code written by someone else in what I believe is F77 (.for extension). The error I'm getting is:
Undefined symbols for architecture x86_64:
"_random_", referenced from:
_pms_ in ccx1qzWD.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
I'm not posting the code now but I've been searching online for what this error even means so I can start searching for the solution but can't find any helpful information. I have no previous Fortran experience! Any help on the meaning of this error is really appreciated. I'm using a mac and the only section that includes the "random" is part of a subroutine:
DOUBLE PRECISION RANDOM
DO 1 J=1,M
w(J)=dw+REAL(J*2-1)*dw/2.0
W1=w(J)*w(J)*w(J)*w(J)*w(J)
Vel1=Vel*Vel*Vel*Vel*W1/w(J)
Vel1=-6844.0697/Vel1
Gw(J)=0.77898/W1*EXP(Vel1)
Phi(J)=RANDOM()*8.0*ATAN(1.0)
1 CONTINUE
I am using: gfortran SEASIM.FOR to compile
Thanks in advance for any advice!
OK, it definitely looks like you're not providing the linker with a library containing an implementation of random. You could dig around and find such a library, modify your linkage, and see how that goes. But it would be easier to replace the call to random with a call to the gfortran intrinsic rand which returns a number from a uniform distribution between 0 and 1. So that's what I suggest you do. Consult the documentation for further details.
A better approach would be to use the now-standard random_number but that's a subroutine and would require more of a modification to your program.
I have a nagging suspicion that if the only step you take to build the program is to execute gfortran SEASIM.FOR at the command-line then you may be missing linking to other necessary libraries so I won't be surprised if you report failure.
This line
DOUBLE PRECISION RANDOM
declares that RANDOM is a DOUBLE PRECISION thing. That it is a function returning a value rather than a variable holding a value is made clear by the later use of RANDOM(). The compiler is indifferent to the location of the code implementing the routine, but the linker is not.
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).
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.