Page 1 of 1

Mac GCC and ifstream command line fix? [SOLVED]

Posted: Tue Aug 06, 2013 9:06 am
by jmcintyretech
Hey guys,

I'm trying to compile my code on OSX Snow Leopard (Virtual Machine). I'm using the GCC compiler WITHOUT XCode (http://kennethreitz.org/xcode-gcc-and-homebrew/). My code is all ISO C++ with no external libraries and compiles fine. However, when I run the program, ifstream does not read anything from the file. (I get blank lines where there should be stats for the game characters).

After DuckDuckGo-ing the problem I found a solution that has worked for developers using Xcode: Delete the preprocessor macros _GLIBCXX_DEBUG=1 and _GLIBCXX_DEBUG_PEDANTIC=1 from the project settings.

However, I am only using the GCC compiler derived from the one packaged with XCode, not the actual IDE. Does anyone know how to remove these options while compiling from the terminal?

Re: Mac GCC and ifstream command line fix?

Posted: Tue Aug 06, 2013 4:42 pm
by Falco Girgis

Code: Select all

 -D_GLIBCXX_DEBUG=0 -D_GLIBCXX_DEBUG_PEDANTIC=0
Although I highly, highly doubt that is your actual problem. I have used GCC from the command-line many times before, and that is what IDE's like QtCreator are doing behind the scenes. Are you positive that your file is actually in your application's working directory? Are you checking to see if the stream is valid or if the file has been opened properly before reading from it?

edit: Actually, this isn't necessary. Those aren't defined by default... which means this can't be your problem.

Re: Mac GCC and ifstream command line fix?

Posted: Tue Aug 06, 2013 6:17 pm
by jmcintyretech
Hmm. I'll add some code to check and see if the file is being opened and read, etc. Like I said this code is working properly in Linux and Windows, and the file is in the same directory as the executable. I'll see if I can further pinpoint where the problem lies. Thanks for the reply Falco!

Re: Mac GCC and ifstream command line fix?

Posted: Tue Aug 06, 2013 8:38 pm
by jmcintyretech
Looked into this further tonight and found an odd solution. If I navigate to the program directory in the terminal and execute it from there, it works fine. I only have this issue when clicking on the executable to run it. Huh. Maybe when you execute an application by double clicking it runs from a different location? I'll do some research and see why that is :) Thanks again for the help.

Re: Mac GCC and ifstream command line fix?

Posted: Wed Aug 07, 2013 9:38 am
by Falco Girgis
Interesting.

Try this at the command-line to display the working directory of your processes:

Code: Select all

lsof -d cwd

Re: Mac GCC and ifstream command line fix?

Posted: Wed Aug 07, 2013 2:06 pm
by jmcintyretech
Tried that handy command.

Turns out that when I double click the executable, it starts it in the home directory /Users/josh

No shit! There's the source of my problem :lol:

Thanks again :lol:

Re: Mac GCC and ifstream command line fix?

Posted: Wed Aug 07, 2013 2:39 pm
by Falco Girgis
Why the fuck... I wonder if there's some OSX-specific flags you can pass to GCC on OSX to make it disable retardism...

Re: Mac GCC and ifstream command line fix?

Posted: Wed Aug 07, 2013 7:39 pm
by jmcintyretech
g++ main.cpp -o main_osx -quit_being_a_dumb_os

:lol: :lol: :lol: