Cygwin + Gdb Simple_Program - gdb

I wrote this simple program in cygwin:
#include <iostream>
int main()
{
int a;
float b;
std::cout << "Podaj liczbe calkowita: ";
std::cin >> a;
std::cout << "Podaj liczbe rzeczywista: ";
std::cin >> b;
std::cout << "Liczba a = " << a << std::endl;
std::cout << "Liczba b = " << b << std::endl;
return 0;
}
When I try to debug it with gdb, (compiling with -g option), and I get this as my output:
g++ -o -g test test.cpp
test: In function `mainCRTStartup':
/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: multiple definition of `mainCRTStartup'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/crt0.o:/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: first defined here
test: In function `mainCRTStartup':
/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: multiple definition of `WinMainCRTStartup'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/../../../../lib/crt0.o:/usr/src/debug/cygwin-2.4.1-1/winsup/cygwin/crt0.c:23: first defined here
test:cygming-crtbegin.c:(.text+0x60): multiple definition of `__gcc_register_frame'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x10): first defined here
test:cygming-crtbegin.c:(.text+0xd0): multiple definition of `__gcc_deregister_frame'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x80): first defined here
test:crt0.c:(.data+0x0): multiple definition of `__dso_handle'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.data+0x0): first defined here
test:crt0.c:(.text+0x50): multiple definition of `.weak._Jv_RegisterClasses.__dso_handle'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x0): first defined here
test:crt0.c:(.idata+0x1d0): multiple definition of `__imp__ZSt3cin'
test:crt0.c:(.idata+0x1d0): first defined here
test:crt0.c:(.idata+0x1d8): multiple definition of `__imp__ZSt4cout'
test:crt0.c:(.idata+0x1d8): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.text+0x0): multiple definition of `main'
test:test.cpp:(.text+0xe0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): multiple definition of `.refptr._ZSt4cout'
test:test.cpp:(.rdata+0xb0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt3cin[.refptr._ZSt3cin]+0x0): multiple definition of `.refptr._ZSt3cin'
test:cygming-crtbegin.c:(.rdata+0xa0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_[.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_]+0x0): multiple definition of `.refptr._ZSt4endlIcSt11char_traitsIcEERSt13basic_ostreamIT_T0_ES6_'
test:test.cpp:(.rdata+0xc0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr.__dso_handle[.refptr.__dso_handle]+0x0): multiple definition of `.refptr.__dso_handle'
test:test.cpp:(.rdata+0xd0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZNSt8ios_base4InitD1Ev[.refptr._ZNSt8ios_base4InitD1Ev]+0x0): multiple definition of `.refptr._ZNSt8ios_base4InitD1Ev'
test:cygming-crtbegin.c:(.rdata+0x90): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt3cin[.refptr._ZSt3cin]+0x0): multiple definition of `__fu0__ZSt3cin'
test:cygming-crtbegin.c:(.rdata+0xa0): first defined here
/tmp/ccG5Ono8.o:test.cpp:(.rdata$.refptr._ZSt4cout[.refptr._ZSt4cout]+0x0): multiple definition of `__fu1__ZSt4cout'
test:test.cpp:(.rdata+0xb0): first defined here
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x35): undefined reference to `_Jv_RegisterClasses'
/usr/lib/gcc/x86_64-pc-cygwin/4.9.3/crtbegin.o:cygming-crtbegin.c:(.text+0x35): relocation truncated to fit: R_X86_64_PC32 against undefined symbol `_Jv_RegisterClasses'
collect2: error: ld returned 1 exit status
./test.exe
Any ideas on why I am getting this?

Related

what does (.text+0x20): undefined reference to `main' mean [duplicate]

This question already has answers here:
(.text+0x20): undefined reference to `main' and undefined reference to function
(3 answers)
Closed 3 years ago.
Could someone help me with the error that I am getting in the code listing below?
The error is, most likely, about the constructor/destructor:
/usr/bin/ld:
/usr/lib/gcc/x86_64-linux-gnu/8/../../../x86_64-linux-gnu/Scrt1.o: in
function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status
Code listing:
// counter_id.cpp
// Obco class member-function definitions.
#include <iostream>
#include "counter_id.h" // include counter class definition
using namespace std;
// constructor sets object's ID number and descriptive message
Obco::Obco( int ID, string messageString )
: objectID( ID ), message( messageString )
{
cout << "Object " << objectID << "constructor runs"
<< message << endl;
} // end CreateAndDestroy constructor
// destructor
Obco::~Obco()
{
// output newline for certain objects; helps readability
cout << ( objectID == 1 || objectID == 6 ? "\n" : "" );
cout << "Object " << objectID << " destructor runs "
<< message << endl;
} // end ~Obco destructor
You're not just separately compiling (-c), you're trying to do a full link but the linker hasn't found any main function.
For a complete link you need to include all your source or object files in your g++ invocation (especially the one with your main function).
For separately compiling just your one file (to be linked later) you need to add the -c option.

"Duplicate symbol" when attempting to compile two .cpp files (from XCode) in terminal (MacOSX)

I have two .cpp files, main.cpp and secondFile.cpp:
#include <iostream>
int main()
{
std::cout << "Hello, World!\n" << std::endl;
std::cout << "I was also able to add this line!" << std::endl;
return 0;
}
And
#include <iostream>
int main()
{
std::cout << "This was from the second file!" << std::endl;
return 0;
}
I have successfully run g++ -o main.cpp main and g++ -o secondFile.cpp secondFile, as well as run each of their corresponding executables. However when I attempt to compile them simultaneously into a single executable g++ -o main.cpp secondFile.cpp bothScripts or clang++ main.cpp secondFile.cpp -o bothScripts I receive the following error:
"duplicate symbol _main in:
/var/folders/49/38grlkzs44zcth3v_dw9m9dm0000gn/T/main-d43536.o
/var/folders/49/38grlkzs44zcth3v_dw9m9dm0000gn/T/secondfile-2bee63.o
ld: 1 duplicate symbol for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)"
Clearly something is being loaded twice, but I am unsure whether this is a library (iostream), that I've named both sections 'main', or something else entirely. There are certainly questions similar to this already, but many are convoluted and not as fundamental for new C++ members (hence my question here).
Context: My rationale is to practice building executables from multiple .cpp files. Is there a better way to go about this? (New to C++ but not to programming/code as a whole.)
The reason for your error is simple. You have 2 main() functions. As you should know, in a C++ program, the function main() generally defines the entry point of a program. When each of the files are compiled together, and have their own main() function, the compiler gets confused and throws an error. To solve this, simply change the name of the main() function in one file, and call it from the other file, if you are planning to run them together.

C++ - What's wrong with this separate compilation code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
I have this code that I'm trying to debug. It looks okay to me. But I get an error I don't understand.Here's my code
//struct.h
#ifndef STRUCT_H
#define STRUCT_H
#include <iostream>
using namespace std;
struct Person {
Person();
Person(int a, string n);
Person(const Person &p);
Person &operator=(const Person &p);
~Person();
int age;
string name;
};
#endif
//struct.cc
#include "struct.h"
Person::Person(): age(0), name("noname") {
cout << "Creating default Person" << endl;
}
Person::Person(int a, string n): age(a), name(n) {
cout << "Creating: " << name << "," << age << endl;
}
Person::Person(const Person &p) {
name = p.name;
age = p.age;
}
Person& Person::operator=(const Person &p) {
Person person;
person.name = p.name;
return person;
}
Person::~Person() {
cout << "Destroying: " << name << "," << age << endl;
}
//structMain.cc
#include "struct.h"
#include <iostream>
using namespace std;
int main() {
Person one(21, "Zuhaib");
cout << "I am " << one.name << ". I am " << one.age << " years old" << endl;
Person two;
cout << "I am " << two.name << ". I am " << two.age << " years old" << endl;
two = one;
cout << "I am " << two.name << ". I am " << two.age << " years old" << endl;
}
I compile with
g++ -c struct.cc
g++ -c structMain.cc
g++ -o struct.o structMain.o
I then get the following error
structMain.o: In function `main':
structMain.cc:(.text+0x3b): undefined reference to `Person::Person(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
structMain.cc:(.text+0xb5): undefined reference to `Person::Person()'
structMain.cc:(.text+0x11e): undefined reference to `Person::operator=(Person const&)'
structMain.cc:(.text+0x180): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x18c): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x1b8): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x1e3): undefined reference to `Person::~Person()'
structMain.cc:(.text+0x1f4): undefined reference to `Person::~Person()'
collect2: ld returned 1 exit status
I think I included all the right files. I double checked the declarations and definitions. I'm just not sure why these errors are coming up. It looks fine to me.
Also, in the main function, what happens at the line
two = one;
I wonder this because, I've overloaded the operator=, but I've also defined the copy constructor which also executes when "=" is encountered. So in the above case, does operator= execute or the copy constructor.
Any help would be appreciated.
Thanks
When you use:
g++ -o struct.o structMain.o
g++ tries to produce an executable by using the code from structMain.o and put the resulting executable in struct.o. Since structMain.o does not have all the object code to create the executable, you get the linker errors.
You need to use:
g++ -o MyProgram struct.o structMain.o
Here, g++ tries to produce an executable by using the code from struct.o and structMain.o and put the resulting the executable in MyProgram. Assuming you have all the required object code in those two files, that should work.
g++ -o struct.o structMain.o
This tells g++ to take the object code in structMain.o, turn it into an executable, and name that executable struct.o.
As your compiler does exactly what you tell it to, you have just overwritten the object code in struct.o, which happened to hold the definition of your Person class, so yes, the references to that class by structMain.o remain unresolved.
What you want is:
g++ struct.o structMain.o
...or give an explicit output name that is not also the name of one of your input files...
g++ -o someGuessedExecutableName struct.o structMain.o
A common convention is to name a source file after the class it defines, i.e. name it Person.cc and Person.o instead of struct.cc / struct.o... which would have made that error more visible.

"Undefined symbols" error in C++

I have Point class that has X, Y and Name as data members. I overloaded
T operator-(const Point<T> &);
This calculates the distance between two points and returns a value
template < typename T>
T Point<T>::operator-(const Point<T> &rhs)
{
cout << "\nThe distance between " << getName() << " and "
<< rhs.getName() << " = ";
return sqrt(pow(rhs.getX() - getX(), 2) + pow(rhs.getY() - getY(), 2));;
}
The main function
int main () {
Point<double> P1(3.0, 4.1, "Point 1");
Point<double> P2(6.4, 2.9, "Point 2");
cout << P2 - P1;
return EXIT_SUCCESS;
}
But the problem is that this program doesn't compile, and I receive this error:
Undefined symbols:
"Point<double>::operator-(Point<double>&)", referenced from:
_main in main.o
ld: symbol(s) not found
collect2: ld returned 1 exit status
Any help is appreciated...
You can't compile non-specialized templates. You have to put the definition code in headers.
You need to put your Point template class in a .hpp file and include that whenever using a Point.
You have to include templates in each file that uses them, otherwise the compiler cannot generate the code for your specific type.
There is also a precedence between operators, which isn't changed when overloading them. Your code will be treated as
(cout << P2) - P1;
Try this instead
cout << (P2 - P1);

Code's not compiling because of nested anonymous blocks?

(Taken from Accelerated C++)
Why does the following code not work:
#include <iostream>
#include <string>
int main() {
{
const std::string s = "a string";
std::cout << s << std::endl;
{
const std::string s = "another string";
std::cout << s << std::endl;
}
}
}
Am I not allowed to nest anonymous blocks in C++? When trying to compile the source, gcc gives me the following error (which I can't make any sense of):
hello: In function `_start':
(.text+0x0): multiple definition of
`_start'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.text+0x0):
first defined here
hello:(.rodata+0x0): multiple
definition of `_fp_hw'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata+0x0):
first defined here hello: In function
`_fini': (.fini+0x0): multiple
definition of `_fini'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.fini+0x0):
first defined here
hello:(.rodata+0x4): multiple
definition of `_IO_stdin_used'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.rodata.cst4+0x0):
first defined here hello: In function
`__data_start': (.data+0x0): multiple
definition of `__data_start'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crt1.o:(.data+0x0):
first defined here hello: In function
`__data_start': (.data+0x4): multiple
definition of `__dso_handle'
/usr/lib/gcc/i486-linux-gnu/4.4.3/crtbegin.o:(.data+0x0):
first defined here hello: In function
`_init': (.init+0x0): multiple
definition of `_init'
/usr/lib/gcc/i486-linux-gnu/4.4.3/../../../../lib/crti.o:(.init+0x0):
first defined here /tmp/cchh83A6.o: In
function `main':
hello.cpp:(.text+0x0): multiple
definition of `main'
hello:(.text+0xb4): first defined here
/usr/lib/gcc/i486-linux-gnu/4.4.3/crtend.o:(.dtors+0x0):
multiple definition of `__DTOR_END__'
hello:(.dtors+0x4): first defined here
/usr/bin/ld: warning: Cannot create
.eh_frame_hdr section, --eh-frame-hdr
ignored. /usr/bin/ld: error in
hello(.eh_frame); no .eh_frame_hdr
table will be created. collect2: ld
returned 1 exit status
That code works fine. See here Your code compiled (You have a problem with your environment)
You appear to be linking against 2, or more, object files (or .a files) that define the same things. If you link against one or the other your linker problem will go away.