Xcode 3.2.1 and C++ string fails! - c++

In Xcode 3.2.1 on Mac OS X Snow Leopard, I open a project under:
Command Line Tool of type C++ stdc++.
I have the following simple code:
#include <iostream>
#include <string>
using namespace std;
int main(){
string myvar;
cout << "Enter something: " << endl;
cin >> myvar;
cout << endl << myvar << endl;
return 0;
}
The program compiles fine, and prompts me to "Enter Something". When I type in something, and then press enter, I get the following error:
myproject(766) malloc: *** error for object 0x1000041c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
sharedlibrary apply-load-rules all
(gdb)
When compiling on an earlier version of Xcode (3.1.2) on a different computer (opened the project with the 'command line utility' option, which does not exist in 3.2.1), the code runs with NO PROBLEM.
Does anybody know what's going on?
Thanks,
Yuval

As far as I can tell, I'm not experiencing this issue in Release mode for x86_64. But I am seeing the issue in Debug x86_64. If I follow the directions given by Howard in this post, I'm able to get it running in debug mode:
Project -> Edit Active Target ...
Click Build tab
Search for "preprocessor"
Delete _GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1
Build and run, you'll notice it works. Another interesting observation is that using __gnu_debug::string (from the <debug/string> header) alone does not trigger the error.
EDIT: from the horses mouth (known issues in XCode 3.2.1)
The default gcc 4.2 compiler is not compatible with the Standard C++ Library Debug Mode. C++ programs compiled with Xcode 3.2 may not work in the Debug configuration. To fix this, set the Compiler Version to 4.0, or edit the Debug configuration’s Preprocessor Macros and remove the entries:
_GLIBCXX_DEBUG=1 _GLIBCXX_DEBUG_PEDANTIC=1
You can do this for all projects by navigating to /Developer/Library/Xcode/Project Templates/Application/Command Line Tool/C++ Tool/C++Tool.xcodeproj/ and editing project.pbxproj and deleting the lines around line 138:
"_GLIBCXX_DEBUG=1",
"_GLIBCXX_DEBUG_PEDANTIC=1",

An easier way of accomplishing the same thing: paste these lines at the very beginning of your program (before any #include statements):
#define _GLIBCXX_FULLY_DYNAMIC_STRING 1
#undef _GLIBCXX_DEBUG
#undef _GLIBCXX_DEBUG_PEDANTIC
Obviously, this will only affect the current project.

Related

String variables don't work in Eclipse CDT

When I use string variables in Eclipse CDT (MinGW compiler) and I run the program, it doesn't show me anything. Here's my code:
#include <iostream>
using namespace std;
int main() {
string hw = "Hello, world!";
cout << hw << endl;
return 0;
}
So that doesn't show anything, but when I just do cout << "Hello, world!" << endl; it does work.
I also tried including <string>, <string.h>, "string" and "string.h" but still no success. The same code does work in VC++ and Codeblocks though.
I can reproduce this problem on my machine. The output does not appear. But it is not an output problem, but actually two problems before you get to main:
The program is failing to launch. If you try double-clicking on it in Windows Explorer, you will get an error message like this:
The program can't start because libgcc_s_dw2-1.dll is missing from
your computer. Try reinstalling the program to fix this problem.
Screenshot of above:
When launched from within Eclipse, this error message is silently swallowed, so how are you supposed to know!
Solutions/Workarounds
You need to get the environment correctly set up to launch the MinGW program because its DLLs are not installed on Windows PATH or other standard places DLLs are searched for.
Add C:\MinGW\bin to your PATH
Launch Eclipse from within a MinGW shell (has basically same effect as 1)
Run the program in debug mode, this causes the program to be launched as a child of GCC and that sets up
Other options (not tried by me) may include statically linking the offending library, see The program can't start because libgcc_s_dw2-1.dll is missing
File a CDT bug about the error message being hidden.
Extra info
If your program compiles, as I am sure it does based on your comments, changing the includes is probably irrelevant.
By adding an infinite loop around the couts I could immediately identify something more than simply an output not being shown was going on. Try the same thing on your machine, and also try running the program from within MinGW shell and from outside it.

Eclipse Kepler: #include "boost/filesystem.hpp" breaking debugging?

I have a fresh install of Eclipse Kepler on Windows 7 x64. My first "Hello world" worked great, including setting breakpoints and walking through code in debug mode. My second program links to Boost. I can set the breakpoints, build 'Debug' without errors, and run Debug, but unlike my Hello World, it blows through my breakpoints. The console window appears to flicker "Info: Nothing to build for..." and then clears. None of my std::cout calls appear.
With a little debugging I found that commenting out "filesystem.hpp" (below) enables the debugging, but of course I need it. I'm new to Eclipse. What would cause this behavior?
#include "boost/filesystem.hpp" <--- removing this enables debugging
#include "boost/regex.hpp"
#include <iostream>
int main()
{
std::cout << "Start search" << std::endl;
:
:
Look in the build output/problems views.
Most likely, you don't link to the required lib(s) for Boost Filesystem (-lboost_system -lboost_filesystem).
In effect this just means that no executable is built, and obviously therefore cannot be started.

No output in c++-program if object is instantiated using Eclipse

I got an issue after installing Eclipse Kepler on my Windows 7 32bit machine. I installed the CDT and the MinGW-compiler. I configured the installation by adding MinGW to the PATH and tested my configuration with a "Hello world"-program, which worked.
The strange thing is, that if I instantiate an object, nothing is outputted. It doesn't matter if it's a std::string or a custom made class. If I instantiate it, nothing is outputted, even if it should be outputted before the instantiation. The exact same code works fine, if I compile it with cygwin gcc from command line. If I change the toolchain to cygwin gcc nothing changes (I've rebuild the program with "build all").
There is no error displayed and no problem listed.
Here's the minimal working example:
#include <iostream>
#include <string>
using namespace std;
class SayWorld{
public:
SayWorld(){
cout << "World!" << endl;
}
};
int main() {
//Only gets outputted, if the lines, that don't work are commented out:
cout << "Hello ";
// Works:
cout << "World!" << endl;
// Doesn't work:
// SayWorld sw;
// Also doesn't work:
// string str("World!");
// cout << str << endl;
return 0;
}
Edit 2:
I narrowed the error to MinGW, as this picture of a Cygwin-Bash-Terminal demonstrates. (The file was not changed beetween the to g++ calls and contains the example above)
Edit 1 (Legacy)
Toolchain-picture:
-picture removed- (don't think it was necessary)
After reinstalling eclipse and MinGW again, step-by-step following this video tutorial:
http://www.youtube.com/watch?v=77xZOT3xer4
and having the same problem afterwards, I stumbled uppon this post in the eclipse forum:
http://www.eclipse.org/forums/index.php/u/104305/
which has brought me to this solution:
Right-click Project -> Properties -> Run/Debug Settings
Choose executable and hit the "New"-button.
Go to the Environment-Tab and create a new variable named PATH with the value: "C:\MinGW\bin".
As I am no expert, I can't explain to you why it works, but it worked for me. If someone knows, how to do this better or wholly avoid this problem, I'd be glad to listen.
This entry was definitively in my Windows-PATH...
PS.: The problem seems also to be known here:
unable to see the output of c++ program in the console

Trying to set up the GNU C++ COMPILER, but I get an error I don't understand when I try to compile hello.cpp

So I bought this book called C++ Programming In Easy Steps by Mike McGrath online.
In the instructions it specifies to create a source file written in C++, the infamous "helo world". So I created my cpp file through sublime text editor and moved it to a file called MyPrograms in my C directory.
The code is as follows:
#include <iostream>
using std::cout;
using std::endl;
int main()
{
cout << "hello world"<< endl ;
return 0 ;
}
I have also tried:
#include
using namespace std;
int main()
{
cout << "hello world"<< endl ;
return 0 ;
}
Ok so I saved this file as hello.cpp in C:\MyPrograms.
Then here is where the error occurs....
I open cmd.
I do "c++"
I receive the message "c++: no input files".Which is what I'm supposed to recieve according to the book.
I proceed to do "cd\myprograms" to enter into the MyPrograms directory.
Once in that directory I do "c++ hello.cpp". According to the book this is supposed to compile my source file and create an executable file next to it. Instead I get a long error message that end in collect 2: 1d returned 1 exit status.
When I visit MyPrograms no executable file has been made next to the original cpp file.
I have also tried to do "c++ hello.cpp -o hello.exe" but it gives me the error again. All of this is done on the command prompt.
Please help :(
It looks to me like MinGW isn't installed properly.
First, it looks like you are trying to use version 4.0.3 but it may be conflicting with a version 3.4.5 that you installed previously (one in c:\mingw and the other in e:\p\giaw\src\pkg).
The latest version of MinGW is 4.7.2.1 which you can install from here: http://www.mingw.org/wiki/InstallationHOWTOforMinGW
But it looks like you're just starting out and it may be better to work with something that's better optimized for Windows (unless you're trying to compile Free Software). You can get a copy of Visual Studio Express for free here:
http://www.visualstudio.com/downloads/download-visual-studio-vs#d-express-windows-desktop
There are older versions available as well if you scroll down (VSE 2010).

C++ using getline() prints: pointer being freed was not allocated in XCode

I'm trying to use std:getline() but getting a strange runtime error:
malloc: * error for object 0x10000a720: pointer being freed was not allocated
* set a breakpoint in malloc_error_break to debug
This is the code that produces this error:
//main.cpp
#include <iostream>
#include <sstream>
int main (int argc, char * const argv[])
{
std::istringstream my_str("demo string with spaces");
std::string word;
while (std::getline(my_str, word, ' ')) {
std::cout << word << std::endl;
}
return 0;
}
Before each word I get this error. From the comments it seems to be a OSX/XCode specific error. Any hints on that?
Update:
The error is only printed in Debug mode. If I build this code in Release mode everything is fine.
Update 2:
More info on that issue can be found here.
Solution:
Set
_GLIBCXX_FULLY_DYNAMIC_STRING=1
in your Preprocessor Macros in targets info build tab.
System info:
OSX 10.6.2 | XCode 3.2 | g++ 4.2 | debug config for i386
At least one person has reported problems with g++ 4.2.1 on Apple that seem possibly related to yours having to do with an improper configuration of the standard library with the _GLIBCXX_FULLY_DYNAMIC_STRING definition (not that I understand any of what I'm typing here).
You might get a bit of a clue from the newsgroup thread that includes this message:
http://gcc.gnu.org/ml/gcc-bugs/2009-10/msg00807.html