Use concatenation and stringizing in the same macro with GCC - c

Those macros are compiled without error with visual studio and codewarrior compilers. With gcc the error is shown in the comment
#define STRINGIFY(x) #x
#define MYINC(n) STRINGIFY(extensions/##n##/myinc.h)
#include "extensions/1/myinc.h" // OK (no surprise)
#include STRINGIFY(extensions/1/myinc.h) // OK
#include MYINC(1) // error: pasting "1" and "/" does not give a valid preprocessing token
Some idea?

Actually On my environment (WSL2 gcc9.3), the line
#include STRINGIFY(extensions/1/myinc.h)
event triger compiling error
test1.c:6:10: error: #include expects "FILENAME" or <FILENAME>
6 | #include STRINGIFY(extensions/1/myinc.h) // OK
| ^~~~~~~~~
test1.c:7:17: error: #include expects "FILENAME" or <FILENAME>
7 | #include MYINC(1)
From GCC document https://gcc.gnu.org/onlinedocs/cpp/Include-Syntax.html I found this:
The argument of ‘#include’, whether delimited with quote marks or angle brackets, behaves like a string constant in that comments are not recognized, and macro names are not expanded. Thus, #include <x/*y> specifies inclusion of a system header file named x/*y.
And MSVC-170 document https://learn.microsoft.com/en-us/cpp/preprocessor/hash-include-directive-c-cpp?view=msvc-170 says:
You can organize constant and macro definitions into include files (also known as header files) and then use #include directives to add them to any source file.
So I guess the problem is some gcc won't expand macro. But if I define:
#define INCFILE "extensions/1/myinc.h"
#include INCFILE
GCC didn't report error. And If I write:
#define INCFILE "extensions/1/myinc.h"
#include STRINGIFY(INCFILE)
GCC also reports:
test1.c:5:10: error: #include expects "FILENAME" or <FILENAME>
5 | #include STRINGIFY(INCFILE) // OK

Related

Why is #define throwing "error: expected declaration specifiers"?

I'm new so apologies if my formatting is a little off.
I'm writing a simple OpenMP program in order to get the hang of it, and I've been stopped completely dead in my tracks by a strange compilation error. My serial implementation compiles just fine (with gnu11), but my parallel compilation seems to fail for some reason I can't locate.
The entire code up to the point of failure is as follows (and the error I receive from make follows thereafter)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#define N_THR 1
#define MIN_SIZE 3 //NOTE: min_size is inclusive here
#ifdef _OPENMP
#include <omp.h>
#undef N_THR
#define N_THR 4
#undef MIN_SIZE
#define MIN_SIZE N_THR
//omp_set_dynamic(false); //we need to explicitly disable dynamic teams to force 4 threads
omp_set_num_threads(N_THR);
#endif
gcc maze.c -o maze
gcc maze.c -fopenmp -o mazep
maze.c:11:16: error: expected declaration specifiers or ‘...’ before numeric constant
#define N_THR 4
^
maze.c:15:22: note: in expansion of macro ‘N_THR’
omp_set_num_threads(N_THR);
^~~~~
make: *** [Makefile:5: parallel] Error 1
Is there some deep C language syntax hint I'm missing or is it something a little more obvious?
It's because you are calling omp_set_num_threads() outside of a function.
I got this error in this file canCom2.h
in this line
#define INST_CANCOM2 (2U)
because I had an extra closing curly braces in this file
eeeprom.c
} /* extra curly braces*/
#if defined(USING_CAN2) && defined(INST_CANCOM2)
After removing the extra curly brace, the error vanished

#include <windows.h> causes errors

I'm trying to use GetTickCount() from the windows.h header file. Right now my code looks like this:
#include <stdio.h>
#include <string.h>
#include "declarations.h"
#define INPUTBUFFER 400 * 6
#define START_POS "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
#include <windows.h>
void interface() {
.....
.....
}
The compiler returns 2 errors:
Expected '{' before '(' token (pointing to the '(' after "interface"
2 or more data types in declaration specifiers (pointing to "interface")
When I comment out #include < windows.h >, the interface function works just fine. I don't even have GetTickCount() in my code yet. I checked the gcc path to make sure windows.h is there, so I'm not sure why I'm getting this error.
As RbMm points out in a comment, the windows headers contain "#define interface struct", so the compiler sees your code as "void struct() { ....}" which is not valid C++
You need to either rename your function (probablly the better option) or add a "#undef interface" before your function.

Include from preprocessor macro

I am trying include a file constructed from pre-processor macros, but running into a wall due to rules regarding tokens, it seems. I used the answer here as a reference: Concatenate string in C #include filename, but my case differs in that there are decimal points in the define I am using to construct my include. This is what I have currently that will not get through the preprocessor stage:
main.c:
#include <stdio.h>
#include <stdlib.h>
#define VERSION 1.1.0
#define STRINGIFY(arg) #arg
#define INCLUDE_HELPER(arg) STRINGIFY(other_ ##arg.h)
#define INCLUDE_THIS(arg) INCLUDE_HELPER(arg)
#include INCLUDE_THIS(VERSION)
int main(int argc, char **argv) {
printf(INCLUDE_THIS(VERSION));
fflush(stdout);
#if defined (SUCCESS)
printf("\nSUCCESS!\n");
#endif
return EXIT_SUCCESS;
}
other_1.1.0.h:
#define SUCCESS
Were I to use #define VERSION 1_1_0 and renamed the header accordingly it would work (but not viable for my use as I have no control over the name of the header files the actual project uses), but 1.1.0 is not a valid preprocessor token.
EDIT:
After a bit more digging through the documentation, I see that 1.1.0 is a valid preprocessing number; it is the resulting concatenation of other_1.1.0 that is invalid. Regardless, the issue of not being able to construct the include remains.
It's easy once you stop thinking about token concatenation. Stringification works with any sequence of tokens, so there is no need to force its argument into being a single token. You do need an extra indirection so that the argument is expanded, but that's normal.
The only trick is to write the sequence without whitespace, which is what ID is for:
#define STRINGIFY(arg) STRINGIFY_(arg)
#define STRINGIFY_(arg) #arg
#define ID(x) x
#define VERSION 1.1.0
#include STRINGIFY(ID(other_)VERSION.h)
See https://stackoverflow.com/a/32077478/1566221 for a longer explanation.
With some experimentation, I came up with a solution that, while not ideal, could be workable.
#define VERSION _1.1.0
#define STRINGIFY(arg) #arg
#define INCLUDE_HELPER(arg) STRINGIFY(other ##arg.h)
#define INCLUDE_THIS(arg) INCLUDE_HELPER(arg)
#include INCLUDE_THIS(VERSION)
Rather than pasting other_ and 1.1.0 together, I am pasting other and _1.1.0. I am not sure why this is acceptable as the resulting token is the same, but there it is.
I would still prefer to have a solution that allows me to just define the version number without the underscore, so I will hold off on accepting this answer in case someone can come up with a more elegant solution (and works for people who don't happen to need an underscore anyways)
If you are passing -DVERSION=1.1.0 as a compile-line parameter, rather than hard-wiring it in the source code, then there's nothing to stop you passing a second define using make or the shell to do the concatenation. For example, in a makefile, you might have:
VERSION = 1.1.0
VERSION_HEADER = other_${VERSION}.h
CFLAGS += -DVERSION=${VERSION} -DVERSION_HEADER=${VERSION_HEADER}
and then:
#include <stdio.h>
#include <stdlib.h>
#define STRINGIFY(arg) #arg
#define INCLUDE_HELPER(arg) STRINGIFY(arg)
#define INCLUDE_THIS(arg) INCLUDE_HELPER(arg)
#include INCLUDE_THIS(VERSION_HEADER)
int main(void)
{
printf("%s\n", INCLUDE_THIS(VERSION));
#if defined (SUCCESS)
printf("SUCCESS!\n");
#endif
return EXIT_SUCCESS;
}
which is basically your code with the #define VERSION line removed, and using the stringified version of VERSION_HEADER instead of trying to construct the header name in the source code. You might want to use:
#ifndef VERSION
#define VERSION 1.1.0
#endif
#ifndef VERSION_HEADER
#define VERSION_HEADER other_1.1.0.h
#endif
for some suitable default fallback version in case the person running the compilation doesn't specify the information on the command line. Or you might use #error You did not set -DVERSION=x.y.z on the command line instead of setting the default value.
When compiled (source file hdr59.c):
$ gcc -O3 -g -std=c11 -Wall -Wextra -Werror -DVERSION=1.1.0 \
> -DVERSION_HEADER=other_1.1.0.h hdr59.c -o hdr59
$ ./hdr59
1.1.0
SUCCESS!
$
I would put the three lines of macro and the #include line into a separate small header so that it can be included when the version header is needed. If the default setting is required too, then that adds to the importance of putting the code into a separate header for reuse. The program's source code might contain:
#include "other_version.h"
and that header would arrange to include the correct file, more or less as shown.

An interesting thing about overriding linux kernel macro

In the following code, it is compiled success and print 1024
#include <stdio.h>
#define FD_SETSIZE 512
#include <sys/types.h>
int main()
{
printf("%d\n", FD_SETSIZE);
}
But in the following code, it is compiled failed and print
test.c:4:1: warning: "FD_SETSIZE" redefined
In file included from /usr/include/sys/types.h:220,
from test_fd.c:3:
/usr/include/sys/select.h:81:1: warning: this is the location of the previous definition
the code is
#include <stdio.h>
#include <sys/types.h>
#define FD_SETSIZE 512
int main()
{
printf("%d\n", FD_SETSIZE);
}
Can anbody explain this? Thanks!
But in the following code, it is compiled failed and print
In the question, both the programs were compiled, but while compiling first program you got warnings in preprocessor stage.
Preprocessor stage is responsible for the replacement of macros.
In this example the preprocessor is using the last defined macro and replacing it.
#include <stdio.h>
#define FD_SETSIZE 512
#include <sys/types.h>
Here the definition of FD_SETSIZE is there in both the .c file and also in header file sys/types.h.
After the file inclusion, then the replacing of macros will be done,so the latest defined macro is replaced.
So the final replacement FD_SETSIZE of will be same as defined in the sys/types.h file and vice-versa.
Hope this is helpful.
you can use the #undef directive to remove the defined macro and replace it later
#ifdef MACRO
#undef MACRO
#endif
#define MACRO

C Macro - Dynamic #include

I'm trying to figure out how to build a variable string for the #include statement using GCC.
The idea is that for each source module I have written, I want to include as a header, a dynamically generated C source, that was created earlier in the build process.
Generating this file is not an issue. Including it, unfortunately, is.
What I have so far is (identities.h):
// identities.h
# define PASTER2(str) #str
# define PASTER(str) PASTER2(str ## .iden)
# define EVALUATOR(x) PASTER(x)
# define IDENTITIES_FILE EVALUATOR(__FILE__)
# include IDENTITIES_FILE
Ideally, this would be used like so (main.c):
//main.c
# include "identities.h"
int main() {return 0;}
Which would be expanded in a single pass by the preprocessor before compilation to yield:
//main.c (preprocessed)
# include "main.c.iden"
int main() {return 0;}
The two levels of indirection I'm using (PASTER and EVALUATOR) are a result of this post.
Unfortunately, this is not working and I am left with the error:
obj/win32/dbg/main.o
In file included from main.c:1:0:
identities.h:42:1: error: #include expects "FILENAME" or <FILENAME>
I think the problem is that the include statement is missing quotes.. Any ideas?
This is actually done in the Linux source tree; See line 100 of compiler-gcc.h.
#define __gcc_header(x) #x
#define _gcc_header(x) __gcc_header(linux/compiler-gcc##x.h)
#define gcc_header(x) _gcc_header(x)
#include gcc_header(__GNUC__)
I'm trying to figure out how to build a variable string for the #include statement using GCC.
This token pastes the value of __GNUC__ to a string; "linux/compiler-gcc" __GNUC__ ".h" and then stringifies the result. This maybe a gcc pre-processor extension.
Here is an example,
t1.h
#define FOO 10
t2.h
#define FOO 20
a.c
#ifndef VERSION
#define VERSION 1
#endif
#define __gcc_header(x) #x
#define _gcc_header(x) __gcc_header(t##x.h)
#define gcc_header(x) _gcc_header(x)
#include gcc_header(VERSION)
#include <stdio.h>
int main(void)
{
printf("FOO is %d\n", FOO);
return 0;
}
Here are two compiles,
g++ -o a a.cc
g++ -DVERSION=2 -o a a.cc
The output of either compile gives expected result.
As with the Linux source, you can key off of gcc pre-defined values. echo | g++ -dM -E - will give a list.
For your case, you can use the makefile to pass a define to the compile to allow dynamic inclusion of the generated header without altering the source. But then a simple alternative is just to run sed, etc on a template source file and replace it with the known include name.
Either technique is good for generating test fixtures, etc. However, for compiler feature discovery, this is a better method. For programmers who use IDEs, this might be their only choice.
I am fairly certain you can't do what you want, __FILE__ returns a string and ## works on tokens and there is no CPP string concat preprocessor macro. Normally this is gotten around due to the fact that two strings in succession e.g.
"Hello" " World"
will be treated as a single string by the C++ parser. However, #include is part of the preprocessor, and thus cannot take advantage of that fact.
Old answer:
Why are you doing this
{ #str, str ## .iden }
I'm certain that's not preprocessor syntax; what do you hope to achieve via that? Have you tried just:
str ## .iden
A '{' could explain the error you are getting.
What about BOOST_PP_STRINGIZE from the Boost Preprocessor library . It is specifically made to add quotes around a name.
Skipping the whole inclusion syntax thing for a while, I don't understand what your code is trying to do. You say:
# define PASTER(str) { #str, str ## .iden }
You give it main.c and expect "main.c.iden", but that returns {"main.c", main.c.iden }.
Are instead you looking for this?
#define PASTER2(str) #str
#define PASTER(str) PASTER2(str ## .iden)
You cannot use preprocessor like this. You have to supply a filename to the #include directive, it can't be some other macro.

Resources