Overriding arguments in clang command line - command-line

If I give clang -O2 -O3 in the same command line, in that order, is the -O3 going to override the -O2 ? Does the later argument always override?
A build script which I can't change by default adds -O2 and I can only add things after it. Is that an acceptable thing to do?

Operation of the Clang driver is described in the manual page Driver Design & Internals § Driver stages. Note how you can use the -### option to get it to dump the result of each stage. This is not something you can exercise with your borken build system since the option must be listed first. But you can verify that the driver does in fact do what you hope it does:
clang -### foo.cpp -O2 -O3 # dumps yayayada "-O3" yadamore
clang -### foo.cpp -O3 -O2 # dumps yayayada "-O2" yadamore
Where “yada” is spew that I omitted since there’s too much of it. So, indeed, the last -O option you specify is the one that is effective. Which is the expected behavior for any compiler driver.

clang processes options left-to-right. Thus, the last -O option "wins". This is a feature exactly for the reason you ask: so there's a possibility to override defaults set by someone else (e.g. some build system, software developers, ...) Yes, it is totally acceptable, and you are in plenty of good company.
The ultimate reference would be the LLVM source code (option handling
is implemented by cl::ParseCommandLineOptions() in file lib/Support/CommandLine.cpp.)
Thinking outside the box: even if you cannot change the build script, you may influence it to do what you want. For example, the optimization option may be part of a variable that is taken from an option or from the environment. For example, if the build uses a Makefile, the variable could be called CFLAGS or COPTS and be set with
make CFLAGS=-O3
If the build uses a shell script, maybe something like
CFLAGS="-O3" ./configure
would work. There's no telling without seeing the build.

Related

How to quickly determine arm-none-eabi-gcc flags for a stm32 board?

I'm new to stm32 and I program on linux shell.
Everytime I watch a arm gcc makefile example, I saw a lot of gcc flags attached. I wanna know how to determine these flags for a specific type of board (stm32f10x, for example). Or should I say what documentation should I check for these information. Or these flags are basically the same for different boards?
Here's a makefile example I found in https://github.com/rowol/stm32_discovery_arm_gcc.git, and I don't know what flags like -mcpu, -mfpu are about.
# Put your stlink folder here so make burn will work.
STLINK=~/stlink.git
# Put your source files here (or *.c, etc)
SRCS=main.c system_stm32f4xx.c
# Binaries will be generated with this name (.elf, .bin, .hex, etc)
PROJ_NAME=blinky
# Put your STM32F4 library code directory here
STM_COMMON=../STM32F4-Discovery_FW_V1.1.0
# Normally you shouldn't need to change anything below this line!
#######################################################################################
CC=arm-none-eabi-gcc
OBJCOPY=arm-none-eabi-objcopy
CFLAGS = -g -O2 -Wall -Tstm32_flash.ld
CFLAGS += -mlittle-endian -mthumb -mcpu=cortex-m4 -mthumb-interwork
CFLAGS += -mfloat-abi=hard -mfpu=fpv4-sp-d16
CFLAGS += -I.
# Include files from STM libraries
CFLAGS += -I$(STM_COMMON)/Utilities/STM32F4-Discovery
CFLAGS += -I$(STM_COMMON)/Libraries/CMSIS/Include -I$(STM_COMMON)/Libraries/CMSIS/ST/STM32F4xx/Include
CFLAGS += -I$(STM_COMMON)/Libraries/STM32F4xx_StdPeriph_Driver/inc
# add startup file to build
SRCS += $(STM_COMMON)/Libraries/CMSIS/ST/STM32F4xx/Source/Templates/TrueSTUDIO/startup_stm32f4xx.s
OBJS = $(SRCS:.c=.o)
.PHONY: proj
all: proj
proj: $(PROJ_NAME).elf
$(PROJ_NAME).elf: $(SRCS)
$(CC) $(CFLAGS) $^ -o $#
$(OBJCOPY) -O ihex $(PROJ_NAME).elf $(PROJ_NAME).hex
$(OBJCOPY) -O binary $(PROJ_NAME).elf $(PROJ_NAME).bin
clean:
rm -f *.o $(PROJ_NAME).elf $(PROJ_NAME).hex $(PROJ_NAME).bin
# Flash the STM32F4
burn: proj
$(STLINK)/st-flash write $(PROJ_NAME).bin 0x8000000
The flags basically indicate what type of processor it is, this information is required by the compiler to cross compiler to the specific processor.
-mcpu: is the arm cortex type. e.g: cortex-m4, cortex-m7, cortex-m0 or cortex-m0+. The type of processor is the same for the different STM families. e.g. STM32F4 is a family of cortex-m4 processors. This information can be found in the datasheet and is often mentioned in the summary or specification when searching for chips.
-mfpu is the floating point unit type, or put another way what does a floating point look like on your system. Embedded Artistry has a nice write-up about this: https://embeddedartistry.com/blog/2017/10/11/demystifying-arm-floating-point-compiler-options/
-mfloat-abi is another flag you need for specific board, this can be hard or soft depending on the fact if the STM32 chip has a floating point unit or not. If it does you want to set this to hard.
Another thing that is important for cross compiling to different chips is specifying a linker script for your specific chip. This is the .ld file that is present and it specifies the memory regions and where all the code goes.
As a tip use STM32CubeMX to generate makefile projects for your given chip. STM32CubeMX is a tool from ST, which allows you to generate the project skeleton for several IDE's and also as a bare makefile. This makefile will give you all the above information including the linker script to compile for your specific target.
Another good reference is the GCC compiler documentation which you can find here: https://gcc.gnu.org/onlinedocs/gcc-10.3.0/gcc/Invoking-GCC.html#Invoking-GCC

gfortran compilation with -fPIC vs -frecursive

I recently followed the instruction on this thread for compiling BLAS and LAPACK as pre-requisites to a SciPy installation. First I got a gfortran error at some point, which recommended that I re-compile LAPACK with -fPIC. So I did this by replacing the -frecursive with -fPIC in makefile.inc (which, I assume is some file the Makefile reads for different compile options) and the error was gone.
Can someone explain more generally what the difference is in compiling something with -fPIC and -frecursive, and how it helped fix the error in my case.
Thanks!
As Soren already commented: -fPIC is totally unrelated to -frecursive. PIC influences how machine code can be positioned inside memory. If you want to compile a library code must be compiled to be relocatable. In other words the code must be able to run regardless of where it is loaded in memory. This question deals with this in more detail.
The -frecursive indeed should be specified if possible. Older implementations of gfortran, e.g. gfortran 4.1.2 on RedHat 5 do not support this option. Currently I haven't seen a workaround, so you have to remove it.
The gfortran documentation describes it as
Allow indirect recursion by forcing all local arrays to be allocated on the stack
In the thread you mentioned, for compilation of the LAPACK library the option -frecursive can be removed. Then the library compiles.
If it works without this feature remains to be seen. Haven't tested yet.

Rcpp+Eclipse on Mac OS X

I am trying to get started using Rccp and decided to use Eclipse as a development environment since I already use StatEt for R. I am having trouble getting even a simple program to compile and run though, and would appreciate some help!
Briefly I tried to follow the instructions on the blog: http://blog.fellstat.com/?p=170 exactly for setting up Rcpp, RInside and Eclipse, and for the example program. I am running on Mountain Lion, and installed g++ using the command line options in XCode. I think I've faithfully followed all the steps in the blog, but cannot get the program to compile. I think the problem is in the way the header files are included, as indicated from the snippet of the output below. As far as I can tell, line 52 of /usr/include/c++/4.2.1/cstring is an include statement for <string.h> and the compiler includes Rccp/include/string.h instead of the string.h from std that is found earlier on the include path.
I am a novice in C++ so I'd really appreciate some pointers on how to proceed.
-Krishna
16:22:38 **** Incremental Build of configuration Debug for project MyTestRCppPackage ****
Info: Internal Builder is used for build
g++ -DINSIDE -I/Library/Frameworks/R.framework/Versions/2.15/Resources/include -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include -O0 -g3 -Wall -c -fmessage-length=0 -arch x86_64 -v -o src/main.o ../src/main.cpp
Using built-in specs.
Target: i686-apple-darwin11
Configured with: /private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/src/configure --disable-checking --enable-werror --prefix=/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2 --mandir=/share/man --enable-languages=c,objc,c++,obj-c++ --program-prefix=llvm- --program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ --with-slibdir=/usr/lib --build=i686-apple-darwin11 --enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2336.11~182/dst-llvmCore/Developer/usr/local --program-prefix=i686-apple-darwin11- --host=x86_64-apple-darwin11 --target=i686-apple-darwin11 --with-gxx-include-dir=/usr/include/c++/4.2.1
Thread model: posix
gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)
/usr/llvm-gcc-4.2/bin/../libexec/gcc/i686-apple-darwin11/4.2.1/cc1plus -quiet -v -I/Library/Frameworks/R.framework/Versions/2.15/Resources/include -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp -I/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include -imultilib x86_64 -iprefix /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/ -dD -D__DYNAMIC__ -DINSIDE ../src/main.cpp -fPIC -quiet -dumpbase main.cpp -mmacosx-version-min=10.8.3 -m64 -mtune=core2 -auxbase-strip src/main.o -g3 -O0 -Wall -version -fmessage-length=0 -D__private_extern__=extern -o /var/folders/hc/vqp48jt56_v332kc3dqyf5780000gn/T//ccqdmOKI.s
ignoring nonexistent directory "/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
ignoring nonexistent directory "/usr/include/c++/4.2.1/i686-apple-darwin11/x86_64"
ignoring nonexistent directory "/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/../../../../i686-apple-darwin11/include"
#include "..." search starts here:
#include <...> search starts here:
/Library/Frameworks/R.framework/Versions/2.15/Resources/include
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/RInside/include
/usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/include
/usr/include/c++/4.2.1
/usr/include/c++/4.2.1/backward
/usr/local/include
/Applications/Xcode.app/Contents/Developer/usr/llvm-gcc-4.2/lib/gcc/i686-apple-darwin11/4.2.1/include
/usr/include
/System/Library/Frameworks (framework directory)
/Library/Frameworks (framework directory)
End of search list.
GNU C++ version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00) (i686-apple-darwin11)
compiled by GNU C version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00).
GGC heuristics: --param ggc-min-expand=150 --param ggc-min-heapsize=65536
Compiler executable checksum: b37fef824b01c0a99fb2679acf3b04f1
In file included from /usr/include/c++/4.2.1/cstring:52,
from /usr/include/c++/4.2.1/bits/stl_algobase.h:66,
from /usr/include/c++/4.2.1/memory:53,
from /usr/include/c++/4.2.1/tr1/hashtable:56,
from /usr/include/c++/4.2.1/tr1/unordered_map:37,
from /Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/platform/compiler.h:158,
from /Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/RcppCommon.h:26,
from /Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp.h:27,
from ../src/main.cpp:8:
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:52: error: 'internal' has not been declared
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:52: error: typedef name may not be a nested-name-specifier
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:52: error: expected ';' before '<' token
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:65: error: expected `)' before 'charsxp'
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:70: error: expected ',' or '...' before '&' token
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:75: error: expected unqualified-id before '&' token
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:75: error: expected ',' or '...' before '&' token
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:75: error: 'Rcpp::String::String()' cannot be overloaded
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:55: error: with 'Rcpp::String::String()'
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:85: error: 'Rcpp::String::String(int)' cannot be overloaded
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:70: error: with 'Rcpp::String::String(int)'
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:88: error: expected `)' before 'x'
/Library/Frameworks/R.framework/Versions/2.15/Resources/library/Rcpp/include/Rcpp/string.h:89: error: expected `)' before 'x'
There are two entirely separate issues here:
Get all you need for Rcpp installed. OS X aspects should be documented on the relevant page maintained by Simon. If you have the tools, and have Rcpp install, then you should be able to do cppFunction('double nPi(int x) { return x*M_PI; }') which is uses functions supplied with Rcpp to create a callable C++ functions accessible to you as nPi() -- and nPi(2) should return a value.
Your choice of IDE and its settings. This is has little to do with 1. apart from requiring it to work to.
So I would work on 1. and see if I got that sorted out first, and only then turn to 2.
To summarize, the issue I faced was that include files in Rcpp with the sames names as those in std were in conflict. In particular, string.h from Rcpp was being included at a point where string.h from std was the right choice, and, as far as I could tell, this was due to the fact that paths specified via the -I directive are searched prior to the default paths.
I tried many different alternatives to solve this, including removing and re-installing XCode and the associated Command Line tools, as well as installing another g++ compiler using macports. None of these resolved the issue. I then used the -idirafter directive instead of the -I directive for the search path for include files for Rcpp and R. I got this hint from gcc include order broken?. This worked since these directories are now searched after the default paths. This precludes (at least so far!) the possibility that string.h from std and string.h from Rcpp come into conflict.
To get step 5 of http://blog.fellstat.com/?p=170 to work I had to set the -idirafter paths in PKG_CPPFLAGS in the file Makevars.
Thanks to everyone for your suggestions.
You simply have to remove include
/Library/Frameworks/R.framework/Resources/library/Rcpp/include/Rcpp
because it is:
unnecessary, as all R imports are in form <Rcpp/XXX>
causes this issue, as compiler looks for string.h in Rcpp directory (when it shouldn't).

Unresolved __builtin_ia32_stmxcsr

I have inherited code, trying to compile with gcc on Linux.
what library am I looking for that has __builtin_ia32_stmxcsr ?
apologies -- i was too fast to submit; running gcc inside of Nvidia Eclipse. actual error message is "Functuion . . . could not be resolved" so i jumped the conclusion i needed to reference some lib. As the offending lines hav a :#if defined(SSE) I take it to mean that the -msse2 switch is present although i cannot seem to find a copyh of the compile command line. [just learning this Eclipse tool -- very new!]
You don't need to link with anything - the "builtin" in the name is a clue that it's a gcc built-in (intrinsic) compiler function.
However you do need to be compiling for an x86 target with SSE enabled for this to be recognised, e.g. gcc -msse2 ....
Note that you can use the _mm_getcsr intrinsic from <xmmintrin.h> instead of __builtin_ia32_stmxcsr - this would be a little more portable.
This is a bug in eclipses indexer with gcc's __builtin* functions. The bug report is at https://bugs.eclipse.org/bugs/show_bug.cgi?id=352537
The problem is that even the glibc/gcc libraries themselves use these __builtin* functions, so eclipse complains about a faulty xmmintrin.h etc., which is of course nonsense.
There is a workaround given in the bug report, you can add the function prototypes as user defined macros for the indexer, but of course this becomes tedious if there are a few more and some type checking abilities are lost.

How do you get NVCC to include macro definition information?

Normally with gcc you can specify the level of debugging information with -g and if you use -g3 it will include preprocessor macro definitions in the executable which debuggers like gdb can read and allow you to use during debugging. I would like to do this with nvcc for debugging CUDA programs.
I am currently working by modifying the template program in the SDK so I'm using the default Makefile and common.mk included from the Makefile. In common.mk within the 'ifeq ($(dbg), 1)' block, I have tried the following:
put -g3 under COMMONFLAGS
put -g3 under NVCCFLAGS
put -g3 under CXXFLAGS and CFLAGS
put --compiler-options -g3 under NVCCFLAGS.
The first two give an unrecognized option error. The second two do not seem to do the trick because when I debug using cuda-gdb I don't get the macro information.
The reason I would like to do this is because I would like to inspect some memory using the same macros the program itself uses to access that memory. For example,
#define ARROW(state, arrow) ((c_arrow_t *)(&((state)->arrows) + (arrow) * sizeof(c_arrow_t)))
#define STATE(nfa, state) ((c_state_t *)(&((nfa)->states) + (state) * sizeof(c_state_t)))
are some macros I use to access states and arrows of a non-deterministic finite state automaton.
Thank you for your help!
You probably need to pass both -g to nvcc to set it to build with host debugging, and also pass -g3 to the host compiler via the -Xcompiler (or --compiler-options).
Just an observation, but you really shouldn't be using that SDK makefile for anything. It is truly evil - a crude broken hack on some autotools generated make statements, which is both unnecessarily complex and very inflexible. Even the NVIDIA developers I interact with warn not to use it.