I want to add these santizers to VS Code.
g++ -std=c++17 -Wshadow -Wall -o "%e" "%f" -g -fsanitize=address -fsanitize=undefined -D_GLIBCXX_DEBUG
I'm using ubuntu. Help me how to do it.
Related
I am attempting to compile a program that uses ranged based for loops, and a couple other features available only in c++11 and above. When I attempt to compile the program using a makefile in the terminal, I get this error:
error: range-based ‘for’ loops are not allowed in C++98 mode
and some warnings:
warning: extended initializer lists only available with -std=c++11 or -std=gnu++11 [enabled by default]
What's annoying is that this has happened before, but it just resolved itself. However, this time it has not resolved itself.
Here's my makefile:
main: main.o
g++5 -std=c++11 -Wall -Werror -g *.cpp -o lab4
Here are some examples of things I have tried to change in the makefile, but did not work.
g++-5 -std=c++11 -Wall -Werror -g *.cpp -o lab4
g++5 -std=gnu++11 -Wall -Werror -g *.cpp -o lab4
g++5 -std=c++0x -Wall -Werror -g *.cpp -o lab4
g++ -std=c++11 -Wall -Werror -g *.cpp -o lab4
g++ -std=gnu++11 -Wall -Werror -g *.cpp -o lab4
All of the previous examples result in similar warnings and errors. What can I do to fix the problem?
I am just curious if the position of the standard selection switch (-std=c++11 for my case) is relevant in g++ command line or not. The reason is that the following:
g++ -ftest-coverage -fprofile-arcs -std=c++11
-ansi -fpermissive -finline-functions -Wno-long-long
-fvisibility-inlines-hidden -m64 -Wall -Wextra
-g -o CMakeFiles/common.dir/cryptoclass.cpp.o
-c /home/work/common/cryptoclass.cpp
does not compile, while the following:
g++ -ftest-coverage -fprofile-arcs
-ansi -fpermissive -finline-functions -Wno-long-long
-fvisibility-inlines-hidden -m64 -Wall -Wextra
-g -o CMakeFiles/common.dir/cryptoclass.cpp.o
-std=c++11 -c /home/work/common/cryptoclass.cpp
does compile. The only change is that the -std=c++11 was moved to the end of the switches.
g++ gives the following warning:
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.
Version:
g++ (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
As per documentation, -ansi option enables the c++-98/c++-03 standard.
If you set multiple standard options, the latter option overrides the former. Same applies to other mutually exclusive options such as optimization levels.
I have a rather complex multithreaded code, which I compile using gcc 4.8.1. When compiling with
g++ -c file.cc -march=native -mfpmath=sse -mpreferred-stack-boundary=4
--param inline-unit-growth=50 -ggdb3 -Wall -Wextra -Winit-self
-O2 -fPIC -funroll-loops -fforce-addr -rdynamic
the code produced crashes with segfault (which I was unable to debug, but the address of a struct all of a sudden differs from what it was when constructed, in particular, is no longer aligned to 32bytes as required by the code but only to 8bytes).
When compiling with -O1 instead, the code works fine. I then added all the optimisation flags that make the difference between -O1 and -O2. (To this end, I created two files O1-opts and O2-opts via
g++ -march=native -mfpmath=sse -mpreferred-stack-boundary=4
--param inline-unit-growth=50 -ggdb3 -Wall -Wextra -Winit-self
-O1 -fPIC -funroll-loops -fforce-addr -rdynamic
-Q --help=optimizers > O1-opts
g++ -march=native -mfpmath=sse -mpreferred-stack-boundary=4
--param inline-unit-growth=50 -ggdb3 -Wall -Wextra -Winit-self
-O2 -fPIC -funroll-loops -fforce-addr -rdynamic
-Q --help=optimizers > O2-opts
when diff O1-opts O2-opts provides the option differences). When adding all the option differences to -O1, the code generated still does not crash. This puzzles me. So my question is: shouldn't this give exactly the same result as with -O2? (and also: what is the likely cause of my problem?)
The point is that the -O2 option not only sets different flags, but also enables additional optimizations in contrast to -O1.
The FAQ section of the GCC Wiki has an appropriate entry for this.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I have a simple example to test my IDE, to get ready my C++ environment.
I'm currently using Codeblocks 13.12 in Ubuntu 14.04.
When I build my program, the compiler executes the following code, and the program will throw an error when running.
g++-4.8 -Wall -fexceptions -O3 -pedantic-errors -std=c++11 -Wextra -Wall -pthread -c /home/mikeldi/workspace/codeblocks/main.cpp -o obj/Debug/main.o
g++-4.8 -o bin/Debug/test obj/Debug/main.o
ERROR:
terminate called after throwing an instance of 'std::system_error'
what(): Unknown error -1
Aborted (core dumped)
In the other hand, if I execute the following line (note that I don't use -c before main.cpp) the program will execute without any problem.
g++-4.8 -Wall -fexceptions -O3 -pedantic-errors -std=c++11 -Wextra -Wall -pthread main.cpp -o main
So, my question is:
What does the -c do that makes the program not work?
Is there any way of setting codeBlocks so it doesn't use -c?
Thanks in advance,
If your program throws an error at runtime, the problem is much more likely to be in your code than in the compiler switches you're using. With that said, there are much more significant differences between the two compiler invocations than -c. In particular, the differences are
-fexceptions -march=corei7 -g
If you add those compile switches to your command-line invocation, as with
g++-4.8 -fexceptions -march=corei7 -g -O3 -pedantic-errors -std=c++11
-Wextra -Wall -pthread main.cpp -o main
you can recompile your code and see that it probably still crashes. Find out where by stepping through your code using the debugger of your choice, since the -g flag tells the compiler to include debugging information into the generated object file.
As PeterT said in the comment, both the compiler and the linker need -pthread
once that solved, the program worked fine:
g++-4.8 -Wall -fexceptions -O3 -pedantic-errors -std=c++11 -Wextra -Wall -pthread -c /home/mikeldi/workspace/codeblocks/main.cpp -o obj/Debug/main.o
g++-4.8 -o bin/Debug/test obj/Debug/main.o -pthread
Thank you everybody.
I'm trying to set windows programming enviroment for c++. I use the Visual Studio to write the code but my University wants me to use g++ compiler. So far I managed to link g++ with the PATH using cmd but that's not all. I have to use an alias which in linux is:
p1++="g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
I tried the same using the command doskey:
doskey p1=g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
and it works but whenever I use for example:
p1++ hello.cpp
it says
g++: fatal error: no input files
compilation terminated.
but if i use:
g++ hello.cc
It does compile, so my question is what does all that code mean and how can I get rid of that error?
Thanks
test.cpp (assumed to exist at C:\test.cpp):
#include <iostream>
int main(void) { std::cout << "hello" << std::endl; return 0; }
Here's what it looks like in my Windows CMD prompt:
NOTE: No quotes in alias, and it is assumed that g++ is already in your PATH.
C:\>set p1++=g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow
C:\>%p1++%
g++: fatal error: no input files
compilation terminated.
C:\>%p1++% test.cpp
C:\>a.exe
hello
C:\>doskey p1++=%p1++% $*
C:\>p1++ test.cpp
C:\>a.exe
hello
If you wanted to have the %p1++% environment variable persist across CMD prompts then you'd need to add an environment variable to your Windows User Profile.
On Windows 7:
Control Panel => System => Advanced System Settings => Environment Variables.
Create a new User Variable with name = p1++ and value = g++ -ansi -Wall -O2 -DNDEBUG -Wextra -Werror -Wno-uninitialized -Wno-sign-compare -Wshadow.