0

I need to have a string that uses a macro value which is an integer. But it's outside of any functions, so I do not want to create a variable. I'm using it in a #pragma comment.

so something like this:

#define num 7
#pragma comment(lib, "string" + num)

which would combine the string and num making it (#pragma comment(lib, "string7")

Kevin
  • 74,910
  • 12
  • 133
  • 166
Bullsfan127
  • 285
  • 6
  • 18

2 Answers2

4

What you want to do is called stringification:

#define stringify_1(x...)     #x
#define stringify(x...)       stringify_1(x)

#define NUM 7

char *p = stringify(NUM);

This is inspired by __stringify macro in include/linux/stringify.h in Linux kernel helpers.

ouah
  • 142,963
  • 15
  • 272
  • 331
  • He wants the pragma directive to become `comment(lib, "string7")` – obataku Sep 19 '12 at 17:54
  • I removed the `__` prefix to Linux macro names as names starting with `__*` are reserved for any use. – ouah Sep 19 '12 at 17:59
  • 1
    @oldring I don't have access to MSVC to check if `comment(lib, "string" stringify(NUM))` would work – ouah Sep 19 '12 at 18:03
3

I am not completely clear on the intent, it sounds like some preprocessor capability: http://gcc.gnu.org/onlinedocs/cpp/Stringification.html#Stringification

From that example you find this terse explanation that seems to be what you want.

#define xstr(s) str(s)
#define str(s) #s
#define foo 4
str (foo)
      ==> "foo"
xstr (foo)
      ==> xstr (4)
      ==> str (4)
      ==> "4"

So you would be able to do something like this:

#define xstr(s) str(s)
#define str(s) #s
#define num 7
#pragma comment(lib, "string" xstr(num))

Normal string merging rules should make that all fine if it were in actual code, but I am not sure if the string will automatically merge in the pragma. That is probably implementation dependent.