I am trying to code a very simple class in C++11 but I am getting the compiler error 'Undefined symbols for architecture x86_64'. What does it mean? How do I deal with it?
I am using Visual Studio Code for MacOS and the compiler Clang-8 (updated 2 days ago).
Here are the 3 files I have written so far:
*------------------------------------------------------------------*/
class Point
{
// Type declaration statements for data members
private:
double xCoord, yCoord; // Class atributes
public:
// Declaration statements for class methods
// Constructors for Point class
Point(); // Default constructor
Point(double x, double y); // Parameterized constructor
};
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
#include "Point.h" // Required
#include<iostream> // Required for cout
using namespace std;
// Default constructor
Point::Point()
{
cout << "Constructing point object, default: \n";
cout << "Initializing to zero" << endl;
xCoord = 0.0;
yCoord = 0.0;
}
// Parameterized constructor
Point::Point(double x, double y)
{
// Input paraneters x and y
cout << "Constructing point object, parameterized: \n";
cout << "Input parameters: " << x << "," << y << endl;
xCoord = x;
yCoord = y;
}
/*------------------------------------------------------------------*/
/*------------------------------------------------------------------*/
#include<iostream>
#include "Point.h"
using namespace std;
int main()
{
// Declare and initialize objects
cout << "In main, declare p1... " << endl;
Point p1;
cout << "\nIn main, declare p2... " << endl;
Point p2{1.5, -4.7};
return 0;
}
/*------------------------------------------------------------------*/
The full error message is:
Undefined symbols for architecture x86_64:
"Point::Point(double, double)", referenced from:
_main in cpp14test-15cf25.o
"Point::Point()", referenced from:
_main in cpp14test-15cf25.o
ld: symbol(s) not found for architecture x86_64
clang-8: error: linker command failed with exit code 1 (use -v to see invocation)
The terminal process terminated with exit code: 1
Related
#include <iostream>
#include <cstdlib>
using namespace std;
class c
{
public:
c(){ cout << "Default ctor" << endl; }
c(const c& o) { cout << "Copy ctor" << endl; }
c(c&& o){ cout << "move copy ctor" << endl; } // Line 11
//c(c&& o) = delete; // Line 12
~c() { cout << "Destructor" << endl; }
};
int main()
{
c o = c(); // Line 18
c p = std::move(c());
return 0;
}
This piece of code produces the below output.
Default ctor
Default ctor
move copy ctor
Destructor
Destructor
Destructor
However, if I deactivate line#11 and activate line#12 then it gives below compile time error.
prog.cc: In function 'int main()':
prog.cc:18:13: error: use of deleted function 'c::c(c&&)'
18 | c o = c();
| ^
prog.cc:12:9: note: declared here
12 | c(c&& o)=delete;
| ^
which implies that, line#18 invokes the move copy constructor provided by compiler. How to get my user defined move constructor in line#11 invoked without using std::move?
Or std::move is the only way to get user defined move constructors invoked?
I have the following code snipet:
// code snipet one:
#include <memory>
#include <iostream>
#include <queue>
struct A {
uint32_t val0 = 0xff;
~A() {
std::cout << "item gets freed" << std::endl;
}
};
typedef std::shared_ptr<A> A_PTR;
int main()
{
std::queue<A_PTR> Q;
Q.push(std::make_shared<A>());
auto && temp_PTR = Q.front();
std::cout << "first use count = " << temp_PTR.use_count() << std::endl;
Q.pop();
std::cout << "second use count = " << temp_PTR.use_count() <<std::endl;
return 0;
}
After running it, I got the result as following:
first use count = 1
item gets freed
second use count = 0
Q1: is anybody can explain what the type of temp_PTR after the third line of main function is called?
if I change that line as
A_PTR && temp_PTR = Q.front();
compiler complains that
main.cpp: In function 'int main()':
main.cpp:26:32: error: cannot bind '__gnu_cxx::__alloc_traits > >::value_type {aka std::shared_ptr}' lvalue to 'A_PTR&& {aka std::shared_ptr&&}'
A_PTR && temp_PTR = Q.front();
Q2: I remember that the return value of a function should be a r-value, but it seems here the compiler tell me: " hey, the return value of Queue.front() is a l-value", why is here?
For Q2, I just check the C++ docs, that the return value of Queue.front() is refernece, that means it return a l-value
reference& front();
const_reference& front() const;
For Q3, it works for A_PTR temp_PTR = std::move(Q.front());, it is what I want.
I keep getting the following error when I try to compile an example program from C++ how to program, Deitel and Deitel. I used g++ Fig11_05.cpp -o Fig11_05
I spent hours trying to solve this problem by looking up the Internet and particularly stackoverflow, but with no avail!
I tried using different command line arguments such as -libstd=libc++, -std=c++11, -std=c++14
the error I keep getting is as this:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream >&, PhoneNumber const&)", referenced from:
_main in Fig11_05-1f04bd.o
"operator>>(std::__1::basic_istream >&, PhoneNumber&)", referenced from:
_main in Fig11_05-1f04bd.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
The result of g++ -v:
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk/usr/include/c++/4.2.1
Apple LLVM version 8.0.0 (clang-800.0.42.1)
Target: x86_64-apple-darwin15.6.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
The codes:
// Fig. 11.3: PhoneNumber.h
// PhoneNumber class definition
#ifndef PHONENUMBER_H
#define PHONENUMBER_H
#include <iostream>
#include <string>
class PhoneNumber
{
friend std::ostream& operator<<(std::ostream&, const PhoneNumber&);
friend std::istream &operator>>(std::istream&, PhoneNumber&);
private:
std::string areaCode; // 3-digit area code
std::string exchange; // 3-digit exchange
std::string line; // 4-digit line
}; // end class PhoneNumber
#endif
// Fig. 11.4: PhoneNumber.cpp
// Overloaded stream insertion and stream extraction operators
// for class PhoneNumber.
#include <iomanip>
#include "PhoneNumber.h"
using namespace std;
// overloaded stream insertion operator; cannot be
// a member function if we would like to invoke it with
// cout << somePhoneNumber;
ostream& operator<<(ostream& output, const PhoneNumber& number)
{
output << "Area Code: " << number.areaCode << "\nExchange: "
<< number.exchange << "\nLine: " << number.line << "\n"
<< "(" << number.areaCode << ") " << number.exchange << "-"
<< number.line << "\n";;
return output; // enables cout << a << b << c;
} // end function operator<<
// overloaded stream extraction operator; cannot be
// a member function if we would like to invoke it with
// cin >> somePhoneNumber;
istream& operator>>(istream& input, PhoneNumber& number)
{
input.ignore(); // skip (
input >> setw(3) >> number.areaCode; // input area code
input.ignore(2); // skip ) and space
input >> setw(3) >> number.exchange; // input exchange
input.ignore(); // skip dash (-)
input >> setw(4) >> number.line; // input line
return input; // enables cin >> a >> b >> c;
} // end function operator>>
// Fig. 11.5: fig11_05.cpp
// Demonstrating class PhoneNumber's overloaded stream insertion
// and stream extraction operators.
#include <iostream>
#include "PhoneNumber.h"
using namespace std;
int main()
{
PhoneNumber phone; // create object phone
cout << "Enter phone number in the form (123) 456-7890:" << endl;
// cin >> phone invokes operator>> by implicitly issuing
// the global function call operator>>( cin, phone )
cin >> phone;
cout << "\nThe phone number entered was:\n";
// cout << phone invokes operator<< by implicitly issuing
// the global function call operator<<( cout, phone )
cout << phone << endl;
} // end main
Note: I recently installed then uninstalled CUDA toolkit 8. I needed a newer version of Xcode, so I installed Xcode the newest version 8.2.1, and kept the old version in a different directory just in case. I don't think the problem is with the installation of Xcode though. Also when I installed CUDA I had to set DYLD_LIBRARY_PATH to a directory. However, it might not be the source of the problem! I am just trying to help you figure out the problem to help me fix it :)
Thank you in advance! You are such a good community!
Your attempt to build the program:
g++ Fig11_05.cpp -o Fig11_05
is unsuccessful because you have neglected to compile and link into the
program the code that contains the definitions of the functions:
std::ostream& operator<<(std::ostream& output, const PhoneNumber& number)
and:
std::istream& operator>>(std::istream& input, PhoneNumber& number)
which are called by your program. That is why the linker says:
Undefined symbols for architecture x86_64:
"operator<<(std::__1::basic_ostream >&, PhoneNumber const&)", referenced from: _main in Fig11_05-1f04bd.o
"operator>>(std::__1::basic_istream >&, PhoneNumber&)", referenced from: _main in Fig11_05-1f04bd.o
...
Do this instead:
g++ Fig11_05.cpp PhoneNumber.cpp -o Fig11_05
Or to spell out the process in full:-
Compile source file Fig11_05.cpp to object file Fig11_05.o:
$ g++ -o Fig11_05.o -c Fig11_05.cpp
Compile source file PhoneNumber.cpp to object file PhoneNumber.o:
$ g++ -o PhoneNumber.o -c PhoneNumber.cpp
Link the object files Fig11_05.o and PhoneNumber.o into program Fig11_05
$ g++ -o Fig11_05 Fig11_05.o PhoneNumber.o
You can then run:
./Fig11_05
Here is a fairly good beginner's tutorial
about building C or C++ programs with GCC.
Trying to invoke hi or std::move on it results in this:
/tmp/cch3DRvH.o: In function `main':
main.cpp:(.text.startup+0x5): undefined reference to `hi(int, char**)'
collect2: error: ld returned 1 exit status
#include <type_traits>
#include <utility>
#include <iostream>
#include <typeinfo>
#include <functional>
int main(int argc, char* argv[])
{
decltype(main) hi;
decltype(main) hi2;
// std::function<int(int, char**)> hi2 = std::move(hi);
// hi(argc, argv);
std::cout << std::boolalpha;
std::cout << std::is_same<decltype(main), decltype(hi)>::value << std::endl;
std::cout << std::is_function<decltype(main)>::value << std::endl;
std::cout << std::is_function<decltype(hi)>::value << std::endl;
std::cout << std::is_same<decltype(std::move(main)), decltype(std::move(hi))>::value << std::endl;
std::cout << std::is_same<decltype(hi2), decltype(hi)>::value << std::endl;
return 0;
}
Output:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:16:54: warning: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]
std::cout << std::is_same<decltype(std::move(main)), decltype(std::move(hi))>::value << std::endl;
main.cpp:16:54: warning: ISO C++ forbids taking address of function ‘::main’ [-Wpedantic]
true
true
true
true
true
It seems hi is almost the same as main except it doesn't give warnings like when using std::cout << hi. Is it possible to get this program to not output undefined reference tohi(int, char**)'`?
decltype(main) is an alias for the type int(int, char **). You have declared a function hi with this type but did not define it.
To get rid of the undefined reference error, just define the function:
int hi( int, char ** ) {
return 0;
}
The messages that appear before true are not output from your program but from the compiler.
In C++11 I have a struct with lots of attributes like so:
#include <atomic>
struct Foo {
int x;
int y;
// ...
// LOTS of primitive type attributes, followed by...
// ...
std::atomic_bool bar;
}
And I'd like to define an instance like so:
bool bar_value = true;
Foo my_foo = {/*attribute values*/, bar_value};
However, the atomic_bool is throwing a "use of deleted function" error because I think copy constructing is not allowed on atomics. Is there any way around this, short of writing out a constructor or assigning each value individually?
It just seems inconvenient to have to treat this otherwise relatively banal struct in a special way just because one of its many attributes is a special case.
Updates:
Any takers? I've been looking around, but there doesn't seem to be any straightforward way to resolve this.
Try wrapping the initialization of the atomic_bool in its own initializer list. It worked for me in g++ 4.7.
#include <atomic>
#include <iostream>
struct Foo
{
int x;
int y;
std::atomic_bool bar;
};
int main(int, char**)
{
Foo f1 = {1, 2, {true}};
Foo f2 = {3, 4, {false}};
std::cout << "f1 - " << f1.x << " " << f1.y << " "
<< (f1.bar.load()?"true":"false") << std::endl;
std::cout << "f2 - " << f2.x << " " << f2.y << " "
<< (f2.bar.load()?"true":"false") << std::endl;
}
I got the following output:
$ g++ -std=c++11 test.cpp -o test && ./test
f1 - 1 2 true
f2 - 3 4 false