g++ macro concatenation vs c++ macro concatenation - c++

I'm porting a windows based c++ project into windows, and I ran into the following error.
UeiDaqAnsiC.cpp:105:24
error: expected ')' before string constant
UEI_ERROR(__FUNCTION__ " error: %s\n", e.getErrorMessage());
It looks as though linux is unable to concatenate the __FUNCTION__ macro with the following string. This is confusing, as the project compiles and builds just fine in Windows.
As a quick fix, it appears simply adding a , between the __FUNCTION__ and "error: %s\n", e.getErrorMessage() completely fixes it.
The new fixed line looks like this UEI_ERROR(__FUNCTION__, " error: %s\n", e.getErrorMessage());
I came here because im not well versed on the linux g++ compiler, and I want to know if this is a valid workaround for this error, before I fix all 130 lines where this error occurs.
EDIT: I also want to ask if the comma keeps the functionality of simply concatenating the macro and string
EDIT2: UEI_ERROR is defined as
#define UEI_ERROR(...) UeiPalTraceOutputWithLevel(UeiPalTraceError, __VA_ARGS__)

__FUNCTION__ is not a string literal and cannot be concatenated by preprocessor with other string literal.
Your "fix" by adding , changes the meaning, mostly as
printf(__FUNCTION__ " format %i", 42); // MyFunction format 42
printf("MyFunction", "unused format %i", 42); // MyFunction
Real fix would be to change the format and reorder parameter:
UEI_ERROR("%s error: %s\n", __FUNCTION__, e.getErrorMessage());// MyFunction error: error message.

Related

C++ multiline CString literal in VS2013 Unicode

A multiline CString literal 'str1', accepted without a wink in VS2012 (with MBCS) is now refused at build time, after upgrading to VS2013 (with Unicode, to alleviate tons of errors from the newly deprecated MBCS, even after installing its addon), with the output message:
error C2308: concatenating mismatched strings
as in the following example (A):
str1 = _T(" HELP - available commands \n\n\n"
"F1 : the present help message \n\n");
The first line is reported 'wide' and the second 'narrow'.
I have then tried (B) to add mono-line CString literals:
str1 = _T(" HELP - available commands \n\n\n")
+ _T("F1 : the present help message \n\n");
but the IDE already complains with
Error: expression must have integral or unscoped enum type
and the builder with
error C2110: '+' : cannot add two pointers
It does indeed work if I build (C) the CString str1 with mono-line literals one by one:
str1 = _T(" HELP - available commands \n\n\n");
str1 += _T("F1 : the present help message \n\n");
but I would like to understand why (A) and (B) do not work here, as expected, and as they were so until now. There are several such problems in this (large) program, but in most other similar instances it does work just fine.
Is it due to changes in VS2013 or (and?) the switch from MBCS to Unicode? Are there special characters I overlooked in these strings? And then, how to fix these problems?
Thanks in advance for your responses.
It should be:
str1 = _T(" HELP - available commands \n\n\n") // no semicolon here
_T("F1 : the present help message \n\n");
The reason it worked before is that _T is a no-op for MBCS but for Unicode expands to a width prefix. In C/C++ concatenation is just by making them adjacent with whitespace, but they need the same prefix, at least for MSVC.

Convert C++ log source snippet to Windows Phone C++/CX

I just started developing for Windows Phone and I'm stuck with one piece of exisiting code I need to maintain. It's a macro from a logging lib that is used in many places of existing code.
This is the macro:
#define LOG_FORMAT_FUNCTION(fmtarg, firstvararg) __attribute__((__format__ (__printf__, fmtarg, firstvararg)))
And this is a method definition that fails to use the above macro with error "{ expected" (In German "Error: Es wurde ein '{' erwartet."):
void LogTrace_s(const char* category, const char* format, ...) LOG_FORMAT_FUNCTION(2, 3);
Can you help me get rid of the error? I'd also like to know what actually the macro does exactly.
Edit: After rading this here I now understand that this macro is good for error checking formatted strings. Now that I know, I need it even more. But I still have no clue how to translate this to MS C++.
Yes you CAN just omit it. Use
#if _MSVC_VER
#define LOG_FORMAT_FUNCTION(fmtarg, firstvararg)
#endif
It is annotating the function with extra information to help gcc give you better warnings. It does not change the behavior of the code in any way.

Qt - getting "warning: format not a string literal and no format arguments"

Keep getting warnings on lines like these
qDebug("An error occured while trying to create folder " + workdir.toAscii());
workdir being QString()
warning: format not a string literal and no format arguments
That should probably be:
qDebug("An error occured while trying to create folder %s", workdir.constData());
since qDebug takes const char* as first argument.
When debbuging with qDebug, I find the following syntax much easier :
qDebug() << "An error occured while trying to create folder" << workdir;
To do this, you'll need to include the <QtDebug> header.
More info : Qt docs regarding qDebug().
I managed to get it to work fine without warning like this :
qDebug("An error occurred while trying to create folder %s", qUtf8Printable(workdir));

vim c++ break line

How can I break long lines when writing c++ code in vim? For example, if I have something like
56 fprintf(stderr, "Syntax error reading recursion value on
57 line %d in file %s\n", line_count, filename);
I get the following compile errors:
:56:25: warning: missing terminating " character
:56: error: missing terminating " character
:57: error: stray ‘\’ in program
:57:37: warning: missing terminating " character
:57: error: missing terminating " character
I'm a vim newbie.
Thanks!
That's not a Vim problem, that's a C problem.
Put quotes at the end of one line and the start of the other. Maybe you're looking for this:
fprintf(stderr, "Syntax error reading recursion value on "
"line %d in file %s\n", line_count, filename);
...and if you want to know how to turn one-long-line into two, if you're splitting mid-string, go to where you want to split and then type 'i' followed by quote-enter-quote. Vim will follow your cindent rules when aligning the second line.
Alternatively, maybe it's a view problem? If you have a linebreak in there, it'll give you a compile error. However, in vim it is possible to have it appear to break the line, put set wrap and set lbr in your vimrc file. Check out :help lbr for info. There's also a way to configure the "leader" on the line, so you know it's a view-only linebreak.
my advice would be to not break the string -
instead do
fprintf (stderr,
"Syntax error reading recursion value on line %d in file %s\n",
line_count,
filename);
Like Billy ONeal, I'm a bit confused why you're asking this as a Vim question. The code you need to write is:
fprintf(stderr, "Syntax error reading recursion value on "
"line %d in file %s\n", line_count, filename);
Note that there's no comma - when you remove the extra whitespace, that's just two string literals together. They'll be combined into one, which I believe is exactly what you want.
Put a trailing \ on the end of the line you want to continue.

Why cannot show debug by ACE_DEBUG?

ACE_DEBUG declare #include< ace/Task.h > in source header file.I trace debug define by
ACE_DEBUG((LM_ERROR, "Reader pathSetOpen : %s
",pathSetOpen);
The string variable name "pathSetOpen" for show value still execute programs.But I cannot compile code.
About ACE_DEBUG,It's macro for printing debug message.
Compile error code.
EnvTest.cpp:353:1: error: unterminated
argument list invoking macro
"ACE_DEBUG"
You've forgot a closing parenthesis:
ACE_DEBUG((LM_ERROR, "Reader pathSetOpen : %s ",pathSetOpen));