Visual Studio vs GCC, concerning "'__cdecl' attribute directive ignored [-Wattributes]" - c

I need to suppress a GCC (or what I believe is GCC) compiler warning in Visual Studio. Usually these Visual Studio compiler warnings come with a warning code, but this one is just null.
The warning is '__cdecl' attribute directive ignored [-Wattributes]. I believe I need to suppress all -Wattributes warnings for my .h file, but I am not sure how to do that.
The block that is giving me the troubles is inside LoggerHelper.h:
#ifdef _MSC_VER
using LoggerFuncPtr = void(__cdecl *)(wchar_t*);
#else
using LoggerFuncPtr = void(__attribute__((__cdecl)) *)(wchar_t*);
#endif

GCC's cdecl attribute doesn't have leading underscores, the declaration should look like:
#ifdef _MSC_VER
using LoggerFuncPtr = void(__cdecl *)(wchar_t*);
#else
using LoggerFuncPtr = void(__attribute__((cdecl)) *)(wchar_t*);
#endif

Related

How to make VS2017 ignore a specific compiler's keyword?

I use Visual Studio 2017 with ReSharper 2017.2 as the code editor for an embedded project. I managed to configure all but one thing:
How do I make VS/R# ignore that specific keyword, _Interrupt1? (this error causes other side effects).
I tried to add it as a preprocessor definition (Project's properties -> C/C++ -> Preprocessor -> Preprocessor Definitions), but it doesn't help.
I am not sure about R#, but in VS you could try to define globally:
#ifdef __INTELLISENSE__
#define _Interrupt1
#endif
to hide _Interrupt1 from Intellisense parsing.
For completeness, thanks to #Hans Passant, use __RESHARPER__ for RS

How to replace function name with string constant in #define macro in C

I wish to replace a function call with a string using pre-processor directives.
Something like this:
#ifdef DEBUG
#define Func1(arg) "Function not supported"
#endif
So basically when someone calls this function, I want a compilation error, such that this function is invisible in DEBUG mode, and in place of that , the following string is printed in the compilation log.
This method throws error.
Is there any other means to print a particular string i want when func1() is called?
The most obvious way to archieve such behaviour is to use #error directive. However since it's not possible to construct "conditional #error directive" I guess next move is _Pragma operator introduced in C99. Here is the solution that produces a message during compilation:
#include <stdio.h>
#define DEBUG 1
#ifdef DEBUG
#define Func1(arg) _Pragma("message \"Function not supported.\"")
#endif
void (Func1)(int arg)
{
}
int main(void)
{
Func1(1);
Func1(2);
Func1(3);
return 0;
}
Compilation (with gcc):
...
check.c:15: note: #pragma message: Function not supported.
check.c:16: note: #pragma message: Function not supported.
check.c:17: note: #pragma message: Function not supported.
I know It's not direct solution (such message is not even treated as warning, so -Werror doesn't change anything), however you can use e.g. grep tool or any other method to scan compiler's output.
Since GCC 4.8 there is also #pragma GCC error "message", which is direct (but non-portable) solution. Check this answer for more information. For example:
#ifdef DEBUG
#define Func1(arg) _Pragma("GCC error \"Function not supported.\"")
#endif
One way is to simply leave the function undefined. This will result in an error at link time.
If you use gcc, you could use one of its extensions, a function attribute:
#ifndef DEBUG
#define __nodebugonly
#else
#define __nodebugonly __attribute__((error("Function not supported")))
#endif
void Func1(int arg) __nodebugonly;
You can use
#error "Function not supported"
to abort the compilation.
But obviously when someone has access to the source that can be removed - so what exactly do you try to do?
If you don't trust those people with your source then don't give it to them.
Link it into an .obj, define the 'interface' with a .h and only give out that.

Compilation error in visual C++

I am using Visual studio 2010 for building C project. My project contains a number of header files,source file and parsers. It uses lex and bason files. I am getting a single error during the compilation and íé the following
abc.y:error C2065: 'INPUT' : undeclared identifier
I tried the solutions I am getting like including
#define WIN32_WINNT >= 0x0501
in my main.c file before the inclusion of any of the header files.I am not able to get rid of this error. Could you please let me know what Can be the reasons for this error?
EDIT
The snippet of code that is showing error is:
list_Cons(0, list_List((POINTER)INPUT)
The surprising thing is that If i alter INPUT into INPUT1, I get the same error. It is stoic to change.
Presumably you read this and this.
#define WIN32_WINNT >= 0x0501 wont work. You should try using #define WIN32_WINNT 0x0501 instead.
Also, check that you are actually #including winuser.h
A C++ compiler cannot process a *.y file. For that you need a yacc / bison program, which does not come included with Visual Studio 2010.
For myself I use CMake which can generate MSVC projects along with other build types. You can tell it that a .y needs to be processed outwith the C/C++ files and it will instruct MSVC to invoke whatever external tools are necessary to preprocess the non-C/C++ parts.

GetCurrentHwProfile in Code::Blocks (C)

I am trying to get the GUID from windows in C in the Code::Blocks IDE with the following code:
#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <stdio.h>
void getHWID()
{
HW_PROFILE_INFO hwProfileInfo;
if(GetCurrentHwProfile(&hwProfileInfo))
{
printf("Hardware GUID: %s\n", hwProfileInfo.szHwProfileGuid);
printf("Hardware Profile: %s\n", hwProfileInfo.szHwProfileName);
}
}
I keep getting these errors even though I am linking and including all the files required:
In function 'getHWID': warning:
implicit declaration of function
'GetCurrentHwProfile'
In function getHWID': undefined
reference toGetCurrentHwProfile'
||=== Build finished: 1 errors, 1
warnings ===|
Let me know if anyone has had this problem or know how to fix it. Also if I right click HW_PROFILE_INFO or GetCurrentHwProfile and click Find Declaration it says not found.
I would like to get this to work, but I am also open to other simple ways to get this done.
edit: I've included Winbase.h now and it found a declaration for HW_PROFILE_INFO but I still get a undefined reference error for GetCurrentHwProfile
Have you configured Code::Blocks to include the correct SDK (I believe this function is part of the Windows SDK)? I suggest using Microsoft Visual Studio to write Windows code.
EDIT: I'm not sure if this is all you need to do, but there is a section in the their wiki about how to use the Microsoft's compiler.
Change
GetCurrentHwProfile
to
GetCurrentHwProfileA
or add
#ifdef UNICODE
#define GetCurrentHwProfile GetCurrentHwProfileW
#else
#define GetCurrentHwProfile GetCurrentHwProfileA
#endif
have a look GetCurrentHwProfile was not declared in this scope using MinGW's g++ compiler

How do I generate an error or warning in the C preprocessor?

I have a program that must be compiled only in DEBUG mode. (testing purpose)
How can I have the preprocessor prevent compilation in RELEASE mode?
Place anywhere:
#ifndef DEBUG
#error "Only Debug builds are supported"
#endif
For reference: Diagnostics
C provide a #error statement, and most compilers add a #warning statement. The gcc documentation recommends to quote the message.
Maybe something more sofisticated, but it is only copy&paste of previous solutions. :-)
#ifdef DEBUG
#pragma message ( "Debug configuration - OK" )
#elif RELEASE
#error "Release configuration - WRONG"
#else
#error "Unknown configuration - DEFINITELY WRONG"
#endif
P.S. There is also another way how to generate a warning.
Create an unreferenced label like
HereIsMyWarning:
and don't reference it. During compilation, you will get a warning like
1>..\Example.c(71) : warning C4102: 'HereIsMyWarning' : unreferenced label
You can use a error directive for that. The following code will throw an error at compile time if DEBUG is not defined:
#ifndef DEBUG
#error This is an error message
#endif
If you simply want to report an error:
#ifdef RELEASE
#error Release mode not allowed
#endif
will work with most compilers.
For GCC and Clang (and probably any compiler that supports the _Pragma feature) you can define a macro:
#if ! DEBUG
#define FIX_FOR_RELEASE(statement) _Pragma ("GCC error \"Must be fixed for release version\"")
#else
#define FIX_FOR_RELEASE(statement) statement
#endif
You can use this macro for temporary hacks, for example to get around code that a co-worker hasn't written yet, to make sure you don't forget to fix it once you want to release a build to the public. Either
FIX_FOR_RELEASE()
// Code that must be removed or fixed before you can release
or
FIX_FOR_RELEASE(statement that must be removed or fixed before you can release);
In Code::Blocks, if you don't want the Release mode, you can delete the Release mode. To do this, click on the Project menu, select Properties..., and in the Build targets tab you can click on Release and then click on the Delete button. Deleting the Release mode only does it for the current project, so you can still use it in other projects.
Otherwise, if you really want to use the preprocessor, you can do this:
#ifdef RELEASE
#error "You have to use the Debug mode"
#endif

Resources