Preprocessor -D option and " characters

Other misc things
Post Reply
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Preprocessor -D option and " characters

Post by DarwinNE »

Hi to everybody,
I'm trying to compile with z88dk a moderately large multiplatform project, where some files are generated automatically.

I have code that is similar to this one:

Code: Select all

#include<stdio.h>

int main(void)
{
    printf("Hello " WORLD_NAME "\n");
    return 0;
}
Normally, on other compiler such as gcc I can use the -D option to define the macro WORLD_NAME:

Code: Select all

$ gcc --version
Configured with: --prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 10.0.0 (clang-1000.11.45.5)
Target: x86_64-apple-darwin17.7.0
Thread model: posix
InstalledDir: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin
$ gcc -DWORLD_NAME=\"Jupiter\" hello.c
$ gcc -DWORLD_NAME=\"Jupiter\" hello.c -o hello
$ ./hello 
Hello Jupiter
However, with zcc it seems that I can not use the " characters in the macro, so I get an error while compiling
(zcc - Frontend for the z88dk Cross-C Compiler - v16709-7140f95c5-20200720)

Code: Select all

$ zcc +zx -DWORLD_NAME=\"Jupiter\" hello.c
hello.c:5:22: error: Missing token, expecting ) got J
hello.c:5:22: fatal error: Expected ';'
Compilation aborted
Is there a workaround for this?
User avatar
dom
Well known member
Posts: 2076
Joined: Sun Jul 15, 2007 10:01 pm

Re: Preprocessor -D option and " characters

Post by dom »

Yes, lots of escaping! The preprocessor is invoked by system() so another level of escaping is needed it seems.

Take a look at the makefile here: https://github.com/z88dk/z88dk/blob/mas ... h/Makefile which passes in a string that's used in a similar way to your example.
DarwinNE
Member
Posts: 18
Joined: Sat Dec 21, 2019 8:33 pm

Re: Preprocessor -D option and " characters

Post by DarwinNE »

Many thanks!
I suspected it was an escaping problem, but I was clueless about how to properly doing it.

Now everything works correctly =D
Post Reply