Code::Blocks Forums

User forums => Using Code::Blocks => Topic started by: johne53 on August 23, 2007, 12:55:04 pm

Title: #define'd strings (when compiling with g++)
Post by: johne53 on August 23, 2007, 12:55:04 pm
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");

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"

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?
Title: Re: #define'd strings (when compiling with g++)
Post by: MortenMacFly on August 23, 2007, 02:13:52 pm
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;
}
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.
Title: Re: #define'd strings (when compiling with g++)
Post by: Seronis on August 23, 2007, 02:59:56 pm
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
Title: Re: #define'd strings (when compiling with g++)
Post by: johne53 on August 23, 2007, 03:14:49 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;
}

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?
Title: Re: #define'd strings (when compiling with g++)
Post by: johne53 on August 23, 2007, 04:55:33 pm
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

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

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?
Title: Re: #define'd strings (when compiling with g++)
Post by: Ceniza on August 23, 2007, 05:08:12 pm
My guess is that it's caused by those \". Remove the backslash and try again.
Title: Re: #define'd strings (when compiling with g++)
Post by: johne53 on August 23, 2007, 05:17:19 pm
Thanks for the suggestion Ceniza but that was one of the 4 options that I originally tried. It produced Error 71 (in this case, - "libsndfile" undeclared).

I'm wondering if C::B actually sends something extra to the compiler that isn't showing up in my build log?
Title: Re: #define'd strings (when compiling with g++)
Post by: Ceniza on August 23, 2007, 05:34:05 pm
OK, then add another backslash: \\"
Title: Re: #define'd strings (when compiling with g++)
Post by: johne53 on August 23, 2007, 05:36:43 pm
Okay - I'll give that a try....

[Edit...] Interestingly, that does actually work but it gives me errors if I try to run the same command from a terminal window (i.e. the opposite way round from before). I'd like to understand what's going on here. Could anyone explain it to me?
Title: Re: #define'd strings (when compiling with g++)
Post by: thomas on August 23, 2007, 06:25:11 pm
If I understand correctly what you want, add to the #defines tab: MODULE="\"$PROJECTNAME\""

This will define MODULE as the project's name enclosed in quotes. New project, new name,... different output.
Title: Re: #define'd strings (when compiling with g++)
Post by: johne53 on August 23, 2007, 07:10:12 pm
Yes that almost works - except that I need to write it like this:-

MODULE=\\"$PROJECTNAME\\"

I'd really be grateful if anyone can explain why the double backslashes are needed.... :?
Title: Re: #define'd strings (when compiling with g++)
Post by: thomas on August 23, 2007, 09:11:52 pm
They aren't... I copied above text from my settings dialog after testing it (and it worked nicely).