Hi,

I am trying to pass a string to a MACRO using -D option in gcc. I am not getting desired output, below is snippet, please let me know what I am missing.

$ gcc -o finame -D CORE="op1_log.txt" finame.c

#define STR(x)  x

int main()
{
        FILE *fp;

        char filename[30];
        strcpy(filename,STR(CORE));

        fp = fopen(filename,"w");

        printf("%s\n",filename);
        if(fp == NULL)
                printf("Can not open File\n");
}

Space between -D and CORE is the problem

$ gcc -o finame -DCORE="op1_log.txt" finame.c

Edited 5 Years Ago by thekashyap: n/a

HINT: You should ideally check if CORE is defined or not. So better code is:

#include <stdio.h>
#include <stdlib.h>

#define STR(x)  x

int main()
{
        FILE *fp;

        char filename[30];
#ifdef CORE
        strcpy(filename,STR(CORE));
#else
#error "Must provide -DCORE=value while compiling this file."
#endif

        fp = fopen(filename,"w");

        printf("%s\n",filename);
        if(fp == NULL)
                printf("Can not open File\n");
}

Source: gcc
You can also provide a default value using "#define"

Edited 5 Years Ago by thekashyap: n/a

Thanks for your reply but still same problem,, below error messgae

finame.c: In function ‘main’:
finame.c:12: error: ‘op1_log’ undeclared (first use in this function)
finame.c:12: error: (Each undeclared identifier is reported only once
finame.c:12: error: for each function it appears in.)

Thanks for your reply but still same problem,, below error messgae

finame.c: In function ‘main’:
finame.c:12: error: ‘op1_log’ undeclared (first use in this function)
finame.c:12: error: (Each undeclared identifier is reported only once
finame.c:12: error: for each function it appears in.)

Probably because of missing #includes..
Code I posted in my second post works for me.. haven't executed but it compiles with warnings.

Can you post the full command with output? Also re-post the code you're compiling, just to be sure..
I'm using gcc.

Here is the below code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define STR(x) x

int main()
{
        FILE *fp;
        char filename[30];
#ifdef CORE
        strcpy(filename,STR(CORE));
#else
#error "Must provide ...."
#endif
        fp = fopen(filename,"w");
        printf("%s\n",filename);
        if(fp == NULL)
                printf("Can not open File\n");
}

Compiler Error Message

[wipro@localhost code]$ gcc -o finame -DCORE="log.txt" finame.c 
finame.c: In function ‘main’:
finame.c:11: error: ‘log’ undeclared (first use in this function)
finame.c:11: error: (Each undeclared identifier is reported only once
finame.c:11: error: for each function it appears in.)

I am also using gcc

Edited 5 Years Ago by rajeevpareek: Typoerror

So close, yet so far. Isn't the preprocessor a bitch to work with for anything but the simplest of replacements? ;)

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define XSTR(x) #x
#define STR(x) XSTR(x)

int main()
{
        FILE *fp;
        char filename[30];
#ifdef CORE
        strcpy(filename,STR(CORE));
#else
#error "Must provide ...."
#endif
        fp = fopen(filename,"w");
        printf("%s\n",filename);
        if(fp == NULL)
                printf("Can not open File\n");
}

Double quotes in the command line will be consumed by the shell, so you need to stringize the macro value to use it in string context. The STR macro gets that value and passes it on to another helper macro, XSTR, which uses the stringize preprocessor operator. Note that if you don't use this delegation and just use the stringize operator directly:

#define STR(x) #x

STR(CORE) will produce "CORE" rather than "log.txt".

Edited 5 Years Ago by Narue: n/a

Interesting that it compiled for me! Don't recall exactly what I passed on command line though..
Anyway, one other hint, gcc -E is quite helpful to find pre-proc related errors. Even if you have no errors I recommend you have a look at the pre-proce'd file at least once in your life.. :) $ gcc -E -o preprocd_finame.c -DCORE="log.txt" finame.c

This question has already been answered. Start a new discussion instead.