I am learning C++ so I would prefer a compiler without any optimisation to see how things work (yeah, I mean copy elision). I am using xCode so the question is what flags and where to set them to disable optimisations preferably for a whole project?
Asked
Active
Viewed 1,723 times
2
-
Possible duplicate of [XCode 4 - Release Mode Flags, Build Flags, Link Flags](http://stackoverflow.com/questions/15731896/xcode-4-release-mode-flags-build-flags-link-flags) It's not exactly the same question, but found it with a simple google search, and has an answer that applies this question. – Savvas Parastatidis Jun 06 '16 at 23:18
-
Most reasonable compilers will apply copy elision even in debug mode! The primary reason is that the code shouldn't behave differently between an optimized build and a debug build and copy elision is done in an optimized build. While [`gcc`](http://gcc.gnu.org) has `-fno-elide-constructors`, I didn't spot anything similar for [`clang`](http://clang.llvm.org). However, the latter is used by XCode as far as I know. – Dietmar Kühl Jun 06 '16 at 23:25
-
I used the -fno-elide-constructors flag and it worked fine. The thing is I set it for main.cpp in Build Phases->Compile Sources i.e on file basis. The other cpp files don't inherit the flag. Anyway it works at least for files I explicitly add to the list. – Jiří Lechner Jun 06 '16 at 23:47
-
Optimization is a compiler dependency. The language standard does not require optimization. Check your compiler documentation for instructions on how to disable optimizations. – Thomas Matthews Jun 06 '16 at 23:55
-
I would first wonder why this matters to you. Any optimization the compiler applies is not permitted to change the function of your program. That is, from the point of view of understanding C++, optimizations have no significance. – Dúthomhas Jun 07 '16 at 00:18
-
You are wrong, copy elision is permitted even if copy/move constructors contains side effects. As I stated I need it for educational reasons. – Jiří Lechner Jun 07 '16 at 00:21
2 Answers
0
I set the -fno-elide-constructors in Build Phase->Compile Sources as described in
It is not an ideal solution but it works and could be helpful for others.

Jiří Lechner
- 750
- 6
- 19
0
It appears by default xcode cpp compiler has some optimizations in place. One such is copy omission/copy elision. To disable this you need to provide this option (-fno-elide-constructors) during build of project target. Here are the steps to add compiler flags during building of a file in xcode -
- Select the project under which your target/executable belongs.
This should show something like this on your screen - screenshot you get when you select your project
Select the target for which you want to provide the compiler flags.
- select "Build Phases" -> "compile sources"
- Double click against the file which generates executable file.
- paste the compiler flag e.g. -fno-elide-constructors
- That is it. Rebuild you target and you should not see any copy constructors getting ommitted.

rajendra
- 1
-
1Given this is a beginner-y type of question I feel that I am entitled to ask this - how do I know whether that is the only compiler optimisation? – Scott Anderson Sep 10 '19 at 15:22