Using preprocessor defined during compilation time - makefile

I want to define a preprocessor in compile time so that I can use it like this in the code
#ifdef MYFLAG
do some thing
#endif
I added that flag using AC_DEFINE([MYFLAG], [1], [My Flag]) in configure.ac and ran autoreconf. I can see it defined in configure file but still I can not use it.

Related

Step to pass "-std=c++11" flag in gdb regression test-suite setup

I am trying to run GDB testsuite with "-std=c++11" flag for the subtest case under "gdb.cp" folder or for the CPP related test-cases.
Currently I am performing it using DejaGnu directive "dg-options" in CPP test-case like :
// { dg-options "-std=c++11" }
But I am not confirm whether this is right way or not. Because if I check the gdb.log file, I am not seeing "-std=c++11" flag passed at compile time.
The easiest way would be, from the top-level build directory, to run the tests like:
make check-gdb RUNTESTFLAGS="CXX_FOR_TARGET='g++ --std=c++11' gdb.cp/*.exp"
GDB also supports CFLAGS_FOR_TARGET which can be set in the same way, despite being called CFLAGS these flags are appended for any compile, so you'll start to see warnings/errors about --std=c++11 being an invalid option for C tests, etc.
A final option that might be of interest would be creating a whole new DeJaGNU board file which would override the compiler flags, however, I couldn't get this working so quickly, so your mileage may vary.
You can find more information about running the tests here: https://sourceware.org/gdb/wiki/TestingGDB

Resolve conflicts with clang-tidy modernize-use-using

I am using the modernize-use-using checker from clang-tidy through the run-clang-tidy-3.9.py script over a whole CMake project (I use the compile_commands.json file generated by cmake).
This works great for other modernize checkers (auto, for loops, override, etc.), but the modernize-use-using checker finds "conflicts" like for instance:
There are conflicting changes to /home/OTB/git/otb/Modules/Filtering/ImageManipulation/include/otbVectorRescaleIntensityImageFilter.h:
The following changes conflict:
Replace 48:3-48:84 with "using RealType = typename itk::NumericTraits<typename TInput::ValueType>::RealType"
Replace 48:3-48:84 with "using RealType = typename itk::NumericTraits<typename VariableLengthVector<double>::ValueType>::RealType"
Replace 48:3-48:84 with "using RealType = typename itk::NumericTraits<typename VariableLengthVector<float>::ValueType>::RealType"
Looking at the source code, I don't understand why there would be such conflicts, but the issue is that because of these, no other fix is applied to my project. These are very few conflicts relative to the number of fixes, but I can't find a way to force clang-tidy to apply the rest.
The clang-tidy tool, when run alone has a -fix-errors option, but this is not available for the run-clang-tidy script which uses the CMake commands file.
Any suggestion would be appreciated.
Thanks

Why isn't Swift compiler flag being set?

I want some code to only be executed in a release build, but the code does not get executed when the scheme is configured to use the release configuration.
What am I missing?
I have the following in my app delegate's didFinishLaunchingWithOptions method:
#if RELEASE
Countly.sharedInstance().startOnCloudWithAppKey(appKey)
Crittercism.enableWithAppID(appID)
NSLog("crash logging enabled")
#endif
The target build settings look like this:
And the scheme is configured to use the Release configuration when I run the app:
You will need to set the preprocessor flags explicitly in the "Swift Compiler - Custom Flags" section, "Other Swift Flags" line.
Please check-
In absence of preprocessor macros, is there a way to define practical scheme specific flags at project level in Xcode project
It looks like the Swift compiler ignores -D flags that assign a specific value. If you use -DDEBUG and -DRELEASE, it seems to work.
now you need to set
Active Compilation Conditions
under Swift Compiler - Custom Flags
e.g.
Active Compilation Conditions DEV
you can check
#if DEV
print("DEV mode")
#else
print("PROD")
#endif

conditional include of sources in kbuild

Say there is a source file a.c with some function func_a. I would like to call func_a based on some feature define and would also like a.c to be included in the module based on ideally same feature define.
so in my module source that is always present (say main.c) I could have something like
#ifdef FEATURE_A
func_a();
#endif
and in the kbuild for the module something like
obj-$(CONFIG_SAMPLE) += sample.o
sample-objs := main.o utils.o
only if FEATURE_A is defined, include a.c into the sample module
???
But this may not make sense since one is a pre-processor directive and the other a compiler/linker/build directive. Maybe the other way where the pre-processor directive uses some flag defined by the kbuild makes more sense? Is there some way to accomplish this?
Thanks.
I will answer your question by pointing out how sysctl support is conditionally included in the NFS module (I'm sure there are other examples, but this is what I'm familiar with):
include/linux/nfs_fs.h
fs/nfs/Makefile
fs/nfs/super.c
The kernel config system maintains a file "include/linux/autoconf.h" that exposes your configuration options as C preprocessor macros. So the files listed above compile differently depending on whether you configured sysctl support.
If sysctl support is enabled: The header "include/linux/nfs_fs.h" checks the macro CONFIG_SYSCTL and declares the C function nfs_register_sysctl(). This function is called in "fs/nfs/super.c". The Makefile (seeing nfs-y += sysctl.o) directs the build system to compile in the file "fs/nfs/sysctl.c" into the module, which defines the function nfs_register_sysctl().
If sysctl support is disabled: The header "include/linux/nfs_fs.h" checks the macro CONFIG_SYSCTL and declares the preprocessor macro nfs_register_sysctl() to be 0. This macro is used in "fs/nfs/super.c" to bypass some (dead) error-handling code. The Makefile (seeing nfs-n += sysctl.o) does not compile or link "fs/nfs/sysctl.c".
Please read 3.7 Compilation flags.
As I understand it, there's loosless reletionship between Kconfig option and preprocessed macros, passed while compiling. It's up to kernel developer to define which preprocessor flags to use during compilation.
For example in net/rds/Makefile:
ccflags-$(CONFIG_RDS_DEBUG) := -DDEBUG
Here preprocessor flag DEBUG will be passed if you kernel is configured with Kconfig option CONFIG_RDS_DEBUG.

Xcode/GCC predefined macro for target name?

I was wondering if there is an Xcode or GCC preprocessor symbol for the target name of the application.
For example if I'm building an application called "MonkeyChicken", is there a preprocessor symbol such that
printf( __TARGET_NAME__ )
outputs:
MonkeyChicken
I don't believe there is any built-in (gcc has no idea what you're building when you compile a file), but you can always create one using GCC_PREPROCESSOR_DEFINITIONS in an xcconfig file (you are using xcconfig, right?) Something like this should work as you indicate above:
GCC_PREPROCESSOR_DEFINITIONS = __TARGET_NAME__=\"$(PRODUCT_NAME)\"

Resources