About g++ -O option - optimization

I run on Ubuntu 10.10.
man g++ talks about -O1, -O2, -O3 optimization options
I noticed that -O5 works also, as well as -O1000...
I used "g++ -v -O5 toto.cpp", but it is not clear to me what's the difference. What does -O5 do actually ?

-O5 currently does the same as -O3, as does -O1000. Optimization level 3 is currently the max, but the -O flag accepts a higher level anyway for forward compatibility. Proof:
$ g++ -O2 -Q --help=optimizers > O2
$ g++ -O3 -Q --help=optimizers > O3
$ g++ -O5 -Q --help=optimizers > O5
$ g++ -O1000 -Q --help=optimizers > O1000
$ diff O2 O3
[ ... lots of output]
$ diff O3 O5
$ diff O3 O1000
$

Related

Can I build my PC file (Oracle Pro*c) conditionally inside of my application's make file?

I'm attempting to combine my makefiles so I can simply build once and it will precompile the pc file completely before continuing to build the application. This should be possible but for the life of me I cannot figure it out. Here is my makefile (for redhat 7).
COMPILEDATE = $(shell date)
COMPILE=g++ -std=c++11 -Wall -Wuninitialized -g
OSTYPE = $(shell uname)
LIBDIR=../../lib/
INC=../../include/
FILES=myProcess
OBJS= myProcess.o \
sqlStuff.o
O8P=$(ORACLE_HOME)
O8P=/u01/app/oracle/11.2.0/client_1
ORACLE_HOME=/u01/app/oracle/11.2.0/client_1
PROC_LINES=proc lines=yes code=ANSI_C iname=sqlStuff.pc parse=partial iname=sqlStuff include=. include=$(ORACLE_HOME)/precomp/public include=$(ORACLE_HOME)/rdbms/public include=$(ORACLE_HOME)/rdbms/demo include=$(ORACLE_HOME)/plsql/public include=$(ORACLE_HOME)/network/public
all: $(FILES)
compileInfo.o : FORCE
$(COMPILE) -c compileInfo.cpp -o $# -I$(INC) -DCDATE="\"$(COMPILEDATE)\"" -DBUILD="\"$(LSWBUILD)\""
FORCE :
%.o : %.cpp $(INC)myProcess.h
$(COMPILE) -c $< -o $# -I$(INC) -DCDATE="\"$(COMPILEDATE)\""
sqlStuff.o : sqlStuff.c
gcc -g -Wall -O -c -lclntsh -I. -I$(ORACLE_HOME)/precomp/public -I$(ORACLE_HOME)/rdbms/public -I$(ORACLE_HOME)/rdbms/demo -I$(ORACLE_HOME)/plsql/lib -I$(ORACLE_HOME)/network/lib
sqlStuff.c : sqlStuff.pc
$(PROC_LINES)
myProcess: $(OBJS) $(LIBDIR)libbase.a $(INC)myProcess.h sqlStuff.o
$(COMPILE) -o myProcess$(OBJS) -L$(LIBDIR) -lbase
clean:
rm -f $(FILES)
rm -f sqlStuff
rm -f sqlStuff.c
rm -f sqlStuff.lis
rm -f $(OBJS)
rm -f core
rm -f *.out
rm -f *.log
rm -f *.err
My fault, I didn't explain what the issue was:
I'm compiling in netbeans using this build command: ${MAKE} -f Makefile. The error is PCC-S-02015, unable to open include file on my object that is not being precompiled, sqlStuff.o
Looking at the gcc command under sqlStuff.o : sqlStuff.c, it looks to me that there should be a -o sqlStuff.o flag to tell gcc that the output should be written to sqlStuff.o instead of the default, which is a.out.
Best of luck.

Why am I getting "nvcc fatal : redefinition of argument 'optimize'"?

I am trying to compile on MacBook Pro Retina with CUDA Driver Version: 7.0.36 and cuda toolkit 7.0 in a nVidia GT 750 M, the following code with its makefile but it gives me this error:
nvcc fatal : redefinition of argument 'optimize'.
Despite I have been able to compile and execute other programes with nvcc, with makefiles and so, now I am not.
Also, I have not been able to find something useful about this error so I ask it here if someone knows how to solve it. I am new with CUDA so if you need more information please ask for it.
Here is my Makefile.inc:
CXX := nvcc
OPTIM := -O3
DEBUG := -g -DOLB_DEBUG
CXXFLAGS := $(OPTIM)
ARPRG := ar
LDFLAGS := -O3
PARALLEL_MODE := OFF
OMPFLAGS := -fopenmp
BUILDTYPE := precompiled
INPUTDIR := ./input
OUTPUTDIR := ./output
INCDIR := ./inc
OBJDIR := ./obj
SRCDIR := ./HeatTransfer
BINDIR := ./bin
###########################################################################
## defines shell
SHELL := /bin/sh
and the Makefile:
###########################################################################
ROOT := .
include $(ROOT)/Makefile.inc
######################################################## Operational system
OS = $(shell uname -s)
MACH = $(shell uname -m)
HOST = $(shell uname -n)
WHOAMI = $(shell whoami )
###########################################################################
HeatTransfer := \
mesh\
stack
PROGRAM := $(BINDIR)/program
###########################################################################
OBJECTS := $(foreach file, $(HeatTransfer), $(OBJDIR)/$(file).o)
###########################################################################
all : compile link
###########################################################################
compile : $(OBJECTS)
$(OBJDIR)/%.o: $(SRCDIR)/%.cu
#echo Compile $<
$(CXX) $(CXXFLAGS) -I$(INCDIR) -c $< -o $#
###########################################################################
link: $(PROGRAM)
$(PROGRAM): $(OBJECTS)
#echo Link $#
$(CXX) $(LDFLAGS) -I$(INCDIR) $(foreach file, $(HeatTransfer), $(OBJDIR)/$(file).o) $(LDFLAGS) -I$(INCDIR) -o $#
###########################################################################
clean : cleanprog cleanobj
cleanprog:
#echo Clean rubbish files
#rm -f *~ core .tmpfile $(PROGRAM)
cleanobj:
#echo Clean object files
#rm -f $(OBJECTS)
###########################################################################
###########################################################################
The complete messege when I try to compile is this:
...Heat_Transfer_CUDA$ make
Compile HeatTransfer/mesh.cu
nvcc -O3 -I./inc -c HeatTransfer/mesh.cu -o obj/mesh.o
Compile HeatTransfer/stack.cu
nvcc -O3 -I./inc -c HeatTransfer/stack.cu -o obj/stack.o
Link bin/program
nvcc -O3 -I./inc ./obj/mesh.o ./obj/stack.o -O3 -I./inc -o bin/program
nvcc fatal : redefinition of argument 'optimize'
make: *** [bin/program] Error 1
The problem is arising due to the fact that your link command is specifying the -O3 switch twice:
nvcc -O3 -I./inc ./obj/mesh.o ./obj/stack.o -O3 -I./inc -o bin/program
^^^ ^^^
And this is not allowed - it will produce that error.
The problem seems to occur due to the fact that your makefile specifies the use of LDFLAGS twice here:
$(CXX) $(LDFLAGS) -I$(INCDIR) $(foreach file, $(HeatTransfer), $(OBJDIR)/$(file).o) $(LDFLAGS) -I$(INCDIR) -o $#
which should not be necessary. Something like this instead:
$(CXX) $(LDFLAGS) -I$(INCDIR) $(foreach file, $(HeatTransfer), $(OBJDIR)/$(file).o) -o $#
should probably fix the issue.

Using g++ with libc++

It's quite easy to get clang++ to use GCC's libstdc++ (-stdlib=stdc++), but how can I do the converse? On OS X Mavericks, the c++ system library is libc++, which means that basically libstdc++ cannot be used (if you mix with other c++ libraries such as boost that have been compiled with libc++). So, roughly, that means G++ is not usable... Unless I can ask it to use libc++ rather than libstdc++.
Thanks.
The script I use (EDIT)
I'm using the following script, g++-libc++, to run g++ on top of libc++, on Mac OS X with MacPorts (hence the -mp names.
#! /bin/sh
clangxx=clang++-mp-3.5
gxx=g++-mp-4.9
libcxx_includes=$($clangxx -print-search-dirs |
perl -ne 's{^libraries: =(.*)}{$1/../../../} && print')
exec $gxx -std=c++11 \
-isystem ${libcxx_includes}/include/c++/v1 \
-nostdinc++ -nodefaultlibs \
-lc -lc++ -lc++abi -lgcc_s.10.5 \
-Wl,-no_compact_unwind \
"$#"
Something like:
g++ -std=c++0x -nostdinc++ -nodefaultlibs -lc -isystem <path_to>/include/c++/v1 -lc++ -lc++abi -lgcc_s.10.5

Gcc error while compiling option -fPIC

I'm reading a book but I get one error while compiling with this code.
$ rm -f injection.dylib
$ export PLATFORM=/Developer/Platforms/iPhoneOS.platform
$ $PLATFORM/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2 \
-c -o injection.o injection.c \
-isysroot $PLATFORM/Developer/SDKs/iPhoneOS5.0.sdk \ -fPIC
$ $PLATFORM/Developer/usr/bin/ld \ -dylib -lsystem -lobjc \
-o injection.dylib injection.o \
-syslibroot $PLATFORM/Developer/SDKs/iPhoneOS5.0.sdk/
I've some trouble especially in this line:
$PLATFORM/Developer/usr/bin/arm-apple-darwin10-llvm-gcc-4.2 \
-c -o injection.o injection.c \
-isysroot $PLATFORM/Developer/SDKs/iPhoneOS5.0.sdk \ -fPIC
This is the error
arm-apple-darwin10-llvm-gcc-4.2: -fPIC: No such file or directory
how can I solve... what does it means?
It means you mistyped the command line:
stieber#gatekeeper:~$ gcc \ -fPIC
gcc: error: -fPIC: No such file or directory
gcc: fatal error: no input files
compilation terminated.
Seems the \ in the middle of the line makes gcc (and probably the llvm-gcc as well) stop considering arguments as options and always treats them as filenames.
stieber#gatekeeper:~$ gcc -fPIC
gcc: fatal error: no input files
compilation terminated.
gives the expected result.

GCC multiple optimization flags

I have some legacy code that compiles with both -02 and -03 set. From the GCC man file I get the guarantee that:
-O3 turns on all optimizations specified by -O2 and also turns on the -finline-functions, -funswitch-loops, -fpredictive-commoning, -fgcse-after-reload and -ftree-vectorize
options.
So, at first glance it would seem likely that turning both of these flags on would be the same as just -O3. However, that got me thinking is that the right thing to do in that case as -O2 is probably the "safer" option. Obviously, it is a simple matter compile some code with all of the permutations and see what happens in each case, but I was wondering if anyone knows if there is a specific policy that GCC has in regard to specifying multiple optimizations levels and if so what is the reasoning behind it?
From the man page:
If you use multiple -O options, with or without level numbers, the last such option is the one that is effective.
For over-concerned users like my self, here is a code begging for optimization:
$ cat dominant_flag.c
#include <stdio.h>
int foo(int i)
{
return 3*i+122;
}
int main(int argc, char **argv)
{
return foo(0xface); // meant to be optimized out
}
And Here are four compilation scenarios:
$ gcc -g -O0 dominant_flag.c -o flag0
$ gcc -g -O3 dominant_flag.c -o flag3
$ gcc -g -O0 -O3 dominant_flag.c -o flag03
$ gcc -g -O3 -O0 dominant_flag.c -o flag30
Once I look for the constant 0xface, I see it exists in the non optimized versions:
$ objdump -S -D flag0 | grep -w "\$0xface" # 61e: bf ce fa 00 00 mov $0xface,%edi
$ objdump -S -D flag30 | grep -w "\$0xface" # 61e: bf ce fa 00 00 mov $0xface,%edi
and optimized out in the optimized versions:
$ objdump -S -D flag3 | grep -w "\$0xface"
$ objdump -S -D flag03 | grep -w "\$0xface"
The whole foo call is gone:
$ objdump -S -D flag03 | sed -n "297,298p;299q"
4f0: b8 e4 f0 02 00 mov $0x2f0e4,%eax # <--- hex(3*0xface+122)=0x2f0e4
4f5: c3 retq