Author Topic: #define'd strings (when compiling with g++)  (Read 12372 times)

Offline johne53

  • Regular
  • ***
  • Posts: 253
#define'd strings (when compiling with g++)
« 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: [Select]
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: [Select]
#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?

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9489
Re: #define'd strings (when compiling with g++)
« Reply #1 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: [Select]
#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.
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: http://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: http://wiki.codeblocks.org/index.php?title=FAQ

Offline Seronis

  • Almost regular
  • **
  • Posts: 197
Re: #define'd strings (when compiling with g++)
« Reply #2 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

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: #define'd strings (when compiling with g++)
« Reply #3 on: August 23, 2007, 03:14:49 pm »
Code: [Select]
#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?

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: #define'd strings (when compiling with g++)
« Reply #4 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?
« Last Edit: August 23, 2007, 04:58:23 pm by johne53 »

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: #define'd strings (when compiling with g++)
« Reply #5 on: August 23, 2007, 05:08:12 pm »
My guess is that it's caused by those \". Remove the backslash and try again.

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: #define'd strings (when compiling with g++)
« Reply #6 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?

Offline Ceniza

  • Developer
  • Lives here!
  • *****
  • Posts: 1442
    • CenizaSOFT
Re: #define'd strings (when compiling with g++)
« Reply #7 on: August 23, 2007, 05:34:05 pm »
OK, then add another backslash: \\"

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: #define'd strings (when compiling with g++)
« Reply #8 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?
« Last Edit: August 23, 2007, 05:50:46 pm by johne53 »

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: #define'd strings (when compiling with g++)
« Reply #9 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.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."

Offline johne53

  • Regular
  • ***
  • Posts: 253
Re: #define'd strings (when compiling with g++)
« Reply #10 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.... :?

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: #define'd strings (when compiling with g++)
« Reply #11 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).
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."