Author Topic: C++11 enabling problem  (Read 10633 times)

Offline AeroWB

  • Single posting newcomer
  • *
  • Posts: 4
C++11 enabling problem
« on: August 19, 2014, 04:24:40 pm »
Before submitting a bug report I wanted to check if this is indeed a bug or if I am missing something.
I am using Code::Blocks 13.12 on Ubuntu 14.04 LTS 64 bit on a Lenovo X240 Core i5 laptop.
My project requires the use of the C++ 2011 standard. So I went to project -> build options -> compiler flags and enable the build in "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" This adds the <Add option="-std=c++11" /> before the -Wall and -fexception options in the Project file, just as expected. However when compiling this option is ignored and the build log shows that only the -Wall and -fexceptions options are used.
I tried adding this option to target build option instead of the project build option but that did not help.
I tried adding the -std=gnu++11 option, but this also gets ignored.
Now I also added the -std=c++11 option to project -> build options -> other options. This option is also listed in the project file as a second <Add option="-std=c++11" /> below the -Wall and -fexception options and now when I press compile it works, the -std=c++11 is listed on the build log once after -Wall and -fexceptions.
Now when I disable the build in "Have g++ follow the C++11 ISO C++ language standard [-std=c++11]" copiler option, the  <Add option="-std=c++11" />  is only in my project file once and it is still listed below  -Wall and -fexception options but the project does not compile as the build log shows only the -Wall and -fexception options being used.

Conclusion: I have to add the -std=c++11 twice to enable it, and the same is true for the -std=gnu+11 option. Is this normal and by design or is this a Code::Blocks bug?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9612
Re: C++11 enabling problem
« Reply #1 on: August 19, 2014, 05:33:15 pm »
...could you provide a sample project file, please? (It works just fine here...)
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline killerbot

  • Administrator
  • Lives here!
  • *****
  • Posts: 5272
Re: C++11 enabling problem
« Reply #2 on: August 19, 2014, 06:55:46 pm »
I use this everyday and it works.

Can you enable in the compiler settings that it shows the full log (command) on the compiler invocation ,and paste it here please ?

Offline AeroWB

  • Single posting newcomer
  • *
  • Posts: 4
Re: C++11 enabling problem
« Reply #3 on: August 19, 2014, 08:28:55 pm »
Thanks for the replies, interesting to hear it just works for some.
The problem is even weirder as my project file which has both -std=c++11 options is not working when I load it in Code::Blocks, I have to add the c++11 option again in the build options before it works.

When I open below project and press build it fails with the error:
-------------- Build: Debug in wb (compiler: GNU GCC Compiler)---------------
gcc -Wall -fexceptions -pthread -g  -c /home/willem/Documents/CodeBlocksProjects/wb/wb.cc -o obj/Debug/wb.o
In file included from /usr/include/c++/4.8/thread:35:0,
                 from /home/willem/Documents/CodeBlocksProjects/wb/wb.cc:16:
/usr/include/c++/4.8/bits/c++0x_warning.h:32:2: error: #error This file requires compiler and library support for the ISO C++ 2011 standard. This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
And a lot more error after that.

Now when I right-click the project, select build options -> other options and add -std=c++11 the project builds ok.

If I remove all -std=c++11 options from the project file, open the project,  right-click the project, select build options -> other options and add -std=c++11 Code::Blocks says: The compiler flags -std=c++11 were stated in 'Other options' but unchecked in 'Compiler Flags'. Do you want to enable these flags?
When I select Yes and Build the project fails.
When I select No and Build the project also fails.
When I select Yes, right-click the project, select build options -> other options and add -std=c++11 again and Build the project works (and the project file has 2 times the c++11 defined but opening the project removes the c++11 option from other options and it will fail)

So I have to select the c++11 compiler flag once and I have to add the same setting to other options every time I open the project.

Project File:
Code: [Select]
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<CodeBlocks_project_file>
<FileVersion major="1" minor="6" />
<Project>
<Option title="wb" />
<Option pch_mode="2" />
<Option compiler="gcc" />
<Build>
<Target title="Debug">
<Option output="bin/Debug/wb" prefix_auto="1" extension_auto="1" />
<Option object_output="obj/Debug/" />
<Option type="1" />
<Option compiler="gcc" />
<Option projectLinkerOptionsRelation="0" />
<Compiler>
<Add option="-g" />
</Compiler>
</Target>
</Build>
<Compiler>
<Add option="-std=c++11" />
<Add option="-Wall" />
<Add option="-fexceptions" />
<Add option="-pthread" />
<Add option="-std=c++11" />
</Compiler>
<Linker>
<Add option="-pthread" />
</Linker>
<Unit filename="include/linux/can.h" />
<Unit filename="wb.cc">
<Option compilerVar="CC" />
</Unit>
<Extensions>
<code_completion />
<debugger />
</Extensions>
</Project>
</CodeBlocks_project_file>

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13069
    • Travis build status
Re: C++11 enabling problem
« Reply #4 on: August 19, 2014, 08:51:10 pm »
gcc -Wall -fexceptions -pthread -g  -c /home/willem/Documents/CodeBlocksProjects/wb/wb.cc -o obj/Debug/wb.o
Are you sure you've setup the compiler correctly?
Can you verify that the c++ compiler in the toolchain executables is set to g++ and not gcc?
gcc is the C language compiler, g++ is the one for C++!
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline jens

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: C++11 enabling problem
« Reply #5 on: August 19, 2014, 08:59:46 pm »
Simple solution (and cause for the issue):
don't use advanced options if you don't know exactly what they do (that's why they are called advanced)!

Right-click the file in the project-manager, click on "Properties" go to the "Advanced"-tab and change the "Compiler variable" back to "CPP" !
"CC" is the c-compiler and that can not work.

Offline AeroWB

  • Single posting newcomer
  • *
  • Posts: 4
Re: C++11 enabling problem
« Reply #6 on: August 19, 2014, 10:33:09 pm »
Thank you very much. Changing the Compiler Variable to CPP fixed it. The weird part is that I have never set this option myself so I was wondering what I did wrong.
Apparently I have started the project with the wizard and selected C in stead of C++ because that sets this option (I tried creating a new project, selected C and checked the project file)
I did a lot of coding, building and testing which al worked fine at first, until I started using threads.
gcc is fine for compiling C++ files but Code::Blocks apparently checks the compiler options and sees the option is for C++ while the project is set to C only and so it filters the option, while still allowing C++ code.
I have never written a C++ project before and also never worked with Code::Blocks so it is al new to me, and this was too confusing for me to solve.
Thanks for the help and thanks for creating this nice IDE.

Offline oBFusCATed

  • Developer
  • Lives here!
  • *****
  • Posts: 13069
    • Travis build status
Re: C++11 enabling problem
« Reply #7 on: August 19, 2014, 10:38:04 pm »
gcc is fine for compiling C++ files but Code::Blocks apparently checks the compiler...
Sorry but you're totally wrong! The executable gcc is not fine for c++ code! It is meant for compiling c code. The write executable for C++ code is g++.
GCC with capital letters is something totally different (Gnu Compiler Collection)!
(most of the time I ignore long posts)
[strangers don't send me private messages, I'll ignore them; post a topic in the forum, but first read the rules!]

Offline AeroWB

  • Single posting newcomer
  • *
  • Posts: 4
Re: C++11 enabling problem
« Reply #8 on: August 20, 2014, 02:14:18 am »
I was compiling C++ code with gcc (lowercase) and it was working fine, so gcc can compile C++ code. But I searched for the differences and if you use gcc to compile C++ you don't have access to the standard C++ libraries and there are some more differences. So you should indeed always use g++ instead of gcc for compiling C++ code, gcc is not fine for C++ code although it will work sometimes.