2

Here's the question:
I wrote a piece of code on Windows. When using /MT option, the answer is wrong. When simply switching to /MTd, the answer is correct.
Now I'm transplanting porting this code to Linux. The answer is wrong using the following command: g++ -Ofast -o test test.cpp. Also, I'm quite sure that this is the only problem left.
I've installed libc6, libc6-dbg, libc6-dev, libstdc++-4.8-dev, libstdc++6 and libstdc++6-4.8-dbg.
What's the problem in this situation and how can I solve it?

Update: Problem solved. Codes can run on Windows (Debug and Release mode) and Linux correctly with various optimization options. There were variable initialization problems. Thanks everyone.

9f241e21
  • 139
  • 4
  • 1
    this is the first time i'm seeing `porting` called `transplanting`. nice job on surprising me :) – Shark Mar 31 '16 at 13:21
  • If you want to build a debug version of your program, you add the `-g` flag when building using `gcc` or `g++`. That's all. – Some programmer dude Mar 31 '16 at 13:23
  • @JoachimPileborg, this is not the essence of the question, however. – SergeyA Mar 31 '16 at 13:24
  • @SergeyA And that's why I didn't write an answer. :) – Some programmer dude Mar 31 '16 at 13:25
  • @JoachimPileborg I tried `-g` flag but it's not working – 9f241e21 Mar 31 '16 at 13:26
  • 1
    http://stackoverflow.com/questions/312312/what-are-some-reasons-a-release-build-would-run-differently-than-a-debug-build – RyanP Mar 31 '16 at 13:28
  • 2
    That's because the problem you are having is something in your code, not something to do with flags. Having debug information (generated when you use the `-g` flag) will help you debug your application, either by using a debugger and stepping through the code, or by using a tool such as [Valgrind](http://valgrind.org/) which will find memory overwrites in your program. Other than saying that, there's nothing more we can do without any code, preferably a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Mar 31 '16 at 13:29
  • @JoachimPileborg Thanks. Great suggestions. I'll look into my codes. – 9f241e21 Mar 31 '16 at 13:30

2 Answers2

1

You need to fix your code. The program behaving differently with debug and release version of C++ runtime library (this is what is controlled by /MT and /MTd) means you have a bug in your program. Most likely undefined behavior.

The first thing would be change your compilation command to g++ -O3 -Werror -Wall -Wextra -pedantic -o test test.cpp and study compiler output. If you are lucky, you will see some warnings turned errors.

SergeyA
  • 61,605
  • 5
  • 78
  • 137
0

You have two problems.

  1. Your g++ compile command is release mode. Use the -g option to compile with debug symbols.

  2. You need to resolve on Windows why you don't get correct information in release mode with /MT.

TriskalJM
  • 2,393
  • 1
  • 19
  • 20