-1

I want to include the result of a macro expansion. It seems include only knows <> ""?

This fails:

#define audio sinwave

#ifdef audio
#include audio".c"
/*#include "sinwave.c"*/
#endif

But this works:

#ifdef audio
        if(i==0){set_audio((char *)audio);return;}
#endif
Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
eexpress
  • 155
  • 1
  • 10
  • what does set_audio() do? I guess what you are trying to do is conditional inclusion. If your included code is big then you can conditionally link to it in your build script(like Makefiles). If your code is small, then why put it in another file when you can replace it with macros or inline function. – Aftnix Sep 22 '12 at 10:58
  • Maybe explain why you want to do this? – Mike Sep 22 '12 at 13:29
  • this c only contain an array, the name of array is same as file name. – eexpress Sep 22 '12 at 14:39

3 Answers3

3

You could do something like this:

#define audio audio

#define FF(X) #X
#define F(X) FF(X.c)

#ifdef audio
#include F(audio)
#endif

that is you'd have to append the .c before you place everything into a string. The usual concatenation "audio" ".c" -> "audio.c" of adjacent strings happens in a later compilation phase than preprocessing, so an #include directive cannot deal with this.

Jens Gustedt
  • 76,821
  • 6
  • 102
  • 177
  • 1
    #define FF(X) #X #define F(X) FF(X.c) #define audio_array_name hello #ifdef audio_array_name #include F(audio_array_name) #endif #ifdef audio_array_name if(i==0){set_audio((char *)audio_array_name);return;} #endif .... it works. great... – eexpress Sep 22 '12 at 14:37
  • @eexpress, if this answer suits you, you could simply "accept" it, there must be a special button for that somewhere. – Jens Gustedt Sep 22 '12 at 16:08
  • i find that green tick. :D – eexpress Feb 16 '13 at 13:48
1

No. Preprocessor directives cannot be used like this. You can use macros to concatenate and stringify names, but that's another case. If you need this, you should most probably re-think your design because it's not good enough at the moment.

0

Maybe it's not clear to me... But I see a few different questions that you're asking...

I think your asking if you can include source files, yes you can, but its not the best idea; see here for a good discussion why.

If you're wondering about including files with "..." vs <...>, the difference is the quotes are when files are in your local directory (or you want to include the path to the file) the <> are for when the file is in your search path.

If you want to stringify the file name, then Jens Guestedt answer is what you want... But I question the logic behind doing this...

Why not include the .c file in your project normally (add it to your makefile or whatever) then just wrap the code in question (or the while file) in the #ifdef? That's a much more standard way to conditionally compile the code.

Community
  • 1
  • 1
Mike
  • 47,263
  • 29
  • 113
  • 177