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
Related
we're curruntly working on a C project and we've downloaded and used the header dirent.h, the problem is the code was compiled successfully on my teammate laptop but in mine it doesn't compile, telling me this :
In file included from utils.c:6:0:
dirent.h: In function '_wopendir':
dirent.h:383:28: error: missing binary operator before token "("
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
^
dirent.h:405:28: error: missing binary operator before token "("
#if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
^
dirent.h:413:5: warning: implicit declaration of function 'wcsncpy_s' [-Wimplicit-function-declaration]
wcsncpy_s (dirp->patt, n+1, dirname, n);
^
I searched for the problem and find that it's a preproccessor error and currently on the #if
i've tried to add #define WINAPI_FAMILY_PARTITION(Partitions) but it doesn't work.
Please suggest me a solution to compile it successfully, and does the windows version affect on preprocessing?
WINAPI_FAMILY_PARTITION is defined in <winapifamily.h>, probably included by <windows.h>. Look at this question for more explanations, but windows intricacies are largely irrelevant for your compilation issue. You might want include <windows.h> before <dirent.h>?
You did not publish the source code for your program, nor did you specify what OS you compile for not what compiler you use, but you mention we've downloaded and used the header dirent.h... This sounds wrong: system include files such as <dirent.h> are automatically installed with the compiler, they are specific to the OS and compiler, you cannot just download one from the net and expect it to work on your system. It might work by chance on your teammate's PC because the OS might be different.
I am developing a C application, and using Eclipse CDT IDE, which I find great. The project uses Glib,Gtk,and GStreamer , so whenever I use some of their features in a file, I need to include:
#include <glib.h>
#include <gtk/gtk.h>
#include <gst/gst.h>
The code compiles without any error, since the PATH variable to search those headers is set correctly in a CMakeLists.txt.
However, while working on the project, I found annoying errors highlighting in my code, regarding type definitions like gchar or GValue or GTKApplication; the error outlined is "symbol **** could not be resolved". These definitions are inside a header file that my Eclipse IDE cannot find (included by glib.h), if not at compile time (indeed the program compiles correctly). Instead, the type GError , defined in gst.h , is not highlighted as an error by the pre-compiler.
I would like then that my Eclipse IDE could search on nested headers (#include inside an #inlcude inside...) to find those type definition, in order so to not have those annoying errors highlighting. How can I do so? I would not like to have to include directly all files where the type definitions are done.
EDIT: As Jonah Graham outlined, the problem is not beacuse Eclispe does a "single-step research" on the headers, since it inspects includes inside other includes like any other IDE. It is a CMake bug with c and Eclipse
Thanks in advance.
The problem you are facing is a CMake bug*. CMake adds __cplusplus into the defined symbols unconditionally, which means that glib headers are not parsed properly when in C mode. You can see this clearly by opening gmacros.h around the definition for G_BEGIN_DECLS:
Because CMake told CDT __cplusplus is defined, it thinks G_BEGIN_DECLS is also defined, which makes code like this from gtypes.h parse incorrectly:
G_BEGIN_DECLS
/* Provide type definitions for commonly used types.
* These are useful because a "gint8" can be adjusted
* to be 1 byte (8 bits) on all platforms. Similarly and
* more importantly, "gint32" can be adjusted to be
* 4 bytes (32 bits) on all platforms.
*/
typedef char gchar;
...
Of course with no gchar defined, everything else is going to go badly.
Luckily there is a quick workaround until the problem is resolved in CMake, remove __cplusplus from the info in CDT.
Open Project Properties
C/C++ Include Paths and Symbols
Remove __cplusplus from the list and press OK
(sometimes necessary) Right-click on project -> Index -> Rebuild
* There may be some other workarounds if you know CMake better. The bug says also it will be fixed for the next release of CMake.
I'm trying to build a project in Xcode but I get the following errors Implicit declaration of function 'clear' is invalid in C99 and Conflicting types for 'clear'.
Here's the code:
//main.c
#include <stdio.h>
#include "tree.h"
int main(){
clear(); // Implicit declaration of function 'clear' is invalid in C99
return 0;
}
//tree.c
#include <stdio.h>
#include "tree.h"
void clear(){ ///Conflicting types for 'clear'
printf("Command clear.\n");
}
//tree.h
#include <stdio.h>
void clear(); ///Conflicting types for 'clear'
Why do I get these errors and warnings? I've tried to search for the solution on StackOverflow, but all the related answers where about the case when there were no #include of some sort.
'clear' is not a keyword in C so it's not the case, is it? (source: http://aboutc.weebly.com/keywords.html)
Related topics (do not answer my question although they are actually related):
Implicit declaration of function 'sum' is invalid in C99
Implicit declaration of function is invalid in C99
Thanks for any help.
UPDATE!
It turns out that changing the name of the clear funtcion to a cleark function solved the problem. Nevertheless it does not make any sense to me yet.
UPDATE 2!
I based my project on the command line tool template from Xcode 6.3 on Mac OS 10.10. Because of that Xcode has automatically added some libraries and flags to the project's compiler. What's the most important here is that the curses.h header has been added and this header already contains the clear() function.
Here's the Conflicting types for 'clear' error log:
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/curses.h:541:28: Previous declaration is here
I've tried to remove the -lcurses from the compiler's flags manually, but I couldn't locate such settings. Is there any another way to build the project? (All in all my goal is to be able to use the Xcode's debugger when the project expands)
UPDATE 3!
According to what Paul Griffiths discovered and published in the comment below the problem is following:
I can indeed replicate this problem with Xcode 6.3.1 with only the code presented. For some reason, stdio.h seems to be including curses.h (i.e. if you don't include stdio.h, this issue goes away), and I haven't been quickly able to find a way to stop it doing that. This seems to be problematic, since standard headers should not import random symbols into the global namespace without an easy and obvious way to turn it off.
Normally I run the C preprocessor to see what the compiler actually parses. However, following Xcode Preprocessor Output to examine the preprocessor output with Xcode does not achieve that - it is translating the #include to #import. Here is what the preprocessor view shows me:
// Preprocessed output for tree.c
// Generated at 9:24:57 PM on Friday, May 1, 2015
// Using Debug configuration, x86_64 architecture for curses-vs-stdio target of curses-vs-stdio project
# 1 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.c"
# 1 "<built-in>" 1
# 1 "<built-in>" 3
# 322 "<built-in>" 3
# 1 "<command line>" 1
# 1 "<built-in>" 2
# 1 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.c" 2
# 1 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.h" 1
void clear(void);
#import Darwin.C.stdio; /* clang -E: implicit import for "/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/stdio.h" */
# 5 "/Users/thomas/Desktop/curses-vs-stdio/curses-vs-stdio/tree.c" 2
void clear(void) {
printf("Command clear.\n");
}
Apparently the problem is Xcode's use of modules rather than stdio.h including curses.h. The "Darwin" module is where the problem lies.
In fact, if I disable modules (using the hint in Enable Clang Modules, Disable Auto Linking), the build problem goes away. This is
in Build Settings
section Apple LLVM 6.1 - Language - Modules
setting Enable Modules (C and Objective-C)
As a further hint to the problem, having rearranged the example slightly (putting the prototype before the include), I see a message complaining about overloading — but that is not C.
Perhaps Apple will fix this in the next release.
You are including "trie.h", not "tree.h".
But maybe that's just you being careless when posting the code...
I bet there is another function named clear () defined somewhere. Possibly in your version of stdio.h.
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.
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.