I write code like this in my Mac OS X v10.8 (Mountain Lion), and when I use "gcc use_new.cpp -o use_new" to compile it, it throws a wrong message like this:
Undefined symbols for architecture x86_64:
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
_main in ccr2vrRQ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(void const*)", referenced from:
_main in ccr2vrRQ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(double)", referenced from:
_main in ccr2vrRQ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
_main in ccr2vrRQ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(unsigned long)", referenced from:
_main in ccr2vrRQ.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccr2vrRQ.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccr2vrRQ.o
"std::cout", referenced from:
_main in ccr2vrRQ.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in ccr2vrRQ.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccr2vrRQ.o
"operator delete(void*)", referenced from:
_main in ccr2vrRQ.o
"operator new(unsigned long)", referenced from:
_main in ccr2vrRQ.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
And when I use "g++ use_new.cpp -o use_new" it is OK. How can I fix this?
#include <iostream>
struct fish
{
float weight;
int id;
int kind;
};
int main()
{
using namespace std;
int* pt = new int;
*pt = 1001;
cout << "int: " << *pt << "in location: " << pt << endl;
double* pd = new double;
*pd = 100000001.0;
cout << "double: " << *pd << "in location: " << pd << endl;
cout << "int point pt is length " << sizeof(*pt) << endl;
cout << "double point pd is length " << sizeof(*pd) << endl;
delete pt;
delete pd;
cout << (int *)"How are you!" << endl;
return 0;
}
This is the case even with the old 4.2 GCC (I experienced this when I set up my unofficial iOS toolchain). gcc assumes C by default, and invokes the linker without linking to the C++ standard library; in contrast, g++ assumes C++ and links against the C++ standard library by default.
All in all - possible solutions:
gcc myprog.c -o myprog -lstdc++
or
g++ myprog.c -o myprog
The answer to this Stack Overflow question has the answer
gcc and g++ linker
Use
gcc -lstdc++ use_new.cpp -o use_new
The -lstdc++ flag tells the linker to include the C++ Standard Library.
I'm running Mac OS X v10.7.4 (Lion) and the library is located here:
/usr/lib/libstdc++.dylib
This isn't related to the code pasted by #user1582840, just my two cents, and from a different cause of the same problem in g++ when working on some of my own code:
I received the "ld: symbol(s) not found for architecture x86_64" error when using g++ 4.2.1 on OS X v10.8 (Mountain Lion)/Darwin 11 for a different reason.
I received this error because I had a Class defined. In the class definition, I declared member functions. However, when I defined the member functions I forgot to include the class modifier.
So for an example of what I'm talking about (code sample, not full program):
class NewClass
{
NewClass(); // Default constructor
};
Then later, when defining the NewClass() constructor (or any member function) I simply had:
// Don't do this. It will throw that error!!
NewClass()
{
// Do whatever
}
rather than:
// Proper way
NewClass::NewClass()
{
// Do whatever
}
This is a rather simple mistake, and I managed to catch it in a short amount of time luckily, but it could be easy for someone to miss (us newbies especially), and the solutions about gcc/g++ linkers, Xcode, etc. aren't any help for this :P
Related
I'm trying to use Openmp on my mac, but i'm getting a compiler error I don't understand. I installed openmp using
brew install llvm
When I try to compile my code using -fopenmp I get errors like this
Undefined symbols for architecture x86_64:
"std::runtime_error::what() const", referenced from:
vtable for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::runtime_error> > in ccOEbbPM.o
vtable for boost::exception_detail::error_info_injector<std::runtime_error> in ccOEbbPM.o
vtable for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::range_error> > in ccOEbbPM.o
vtable for boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<std::overflow_error> > in ccOEbbPM.o
vtable for boost::exception_detail::error_info_injector<std::range_error> in ccOEbbPM.o
vtable for boost::exception_detail::error_info_injector<std::overflow_error> in ccOEbbPM.o
"std::locale::operator==(std::locale const&) const", referenced from:
boost::detail::lcast_put_unsigned<std::char_traits<char>, unsigned long long, char>::convert() in ccOEbbPM.o
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find_first_not_of(char, unsigned long) const", referenced from:
boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1, (boost::multiprecision::cpp_int_check_type)0, std::allocator<unsigned long long> >::do_get_string[abi:cxx11](std::_Ios_Fmtflags, mpl_::bool_<false> const&) const in ccOEbbPM.o
"std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
std::basic_ostream<char, std::char_traits<char> >& boost::multiprecision::operator<< <boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1, (boost::multiprecision::cpp_int_check_type)0, std::allocator<unsigned long long> >, (boost::multiprecision::expression_template_option)1>(std::basic_ostream<char, std::char_traits<char> >&, boost::multiprecision::number<boost::multiprecision::backends::cpp_int_backend<0u, 0u, (boost::multiprecision::cpp_integer_type)1, (boost::multiprecision::cpp_int_check_type)0, std::allocator<unsigned long long> >, (boost::multiprecision::expression_template_option)1> const&) in ccOEbbPM.o
It's just a small segment of the error code since it goes on for quite a while, but I post the rest if need be. I also have the boost library installed.
Any help is appreciated, thanks.
EDIT:
Simple program that causes the error:
#include <iostream>
#include <omp.h>
using namespace std;
int main(){
int x = 5;
#pragma omp parallel
for(int y = 0; y < 50;y++){
x += 5;
}
cout << x << endl;
return 0;
}
Commands used to compile:
gcc-7 -fopenmp -Wall ex.cc
gcc-7 -std=c++11 -fopenmp -Wall ex.cc (I get less errors doing it this way)
Error when using the second compile command:
Undefined symbols for architecture x86_64:
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)
(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
_main in ccXt8Pa8.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
_main in ccXt8Pa8.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccXt8Pa8.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccXt8Pa8.o
"std::cout", referenced from:
_main in ccXt8Pa8.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in ccXt8Pa8.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
Use g++-7 rather than gcc-7, and all will be fine - it is C++ after all ;-)
g++-7 -std=c++11 -fopenmp openmp.cpp -o openmp
I started learning C++ and I tried to run the classic Hello World program:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello, World!";
return 0;
}
but when I compile it in terminal using GCC I always get that error:
Undefined symbols for architecture x86_64:
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::size() const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const", referenced from:
std::__verify_grouping(char const*, unsigned long, std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)in ccpe3DPY.o
"std::cout", referenced from:
_main in ccpe3DPY.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccpe3DPY.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in ccpe3DPY.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in ccpe3DPY.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccpe3DPY.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
can you tell me why?
You've compiled your program with gcc, which is a C compiler, not a C++ compiler. Use g++ instead; it knows to include the C++ run-time libraries, where those missing symbols are defined.
Thats a C compiler you need to use a C++ compiler like rob says above g++. Also if you are a beginner I would highly recommend using Microsoft visual studio express if you run a windows OS; if not then netbeans.
I wrote a program to create gnuplot commands according to data that was read from an input file. I required far too many commands to do this by hand. This worked quite nicely, but I now need to read data from two different data files and use these to create the gnuplot commands. Unfortunately something now seems to be going wrong with the reading of the two files. The portion of code involved in reading from the data files is as follows:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int n_snapshots;
int main () {
std::cout << "Enter number of snapshots" << "\n";
std::cin >> n_snapshots;
int snap_cell_count[n_snapshots];
std::ifstream in_file_count("data/snapshot_data");
std::ifstream in_file_fates("data/cell_fates_data_final");
for (int i=0; i<n_snapshots; i++) {
in_file_count >> snap_cell_count[i];
}
in_file_count.close();
int cell_fates[snap_cell_count[n_snapshots-1]];
for (int i=0; i<snap_cell_count[n_snapshots-1]; i++) {
in_file_fates >> cell_fates[i];
}
in_file_fates.close();
n_snapshots is simply some integer, snap_cell_count[] is an array with "n_shapshots" elements, each of which takes a value read from the data file "snapshot_data". cell_fates[] is an array with a number of elements that is equal to the value of the last element in snap_cell_count[], and again the values of its elements are read from a file, in this case "cell_fates_data_final". The data files to be read from are stored in a folder called "data".
Unfortunately, the compiler returns the following errors.
Undefined symbols for architecture x86_64:
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::c_str() const", referenced from:
_main in ccrpSAv5.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::str() const", referenced from:
_main in ccrpSAv5.o
"std::basic_istream<char, std::char_traits<char> >::operator>>(int&)", referenced from:
_main in ccrpSAv5.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
_main in ccrpSAv5.o
"std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()", referenced from:
_main in ccrpSAv5.o
"std::basic_ifstream<char, std::char_traits<char> >::close()", referenced from:
_main in ccrpSAv5.o
"std::basic_ifstream<char, std::char_traits<char> >::basic_ifstream(char const*, std::_Ios_Openmode)", referenced from:
_main in ccrpSAv5.o
"std::basic_ifstream<char, std::char_traits<char> >::~basic_ifstream()", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::open(char const*, std::_Ios_Openmode)", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::close()", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::basic_ofstream()", referenced from:
_main in ccrpSAv5.o
"std::basic_ofstream<char, std::char_traits<char> >::~basic_ofstream()", referenced from:
_main in ccrpSAv5.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::basic_stringstream(std::_Ios_Openmode)", referenced from:
_main in ccrpSAv5.o
"std::basic_stringstream<char, std::char_traits<char>, std::allocator<char> >::~basic_stringstream()", referenced from:
_main in ccrpSAv5.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
"std::ios_base::Init::~Init()", referenced from:
__static_initialization_and_destruction_0(int, int) in ccrpSAv5.o
"std::cin", referenced from:
_main in ccrpSAv5.o
"std::cout", referenced from:
_main in ccrpSAv5.o
"std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)", referenced from:
_main in ccrpSAv5.o
"___gxx_personality_v0", referenced from:
Dwarf Exception Unwind Info (__eh_frame) in ccrpSAv5.o
ld: symbol(s) not found for architecture x86_64
collect2: error: ld returned 1 exit status
This is being compiled with the gcc compiler on Mac OSX Mountain Lion.
Does anyone have any idea what's wrong here?
Well, for starters you can't just say int snap_cell_count[n_snapshots] with the value of n_snapshots being determined at runtime. You have to do something like
int *snap_cell_count = new int[n_snapshots];
// do some stuff
delete[] snap_cell_count;
Same thing for cell_fates
As for your linker errors, maybe this question solves your issues...
Ok, I just switched to MacOS after a while of Windows/Linux coexistence. I open a simple text editor, write a simple program just to check if everything is all right. I know there are other threads about this error but it just seems that in all the other cases the problem lied in a specific part of their program, which in turn is far more complex than this. I just want to understand why the compiler says what it says even with a simple thing like this.
#include <iostream>
#include "numeri.h"
int main(void)
{
std::cout << numeri() << std::endl;
return 0;
}
with
//numeri.h
int numeri(void);
and
//numeri.cpp
#include "numeri.h"
int numeri(void)
{
return (3);
}
what I get from gcc helloworld.cpp -Wall is
Undefined symbols for architecture x86_64:
"numeri()", referenced from:
_main in cc6WY2MJ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))", referenced from:
_main in cc6WY2MJ.o
"std::basic_ostream<char, std::char_traits<char> >::operator<<(int)", referenced from:
_main in cc6WY2MJ.o
"std::ios_base::Init::Init()", referenced from:
__static_initialization_and_destruction_0(int, int)in cc6WY2MJ.o
"std::ios_base::Init::~Init()", referenced from:
___tcf_0 in cc6WY2MJ.o
"std::cout", referenced from:
_main in cc6WY2MJ.o
"std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)", referenced from:
_main in cc6WY2MJ.o
ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
2 cases:
1 - I forgot how to use a simple header (I prefix, I'm a physics student, I can program for what it regards physics and numeric simulations [or at least, I could], but I just don't know that much about linkers-compilers-architecture and this kind of stuff)
2 - something's wrong
The C++ compiler is g++, . Also you need to add numeri.cpp to your commmand line.
g++ helloworld.cpp numeri.cpp -Wall
I am a computer science student and taking my first C++ class. I have a problem understanding what is going on with my code:
// This program uses the address of each element in the array.
#include <iostream>
using namespace std;
int main()
{
const int NUM_COINS = 5;
int coins[NUM_COINS] = {5, 1, 25, 5, 10};
int *p1; // Pointer to a double.
int count; // Counter variable.
// Use the pointer to display the values in the array.
cout << "Here are the values in the coins array: \n";
for(count = 0; count << NUM_COINS; count++)
{
// Get the address of an array element
p1 = &coins[count];
// Display the contents of the element
cout << *p1;
}
cout << endl;
return 0;
}
so my first question is why doesn't make compile it? I have no problems at all with any of my other simple programs. I am using g++ on OS X 4.2.1. I have to type the g++ -o command for it to compile, if not...i get these errors:
g++ -c -o 9-8.o 9-8.cpp cc 9-8.o
-o 9-8 Undefined symbols: "std::basic_ostream >&
std::operator<<
(std::basic_ostream >&, char
const*)", referenced from:
_main in 9-8.o
_main in 9-8.o "std::ios_base::Init::Init()",
referenced from:
__static_initialization_and_destruction_0(int, int)in 9-8.o
"std::basic_string,
std::allocator >::size() const",
referenced from:
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
"std::basic_string,
std::allocator
::operator[](unsigned long) const", referenced from:
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
"___gxx_personality_v0", referenced
from:
std::__verify_grouping(char const*, unsigned long,
std::basic_string,
std::allocator > const&)in 9-8.o
___tcf_0 in 9-8.o
_main in 9-8.o
unsigned long const& std::min(unsigned long
const&, unsigned long const&)in 9-8.o
__static_initialization_and_destruction_0(int, int)in 9-8.o
global constructors keyed to mainin 9-8.o
CIE in 9-8.o "std::ios_base::Init::~Init()",
referenced from:
___tcf_0 in 9-8.o "std::basic_ostream >&
std::endl
(std::basic_ostream >&)",
referenced from:
_main in 9-8.o "std::basic_ostream
::operator<<(std::basic_ostream >&
(*)(std::basic_ostream >&))",
referenced from:
_main in 9-8.o "std::basic_ostream
::operator<<(int)", referenced from:
_main in 9-8.o "std::cout", referenced from:
_main in 9-8.o
_main in 9-8.o
_main in 9-8.o ld: symbol(s) not found collect2: ld returned 1 exit
status make: *** [9-8] Error 1
which leads to my second question. Even if I do type the g++ command, it compiles but after running it outputs an empty array. So my question #2 is: is my code correct? How do I properly use pointers with the reference address statement?
Reason: You are not using the comparision operator correctly. After changing it to be "<", your code should work correctly.
for(count = 0; count << NUM_COINS; count++)
^ should be "<" here
I don't see any problem except that one problem in your for loop:
for(count = 0; count << NUM_COINS; count++)
//^^
That is not comparison. That is left-shift operation. I'm sure you didn't intend that.
That should be : count < NUM_COINS.