Visual C++ Unicode String Literal is giving error: 'L': undeclared identifier - visual-c++

I'm working on getting a Visual C++ 2005 solution to compile in unicode. However, In some of my projects (but not all), I get errors in the form:
1>.\CBitFlags.cpp(25) : error C2065: 'L' : undeclared identifier
and the line of code in question is:
LOGERROR(UTILITY, L"Tried to use object to store %d flags, when max is %d",
I am BAFFLED. It seems to be treating L as an identifier when L is part of the language syntax. Does anyone know if there is some flag somewhere that has to be enabled in the project or compile settings that if not toggled would cause this? The really weird part is it isn't all of the occurrences of this, it's only some of them. It does seem to be consistent within a single project, but I have entire projects compiling fine, and others that fail miserably like this.

The problem is almost certainly inside of the LOGERROR macro. Look at how it treats that second paramater. Expand the macro yourself, it is easy to overlook small errors in macros sometimes.

Visual C++ 2005 does support the L syntax for wide strings, and doesn't need any special flags or anything to support it. So most likely, your problem is elsewhere. Perhaps the definition of LOGERROR or UTILITY. Or a missing semicolon previously, or.... It could be anything which causes the compiler to expect something other than a string literal when it gets to the L.

Since this appears to be something used in a macro, I think you should look at the definition of the LOGERROR and UTILITY macros and at what they expands to in the context of your code. Use the /P compiler option to preprocess your files without proceeding to the compilation step, and then look at the result to see what the compiler's really seeing.
I can reproduce the error you see if I have a space after the L:
wchar_t const* foo = L "foo";
Are you sure you've copied and pasted the actual code that's giving you trouble?

Related

problems compiling with gcc and gnu make

The problem arises firstly from the "isfinite" function: (undefined reference to isfinite). From google search I find that I must include "math.h" and write three lines of code, like:
ifdef __linux__
define _finite(v) (__builtin_isfinite(v))
endif
But then, there comes the error: (Make:47 missing endif. Stop).
If I comment out those three lines of code, the error becomes: (<math.h> no such file or directory).
My system is OpenSUSE Leap 15.4; gcc version 7; gnu make version 4.2.1-7.3.2.
I think I have installed all the needed packages. However the errors persist. Any help?
I primarily want to address this part of the question, as the underlying issue has been addressed elsewhere, multiple times:
The problem arises firstly from the "isfinite" function: (undefined reference to isfinite). From google search I find that I must include "math.h" and write three lines of code, like:
ifdef __linux__
define _finite(v) (__builtin_isfinite(v))
endif
I started to write "Google led you astray", but I think it's more likely that you seriously misunderstood what it led you to. And perhaps you happened to choose poor results.
The error message indicates that you put those lines in your makefile, but they are wholly inappropriate for that. Don't put them there.
The lines are C preprocessor directives that have been stripped of their leading # characters. You would use them by restoring the # characters ...
#ifdef __linux__
#define _finite(v) (__builtin_isfinite(v))
#endif
... and putting the resulting lines into one or more of your C source files. BUT DON'T! Those lines are unnecessary and at best unhelpful.
You do need to #include <math.h> in each C source file that contains a call to isfinite(), because that is the header that provides a declaration of the function. Since C99, functions must be declared before they are called (and it was good practice well before then).
Other than that, with GCC and many other traditional-style Unix C compilers, you need to explicitly include the math library in your link when you need any of the functions it provides. That would involve adding -lm to your makefile, at the end of the gcc command that builds the final executable. This is covered in gcc will not properly include math.h, and many other duplicates on this site.

Just starting off with C++, not registering command

I'm not really sure why "cout" and "endl" are not being recognized. Any help would be great!
The error is:
and the code is:
The fact that iostream has a red squiggle underneath it is a near certainty that you have something wrong with your environment (such as compiling with a C compiler rather than a C++ one, for example).
You need to fix that, since cout and endl are defined in that header. I'd start by hovering the mouse over the iostream text and see what the tooltip shows you.
If it cannot find the file iostream then you're either not using a C++ compiler, or your environment is severely damaged.
Either way, it's not a correct C++ environment.
Things to look in to are (to start with):
Examine the file extension. Using *.c instead of *.cpp may use a C compiler rather than a C++ one, for example).
Examine the output of your compilation, if available. You will hopefully be able to tell which compiler is being used.
If you are sure you're using a C++ compiler:
You might have a funny character in your iostream string. You could totally delete that line and retype it (don't edit, it may not get rid of the funny character).
Try a different header (like cstdlib) to see if it has the same problem.
Last-straw solution would be re-installation of your development environment, in case things are so damaged it's unrecoverable.

vim couldn't jump a definition navigation for template functions

I have a .h file who contains these code:
template<BLA>
func1() {}
// something
template<BLA>
func2() {
func1();
}
when I typed the command GoToDefinition, the error appeared :"YCM : 'RuntimeError : can't jump to definition.'".
Do I miss something? And how to find the definition?
By the way, I have this in my .vimrc:
let g:ycm_global_ycm_extra_conf = ' ~/ycm_extra_conf.py'
**************second edit*******************
I reinstalled my YCM, and I tried ctags for YCM by this command :
ctags -R --fields=+l
It works, and thanks.
Last time I've checked, YCM understanding of a source code is restricted to one translation unit. It'll be very difficult for it to find where a function is defined as it's likely to be in another translation unit.
In other words, it should work as long as you want to jump to a definition that is in the same .cpp file as the one your are currently editing.
Thus, it should also work when trying to access to a template function definition from it's call site -- as we're supposed to include the related code. If it doesn't, it could be related to an improper understanding of the source code by clang engine YCM is using, or to YCM not configured to use clang.
Regarding tags, they could do the job, but indeed, in C++, you'll want a way to narrow the tags presented. That's what had me started lh-tags: it presents all matching tags and it permits to filter them on various criteria (filename, kind, scope, ...)

wide version of __FUNCTION__ on linux

is there a way i can print __FUNCTION__ as a wide character on linux?
the trick with the WIDEN doesn't work for me, the gcc compiler prints:
error: ?L_FUNCTION_? was not declared in this scope
any help?
Thanks
7+ years later (although things seem to be the same) ...
Since you mentioned gcc, check [GNU.GCC]: Standard Predefined Macros (emphasis is mine):
C99 introduced __func__, and GCC has provided __FUNCTION__ for a long time. Both of these are strings containing the name of the current function (there are slight semantic differences; see the GCC manual). Neither of them is a macro; the preprocessor does not know the name of the current function.
Since __FUNCTION__ is not a macro (the preprocessor doesn't "know" anything about it), it will remain untouched during (outer) macro expansion, generating at the end the L__FUNCTION__ identifier, which is obviously invalid. That's why the double macro approach works for __FILE__ (for example), but not for __FUNCTION__ (or __func__).
So, the short answer to your question is "NO" (at least not at preprocessor level). You will need to convert __FUNCTION__ "manually" (e.g. using one of the [man7]: MBSTOWCS(3) functions family).
Note: It works on VStudio, since according to [MS.Docs]: Predefined Macros (emphasis still mine):
__FUNCTION__ Defined as a string literal that contains the undecorated name of the enclosing function. The macro is defined only within a function.
It can be done using macros, you just have to understand how macros expand.
To get a wide char version of the macro you need to create 2 layers of macros like so:
#define WIDE2(x) L##x
#define WIDECHAR(x) WIDE2(x)
#define WIDE_FUNCTION WIDECHAR(__FUNCTION__)
The all important piece is the L##x that appends the L character to the string constant before the compiler sees it. You can do this to __FILE__ as well using the same technique.
That looks more like a typo of __FUNCTION__ than an issue with widen() or similar, at least if you pasted the exact error message.

MSVC++ Preprocessor - How do I output a define's value with a compiler macro?

I tried
#error MY_DEFINE
But all that did is echo "MY_DEFINE" when it threw the error.
Thanks!
You can tell the compiler to save the preprocessor output (/E or /EP) and then look at that file. That's usually how I debug problems related to macro expansion.
If you're trying to make some cool error facility for a library, you might be out of luck. I think you'll have a hard time getting the preprocessor to expand a macro into a compile-time message. Perhaps if you combined a template trick that used the macro, you could get it to appear in a cryptic compiler error message.

Resources