GCC #pragma or command options - c++

If the compiler has some command-line flags and the code has some pragmas that are incompatible with those flags, which one will be used?
To be clearer: I am compiling with g++ -g -O2 -std=gnu++17 -static {files} – GCC version g++ (Ubuntu 9.3.0-10ubuntu2) 9.3.0.
If I write in my code #pragma GCC optimize("Ofast"), will the final code be compiled with -O2 or with -Ofast?

That depends on if it's above or below the pragma.
void this_will_be_compiled_with_O2() { stuff(); }
#pragma GCC optimize("Ofast")
void this_will_be_compiled_with_Ofast() { stuff(); }

Although not explicitly mentioned in the documentation, the description of the #pragma GCC reset_options directive implies that any #pragma GCC optimize directive will override the command line option(s):
#pragma GCC reset_options
    This pragma clears the current #pragma GCC target and #pragma GCC optimize to use the default switches as specified on the command line.

Related

Unknown GCC pragmas on Mac

warning: unknown pragma ignored [-Wunknown-pragmas]
#pragma GCC optimize("O3")
I want to enable GCC optimization flags, but they are ignored when i'm compiling code, although they are present in documentation. Why does this happen?
The problem was that g++ is a clang alias in MacOS by default
So i did this
cd /usr/local/bin
ln -s g++-11 g++
this fixes the problem and g++ command will run actual gcc compuler instead of clang

Translation table for converting gcc compilation/link options to icc

For testing I would like to switch from using GCC to ICC for compiling my C/CXX-code. During compilation I am using command line options such as -mavx -march=native, which are set in my CMakeLists-file with
target_compile_options(${PROJECT_NAME} PUBLIC -mavx -march=native)
Now I also know that I have to replace -mavx in GCC with -xavx for ICC, but I do not know what I can use for the other compilation/link parameters. Is there a translation table between the options for GCC and ICC?

g++ Compilation option priority

when i build, using this line:
g++ -g -O2 -std=gnu++0x -static *.cpp
And my script had some other options like:
#pragma GCC optimize("O3")
#pragma comment(linker, ”/STACK:36777216“)
__attribute__((optimize("O3"))) void my_func()
{some code}
what will happen ? which one will be kept ?
Attributes or pragmas inside file take priority - so if you mark function with
__attribute__((optimize("O3")))
it will be compiled as if you compile it with -O3 regardless what you passed to gcc e.g. -O0.

Suppress -Wconversion for specific lines of code

I use a header file that provides inline functions. These functions are not always save in respect to the GCC -Wconversion check.
Now I want to use the -Wconversion check for my code but want to suppress the warning I get for the include file.
Edit: When I just add the conversion check to the compiler options I get the diagnostics, omitting -Wconversion gives me a clean compiler run.
Corresponding to this question I surrounded the include with some pragmas:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#include <lpc177x_8x_crc.h>
#pragma GCC diagnostic pop
Unfortunately this does not suppress the warnings.
warning: conversion to 'int32_t' from 'uint32_t' may change the sign of the result [-Wsign-conversion]
For an easy check you could even try this if you don't have CMSIS available:
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
int32_t foo(void)
{
uint32_t result;
return result;
}
#pragma GCC diagnostic pop
The compiler command line arguments are:
arm-none-eabi-gcc.exe -mthumb -Wshadow -Winit-self -Wredundant-decls -Wcast-align -Wunreachable-code -W -Wextra -Wall -Wformat=0 -Wconversion -g -O0 -ffunction-sections -fdata-sections -g3 -mcpu=cortex-m3 -c foo.c -o foo.o
I use arm-none-abi-gcc version:
gcc version 4.7.3 20121207 (release) [ARM/embedded-4_7-branch revision 194305] (GNU Tools for ARM Embedded Processors)
Since the warning message identifies the relevant flag as -Wsign-conversion, you should add that to the pragmas.
#include <stdint.h>
extern int32_t foo(void);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
#pragma GCC diagnostic ignored "-Wsign-conversion"
int32_t foo(void)
{
uint32_t result = 0;
return result;
}
#pragma GCC diagnostic pop
If you comment out the second ignored and compile with -Wconversion, you get the error:
$ gcc -O3 -g -std=c11 -Wall -Wextra -Wmissing-prototypes -Wstrict-prototypes -Wold-style-definition -Werror -Wconversion -c cvt.c
cvt.c: In function ‘foo’:
cvt.c:9:5: error: conversion to ‘int32_t’ from ‘uint32_t’ may change the sign of the result [-Werror=sign-conversion]
return result;
^
cc1: all warnings being treated as errors
$
If you uncomment that pragma, you get no warnings or errors.
(Tested on Mac OS X 10.9.1 Mavericks with GCC 4.8.2 — YMMV!) I note that the Apple-supplied clang (version 'Apple LLVM version 5.0 (clang-500.2.79) (based on LLVM 3.3svn)') does not object with the second ignored pragma commented out, with -Wconversion or with -Wsign-conversion, and I tried dinking with the code passing a uint32_t parameter and assigning that to result before returning it, etc (so it isn't simply superior optimization recognizing that 0 is special, or that returning an uninitialized variable is undefined behavior, etc.):
#include <stdint.h>
extern int32_t foo(uint32_t v);
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wconversion"
//#pragma GCC diagnostic ignored "-Wsign-conversion"
int32_t foo(uint32_t v)
{
uint32_t result = v;
return result;
}
#pragma GCC diagnostic pop

Why nasm says I have error in the assembly g++ created?

I have this c++ code:
#include <iostream>
using namespace std;
int main () {
char chr[] = "111111111111";
int pop = 9999;
cout << chr << (pop+1) << endl;
}
when I do in the shell (64 bit linux) g++ -S hello.cpp I get assembly code :
when I use on it nasm hello.s it says it contains a lot of errors such as:
instruction needed
expression syntax error
symbol `popq' redefined
maybe it is because it is 64bit? how can I compile the .s I created with the g++?
The assembler generated by GCC is using what is known as AT&T syntax, which differs from the Intel-syntax used by nasm. You have to use the GCC assembler (as) to compile GCC generated assembler files.
See e.g. http://en.wikipedia.org/wiki/GNU_Assembler#Criticism.
For more information about the GNU assembler syntax, see http://en.wikibooks.org/wiki/X86_Assembly/GAS_Syntax.
There are several assembler syntaxes for x86. In particular, nasm and gas (the assembler inside binutils) have different syntaxes.
Very often, GCC is configured to emit assembly code using gas syntax. You could find out what GCC is doing with eg g++ -O -v -c yourcode.cc and you can learn how GCC was configured with gcc -v or g++ -v alone.
And you might invoke GCC as g++ -S -fverbose-asm yourcode.cc to get a more readable yourcode.s