make and -std=c++11

Pages: 12
Hello everyone,
I use ubuntu 16.04 and GNU Make 4.1
and while reading my "older" book at least it compiled
( my projet that is ). Then I started on the C++ tutorial and
on some of my modules I get this
-------
adisplay.cpp:9:20:
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11
int quelleRangee { 8 };
------
This is my makefile
objects = abase.o aglobals.o agenutils.o adisplay.o
aa: $(objects)
g++ -std=c++11 -o aa $(objects)
abase.o: abase.cpp
g++ -std=c++11 -c abase.cpp -std=c++11 -o abase.o
aglobals.o: aglobals.cpp
g++ -std=c++11 -c aglobals.cpp -std=c++11 -o aglobals.o
agenutils.o: agenutils.cpp
g++ -std=c++11 -c agenutils.cpp -std=c++11 -o agenutils.o
aisplay.o: display.cpp
g++ -std=c++11 -c adisplay.cpp -std=c++11 -o adisplay.o
I figure the makefile could be wrong or
statements like : int quelleRangee { 8 }; is not allowed.
So you are still getting this warning after adding the -std=c++11 flag? I would've expected the warning to go away.
It's only a warning so this particular piece of code shouldn't prevent you from actually building and running the program.
What compiler version do you have? To get details: g++ -v
Last edited on
g++ -v outputs the following :
gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.5)

Yes the whole she-bang builds and runs ok but what is weird
is that the aglobals.cpp module does not produce any warnings.
whereas I get warnings about locals in member functions
:?
Thanks for your patience
Hi

It looks as though you could do with an update to the compiler, the current version is 7.2 which does some of the c++17 standard. You may be able to do it with an apt command from the shell: I use Fedora, it worked for me using dnf which is the same kind of thing as apt.

When you compile turn on warnings, use the latest standard your compiler can do, use the following as a minimum:

g++ -std=c++14 -Wall -Wextra -pedantic-errors

There are other flags which are still not turned on but still handy, it's worth reading the gcc manual despite there being zillions of options.

Good Luck !!
Ubuntu in response to an apt-get script says :
gcc is already the newest version (4:5.3.1-1ubuntu1).
Maybe it's time for me to abandon the ubuntu ship :]

Thanks for the "ideas" :)
Michel Chassey
Ubuntu in response to an apt-get script says :
gcc is already the newest version (4:5.3.1-1ubuntu1).
Maybe it's time for me to abandon the ubuntu ship :]


It is probably saying that because that is the latest version in that repository. I wouldn't abandon the whole OS just because of that.
I did a little digging (amazing what can be found with an internet search) there are updates via PPA's. Remember in Linux there is always a way.

https://wiki.ubuntu.com/ToolChain Toolchain updates section
https://launchpad.net/~ubuntu-toolchain-r/+archive/ubuntu/test

https://gcc.gnu.org/install/

It is possible to manually build gcc which includes g++. The installation pages seem quite involved, but one doesn't have to supply that many options in order to build. The biggest thing is to specify which languages you want, so you don't get all of them ..... (Fortran, etc)

The most important thing of all is the warnings options I mentioned above when compiling, try that with your existing system and see what difference it makes. Warnings are your friend: they tell you what problems you have with your code. Just because something compiles with a couple of warnings doesn't mean the program will always work. When I am compiling, if I have warnings it means I haven't finished yet. Some people set the -Werror flag which turns all warnings into errors, forcing them to fix them all.

Good Luck
Hello,
with your last post I hit gold.
internet search indeed !
from bash I typed :
sudo add-apt-repository ppa:jonathonf/gcc-7.1
sudo apt-get update
sudo apt-get install gcc-7 g++-7

g++ -v prints out :
gcc version 5.4.1 20160904 (Ubuntu 5.4.1-2ubuntu1~16.04)
BUT :
g++-7 -v prints out :
gcc version 7.2.0 (Ubuntu 7.2.0-1ubuntu1~16.04)

imagine working whith a 20-year old toolchain :)
Thanks for the "ideas"

Michel Chassey
Cool, pleased you have it worked out :+)

How did you go with your make file? You would have had to change all the g++ to g++-7 and add the warning options I mentioned. Another thing, with the warning options - one only needs most of them for compiling the c++ code, not for linking a bunch of .o files together. I am not so sure you need to mention the -std=c++ option twice per line?
Here is the makefile as requested with the long lines "rem'd"
out.
# baby-step makefile 01-01-2018
objects = abase.o aglobals.o agenutils.o adisplay.o
options = -std=c++14 -Wall -Wextra -pedantic-errors

aa: $(objects)
g++-7 $(options) -o aa $(objects)

abase.o: abase.cpp
g++-7 -c abase.cpp -o abase.o
#abase.o: abase.cpp
# g++-7 $(options) -c abase.cpp \
# $(options) -o abase.o

aglobals.o: aglobals.cpp
g++-7 -c aglobals.cpp -o aglobals.o

#aglobals.o: aglobals.cpp
# g++-7 $(options) -c aglobals.cpp \#
# $(options) -o aglobals.o

agenutils.o: agenutils.cpp
g++-7 -c agenutils.cpp -o agenutils.o

#agenutils.o: agenutils.cpp
# g++-7 $(options) -c agenutils.cpp \
# $(options) -o agenutils.o
It seems to build OK.
Right now it's only displaying a chess board and waiting
for user input.
Again, thanks !
Michel Chassey

I'm slowly rewriting the code to
Awesome !
Do not forget that source files depend on the headers they include, both directly and indirectly. Fortunately for you, G++ can automatically generate dependency information in the form of recipes, so you don't need to manage that manually.

Nor should you need to write rules for every single file. Make is a lot more versatile than it appears from simple examples like this. GNU Make especially.

You might get away with something like the following:

1
2
3
4
5
CXX=g++-7
CXXFLAGS += -std=c++11 -MT $@ -MP -MMD -MF $(@:.o=.d) 
aa: $(patsubst %.cpp,%.o,$(wildcard *.cpp))
	$(LINK.cc) $(OUTPUT_OPTION) $^
-include $(patsubst %.cpp,%.d,$(wildcard *.cpp))

Consider skimming the manual at some point.
https://www.gnu.org/software/make/manual/make.html

Last edited on
You sure are shaking up this hobbyist :)
I did get away with the above 5-line wonder as long as I rename
unused .cpp files to .txt files.

My makefile now looks like a bash config file.
I consider myself the "somewhat awesome" noob :)
Michel Chassey
ps Do I go back to -std=c++11 from -std=c++14 ??
Do I go back to -std=c++11 from -std=c++14 ??


Now that you have g++7.2 , you could compile with c++17 :+) Even though you may not use all the language features of that standard, it's still worth doing, may as well have the latest :+) . I wouldn't go back to c++11, there are some handy things in c++14 like the use of auto for instance.
Boy, you really had this noob going for about 1/2 hour, trying to
find sources for g++-17.
p-|
Then the lights came on : 11 to 14 to 17.
As long as it's backward compatible with my old code, I'm going
for it. -std=c++17 sounds good.
there are some handy things in c++14 like the use of auto for instance.

auto was already available in C++11.
I did get away with the above 5-line wonder as long as I rename
unused .cpp files to .txt files.

That works, I guess, but ideally you would put each program in a separate directory.

Do I go back to -std=c++11 from -std=c++14 ??

That -std=c++11 flag was just a copy/paste bug. Use whatever you need. Although while you're at it, general advice is to add the compiler flags to your command line. -pedantic-errors -Wall -Wextra.

And yes, C++ revision N is mostly backwards-compatible with C++ revision N-1. In general, the committee tries hard to avoid breaking existing code, at least without a warning. In any event, the more language revisions you jump at the same time, the greater the danger of problems. As a beginner though, as long as you choose at least C++11, revision choice isn't a big deal.

Like @TheIdeasMan suggests, newer is probably better!
@Peter87

http://en.cppreference.com/w/cpp/language/auto

There were 2 new meanings for auto in c++11 , and 4 new meanings for c++14, now 2 more for c++17, plus 1 for concepts TS. Edit: I should have been more explicit in what I said :+)

mycuser wrote:
Boy, you really had this noob going for about 1/2 hour, trying to
find sources for g++-17.


Nothing like jumping in the deep end !

http://en.cppreference.com/w/cpp/symbol_index

This has a list of all the symbols in the std namespace, each is annotated with which C++ standard it comes from. You could look at the examples that are provided for many of them. But it doesn't have things like auto which is a key word, so isn't a member of the std namespace. Edit: Which standard things come from is annotated throughout the website. The cppreference site is awesome for detailed and technical standard conforming C++ reference. There is also reference right here at cplusplus, top left of the web page. This might be easier to understand for beginners :+)

Some other handy websites:

http://www.stroustrup.com/ The inventor of C++ !!

https://isocpp.org/faq
https://isocpp.org/get-started

http://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines


If you are really keen, you could consider trying another compiler, like clang for instance. It is sometimes instructive to compile your code with a different compiler. The options for clang are almost identical to g++ , so it's not a learning curve to use it.
Last edited on
cmake_minimum_required(VERSION 2.6)

PROJECT(Test)

if(UNIX)
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=gnu++0x")
endif()

http://www.cetpainfotech.com/technology/unix-training

Getting the know more click here !!
Hello everyone,
right now I compile with the below makefile :

CXX=g++-7
CXXFLAGS += -std=c++17 -MT $@ -MP -MMD -MF $(@:.o=.d)
aa: $(patsubst %.cpp,%.o,$(wildcard *.cpp))
$(LINK.cc) $(OUTPUT_OPTION) $^
-include $(patsubst %.cpp,%.d,$(wildcard *.cpp))

As far as the compiler flags the CXXFLAG line doesn't tell me much.
I know, "RTFM". I am "slowly" learning.
Fast with the teaching, slow with the learning :/
Only last week, I was gcc'ing with just -std=c++11.

I will be trying this :
OUTPUT_OPTION += -pedantic-errors -Wall -Wextra

CXX=g++-7
CXXFLAGS += -std=c++17 -MT $@ -MP -MMD -MF $(@:.o=.d)
OUTPUT_OPTION += -pedantic-errors -Wall -Wextra
aa: $(patsubst %.cpp,%.o,$(wildcard *.cpp))
$(LINK.cc) $(OUTPUT_OPTION) $^
-include $(patsubst %.cpp,%.d,$(wildcard *.cpp))

Michel Chassey



Pages: 12