User forums > Using Code::Blocks
#define'd strings (when compiling with g++)
johne53:
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:-
--- Code: ---sprintf (my_error_destination, "%s %s", MODULE, "an appropriate message");
--- End code ---
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.
--- Code: ---#define MODULE "My_Module_Name"
--- End code ---
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?
MortenMacFly:
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:
--- Code: ---#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;
}
--- End code ---
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.
Seronis:
Morton, I know its best to stay on-topic at given boards but there is also a tendancy for a lot of people to keep their web browsing to a few core sites. I'm one of them =-) Is there any possibility that the C::B forums could have a few general purpose programming sections added? I'm not asking for developers to take time away from C::B work to answer general language issues, but that doesnt mean there arent other visitors who would be willing to do so. And I think having those general purpose people have OTHER incentives to visit _here_ could be conductive to maintaining interest for new users.
just a random thought. "Wont Happen" as an answer wont offend me =-)
Seronis
johne53:
--- Quote from: MortenMacFly on August 23, 2007, 02:13:52 pm ---
--- Code: ---#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;
}
--- End code ---
--- End quote ---
Morten - sorry if my question wasn't clear, but I wasn't asking for programming advice. With reference to the #define, I know that the example you gave will work. What I was asking was whether or not it's possible to do the same thing using my project's build options? I tried (by guesswork) several options that I thought might work but none of them did. In each case, g++ wasn't able to understand what it was being sent. I was just trying to clarify whether it's possible to achieve this in my project options - or is it something that's only possible by editing my source code?
johne53:
Maybe I should rephrase my question here - because I do think there's probably a C::B problem at the bottom of all this.....
Forget about my example for the time being. Here is the actual command being sent to my compiler for one of the source modules (this one happens to be a plain old 'C' file but the C++ ones are similar). I've copied & pasted this output (unmodified) directly from the Build Log:-
--- Quote ---gcc -Wall -march=i686 -g -c -msse -mfpmath=sse -fPIC -Ilibs/libsndfile -DARCH_X86 -DUSE_XMMINTRIN -DBUILD_SSE_OPTIMIZATIONS -DHAVE_LIBLO -DENABLE_NLS -DPACKAGE=\"libsndfile\" -DVERSION=\"ardour-special\" -DDEBUG -I/usr/include/ -I/opt/gnome/include/glib-2.0/ -I/opt/gnome/lib/glib-2.0/include/ -I/media/SHAREDDATA/ardour2/libs/glibmm2/ -I/media/SHAREDDATA/ardour2/libs/sigc++2/ -I/media/SHAREDDATA/ardour2/libs/pbd/ -I/media/SHAREDDATA/ardour2/libs/ardour/ -I/media/SHAREDDATA/ardour2/libs/ -I/media/SHAREDDATA/ardour2/ -c /media/SHAREDDATA/ardour2/libs/libsndfile/src/broadcast.c -o obj/Debug/src/broadcast.o
--- End quote ---
Note how I've set up the preprocessor definitions for PACKAGE and VERSION. This command produces the following error messages when building this particular source module:-
--- Quote ---/media/SHAREDDATA/ardour2/libs/libsndfile/src/broadcast.c: In function ‘broadcast_add_coding_history’:
/media/SHAREDDATA/ardour2/libs/libsndfile/src/broadcast.c:71: error: ‘libsndfile’ undeclared (first use in this function)
/media/SHAREDDATA/ardour2/libs/libsndfile/src/broadcast.c:71: error: (Each undeclared identifier is reported only once
/media/SHAREDDATA/ardour2/libs/libsndfile/src/broadcast.c:71: error: for each function it appears in.)
/media/SHAREDDATA/ardour2/libs/libsndfile/src/broadcast.c:71: error: ‘ardour’ undeclared (first use in this function)
/media/SHAREDDATA/ardour2/libs/libsndfile/src/broadcast.c:71: error: ‘special’ undeclared (first use in this function)
Process terminated with status 1 (0 minutes, 1 seconds)
5 errors, 0 warnings
--- End quote ---
The 2nd, 5th and 6th red lines are the ones of most significance. Now for the interesting bit.... if I take that exact same command (copied from the Build Log directly to my clipboard) and I paste it straight into a terminal window and press <RETURN> - believe it or not, the compilation completes without any errors.
Now this is confusing me. Why should that exact same command be working correctly when issued from a terminal window - but not when issued from Code::Blocks?
Navigation
[0] Message Index
[#] Next page
Go to full version