Suppose I want to write error messages like this (in C++) so that C::B will pass the correct #define (i.e. the correct substitution) to the compiler:-
sprintf (my_error_destination, "%s %s", MODULE, "an appropriate message");
where MODULE is a string that's #define'd on a project-by-project basis. Normally I'd write something like this in one of my source files, which would result in My_Module_Name being passed to sprintf(...) as a text string.
#define MODULE "My_Module_Name"
The above code should cause the compiler to look for every instance of MODULE and change it to "My_Module_Name". However, I can't find a way to do this using the #defines in my project's build options. If I try this #define:-
MODULE "My_Module_Name"
g++ complains that I've passed an int when sprintf() was expecting char*
Alternatively, if I write the #define like this:-
MODULE="My_Module_Name"
g++ produces error 71 - "My_Module_Name" undeclared
Alternatively, if I write it like this:-
MODULE=\"My_Module_Name\"
g++ tells me that "My_Module_Name" is not declared in this scope.
And if I try it like this:-
MODULE \"My_Module_Name\"
I get the same error that I got in the first example.
In fact, as far as I can tell, there doesn't seem to be any syntax that works correctly for a #define'd string. Can someone tell me if there's a correct way to #define a string within the build options for a C::B project?
Although it's *not* C::B related (and therefore should really not asked in out forum) I'll give you an example of code that works:
#include <stdio.h>
#include <stdlib.h>
#define MODULE "Module_Name"
int main()
{
char my_error_destination[50];
sprintf(my_error_destination, "Hello world %s!\n", MODULE);
printf(my_error_destination);
return 0;
}
In the future please don't ask non-C::B related question in this forum. There are plenty of C/C++ related forums/newsgroups around.
With regards, Morten.