PDA

View Full Version : C++ compilation error



Eragon0605
December 5th, 2009, 02:22 AM
I have been programming in C++ for about a year now, but it was always under Window$. Today was my first time programming under Ubuntu, and I have run into a serious error. When I try to compile a C++ program with strings in it, I get the messages:

/home/dck/strings.o In function 'main':
strings.cpp:(.text+0xe7) undefined reference to '__sync_fetch_and_add_4'
=== Build finished: 1 errors, 0 warnings ===

Here is the code:

#include <string>
#include <iostream>

using namespace std;

int main()
{
string a("Bob the Samurai is Awesome!!!");

cout << a << endl;

return 0;
}I can compile any program that doesn't use strings no problem. I have looked at several tutorials, and I can't see any difference between my code and their code... I am using the Code::Blocks IDE.

MadCow108
December 5th, 2009, 02:49 AM
what compiler version and distro do you use?
does it also happen if you compile over command line?

this is no coding error, there is probably something wrong with your libraries.

you could try reinstalling/updating the build essentials

A_Fiachra
December 5th, 2009, 05:03 AM
Yep -- that's utterly valid C++ code.

How are you compiling and linking??

endBc
December 5th, 2009, 01:13 PM
#include <string>
#include <iostream>

using namespace std;

int main()
{
string str = "Bob the Samurai is Awesome!";
cout << str << endl;
return 0;
}


Does this piece of code produce the same output ( error ) ?

Eragon0605
December 5th, 2009, 05:12 PM
Sorry for not replying sooner, I was messing around with X and ran into some promblems... :)

To endBc, yes, I still get the same error.

To A_Fiachra, well, uh, I'm not exactly sure how i'm linking, but I compile by pressing the compile button...

To MadCow108, I am using 9.10 Karmic Koala. As for the compiler version, I know g++ is version 4.4 (maybe 4.4.1), but I don't know about GNU GCC compiler. Code::Blocks doesn't seem to support command-line compiling, or at least requires a special command. Build-essential had some updating to do, but it didn't fix my problem...

Eragon0605
December 5th, 2009, 05:22 PM
I reinstalled Code::Blocks and... Ta-Dah! It works fine now. 0 warnings and 0 errors. It must have been an issue with the IDE.

lisati
December 5th, 2009, 05:23 PM
Edit (crossed posts): good news that it now works!

dwhitney67
December 5th, 2009, 09:19 PM
To A_Fiachra, well, uh, I'm not exactly sure how i'm linking, but I compile by pressing the compile button...

That's rich.

Consider learning what the tool does before making yourself dependent upon it.