What is the ruby equivalent of #define A B in C/C++? - ruby

Is there any Ruby equivalent of C/C++ macro?
#define something somethingelse

Ruby projects typically have neither a pre-processing nor a compilation step and usage of any pre-processor is not common.
You can define constants by using capitalized variable names.
However, if you really need a pre-processor then you can use CPP with ruby or any other language - it does not really care about the actual syntax.

There isn't a direct equivalent, but you can do it. For example,
#DEFINE PI 3.14159
in the C/C++ Preprocessor can be
PI=3.14159
in ruby. Capitalized variables are constants in ruby.
The other type of macro being, for example
#DEFINE ADD(A, B) A + B
is
def add(a, b)
a + b
end
in ruby. There is real reason to use a C/C++ style macro in ruby when it comes to function macros as you can easily define methods in ruby that work just as well as C/C++ style macros.

Related

What is __XSI_VISIBLE?

I was working on a C/C++ project for an embedded system that uses gcc-arm-none-eabi-8-2019-q3-update as a compiler.
I added the use of the strptime function of time.h but initially it was undefined and I found in the compiler inclusions:
#if __XSI_VISIBLE
...strptime...
#endif
So, I solved the problem with:
#undef __XSI_VISIBLE
#define __XSI_VISIBLE 1
#include <time.h>
#undef __XSI_VISIBLE
#define __XSI_VISIBLE 0
Now it works BUT:
What have I done?
What is __XSI_VISIBLE?
What is it for?
Why does this compiler keep it by default at 0?
From https://pubs.opengroup.org/onlinepubs/9699919799/:
The X/Open System Interfaces (XSI) option is the core application programming interface for C and sh programming for systems conforming to the Single UNIX Specification. This is a superset of the mandatory requirements for conformance to POSIX.1-2017.
The __XSI_VISIBLE macro makes visible extensions to "vanilla" POSIX interfaces, which otherwise would be forbidden to be in the name space. Remember that C language standards like ISO C and POSIX permit the application to define all non-standard identifiers (in ISO C and "vanilla" POSIX, strptime is not reserved, you can write a function with that name and have it not interfere). By defining so-called feature test macros you extend the set of standard identifiers and reduce those available to define by the application programmer.
Your compiler keeps it at 0 because the implementation vendor chose that it is the application programmer's job to enable XSI when s/he wants it. Application programmers do this by defining the desired feature test macros before header inclusion, e.g. with
#define _POSIX_SOURCE
#define __XSI_VISIBLE 1
#include <time.h>
or pass -D__XSI_VISIBLE=1 to the compiler.
The correct defs to use are -D_XOPEN_SOURCE=1 and -D_GNU_SOURCE=1. These are used to conditionally define __XSI_VISIBLE and __GNU_VISIBLE respectively in <sys/features.h>. Defining __XSI_VISIBLE and __GNU_VISIBLE will not always work because they are overridden in <sys/features.h>.

"string safe functions" and gcc

I'm using CodeBlocks and GCC compiler. I'd like to use "string safe functions" e.g strlen_s, strcpy_s, but compiler shows an error:
Undefined reference to strlen_s.
I then add a line to the code:
#define __STDC_WANT_LIB_EXT1__ 1
As well as writing the following in the Compiler Options (settings -> compiler -> global compiler settings -> other compiler options):
-std=c11
In the book that I'm reading there's a code to checking whether my compiler supports these functions. The code is as follows:
#include <stdio.h>
int main()
{
#if defined __STDC_WANT_LIB_EXT1__
printf("optional functions are defined");
#else
printf("optional functions are not defined");
#endif
return 0;
}
When I run this code I see "optional functions are defined". I've also reinstalled CodeBlocks but I still get these errors.
Should I install another compiler? If I should, which one will be the best?
#define __STDC_WANT_LIB_EXT1__ 1 is expected to be defined by your application - you have to define it yourself to enable the use of the bounds-checking interface functions.
In order to see if the bounds-checking interface is at all available, you need to check if __STDC_LIB_EXT1__ is defined by the compiler.
Note that no function called strlen_s exists.
This test is not sufficient, you should also test whether the implementation defines the macro __STDC_LIB_EXT1__.
These functions are from a part of the C standard that is called "Annex K" and that is optional. With this macro you test if your C library provides that feature, with the WANT macro defined before any includes you tell the compiler that you want to use these features from Annex K.
Annex K is much controversial, and not many public domain C libraries implement it. Many people think that its interfaces don't provide the security that it claims.
And for the book that you are reading this doesn't seem to be too reliable. But then, I may be biased on that point.

Writing custom GCC language front-end

Is there a way to write a front-end for my own, invented language?
Let's say I know regular expressions, yacc, lex and stuff, I want only to know how to bind it to existing GCC. I want something like:
%% This is a comment
%% This is source of XD programming language
troll x = 1; %% 'troll' is an automatically guessed type
x+laugh[]; %% Will print some laughs and format the hard drive
Here, the + represents C/C++ structure/class member operator, the square brackets are function call arguments. Semicolon means the same as in C.
How could I bind it so that if i entered
gcc -o app app.troll
it would compile my code, possibly with a similar C intermediate (anyways, the comments would probably be absent):
// The type has been guessed... It's int!!!
int x = 1;
// We can't call a method on an `int`, so we make it an external one
__trollvm_laugh_int (x);
And finally, it would be converted to GENERIC/GIMPLE, optimized and dumped to assembly.
I have seen two approaches:
G++ (GNU C++ Compiler, part of GCC). It compiles directly to GENERIC/GIMPLE
GPC (GNU Pascal Compiler) - translates code to C, then eventually calls GCC
If anyone knows anything, please let me know. Any help appreciated.

Since there is no preprocressor in Swift, what replaces C macros?

Is there a way to do #define, #ifdef and the other powerful macros in Swift?
Swift doesn't have a preprocessor and can't use C macros. There are some alternatives though.
For constants you can just use a let statement. For example:
let defaultHeight = 100
There is also some support for build configurations. They have this format.
#if build configuration && !build configuration
statements
#elseif build configuration
statements
#else
statements
#endif
You can replace "build configuration" with the functions os() and arch() that return true or false. os() can take OSX or iOS as arguments while arch() can take x86_64, arm, arm64 and i386 as arguments.
You can see more about how Swift replaces C macros here
Simple Macros
Where you typically used the #define directive to define a primitive constant in C and Objective-C, in Swift you use a global constant instead. For example, the constant definition #define FADE_ANIMATION_DURATION 0.35 can be better expressed in Swift with let FADE_ANIMATION_DURATION = 0.35. Because simple constant-like macros map directly to Swift global variables, the compiler automatically imports simple macros defined in C and Objective-C source files
reference: Page 40, Simple Macros
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/BuildingCocoaApps.pdf

question on C preprocessor definitions

Hi I am currently installing a software called super LU and in the README file there is the following instruction for modifying a makefile depending on system set-up.
C preprocessor definition CDEFS.
In the header file SRC/Cnames.h, we use macros to determine how
C routines should be named so that they are callable by Fortran.
(Some vendor-supplied BLAS libraries do not have C interfaces. So the
re-naming is needed in order for the SuperLU BLAS calls (in C) to
interface with the Fortran-style BLAS.)
The possible options for CDEFS are:
o -DAdd_: Fortran expects a C routine to have an underscore
postfixed to the name;
o -DNoChange: Fortran expects a C routine name to be identical to
that compiled by C;
o -DUpCase: Fortran expects a C routine name to be all uppercase.
A Makefile is provided in each subdirectory. The installation can be done
completely automatically by simply typing "make" at the top level.
I am not really sure what this instruction means. Which of these three options should I choose?
Just try building the software running make at the top level.
If there are linking problems because of missing BLAS functions
start experimenting with underscore.
So start with NoChange, then try Add_.

Resources