0

I am trying to run some code copied from Windows Dev Center, but keep encountering an error over some unresolved identifiers. This maybe sound silly, but why would these lines cause this error:

CONDITION_VARIABLE BufferNotEmpty;
CONDITION_VARIABLE BufferNotFull;

And "not declared in this scope" on other lines similar to this:

SleepConditionVariableCS (&BufferNotFull, &BufferLock, INFINITE);

When I've included all the headers (or at least i think) necessary?

#include <mutex>
#include <condition_variable>
#include <cstdint>
#include <windows.h>
#include <stdlib.h>
#include <stdio.h>
#include <WinBase.h>

Running NetBeans with project configured to C++11 in case that changes anything. Thanks in advance if anyone has any suggestions!

EDIT:

Error output:

cd 'C:\Users\Linda\Documents\NetBeansProjects\CppApplication_2'
C:\MinGW\msys\1.0\bin\make.exe -f Makefile CONF=Debug
"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/Linda/Documents/NetBeansProjects/CppApplication_2'
"/C/MinGW/msys/1.0/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/cppapplication_2.exe
make.exe[2]: Entering directory `/c/Users/Linda/Documents/NetBeansProjects/CppApplication_2'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/conditional_var.o.d"
g++ -std=c++11   -c -g -std=c++11 -MMD -MP -MF "build/Debug/MinGW-Windows/conditional_var.o.d" -o build/Debug/MinGW-Windows/conditional_var.o conditional_var.cpp
conditional_var.cpp:20:1: error: 'CONDITION_VARIABLE' does not name a type
 CONDITION_VARIABLE BufferNotEmpty;
 ^
conditional_var.cpp:21:1: error: 'CONDITION_VARIABLE' does not name a type
 CONDITION_VARIABLE BufferNotFull;
 ^
conditional_var.cpp: In function 'DWORD ProducerThreadProc(PVOID)':
conditional_var.cpp:43:40: error: 'BufferNotFull' was not declared in this scope
             SleepConditionVariableCS (&BufferNotFull, &BufferLock, INFINITE);
                                        ^
conditional_var.cpp:43:76: error: 'SleepConditionVariableCS' was not declared in this scope
             SleepConditionVariableCS (&BufferNotFull, &BufferLock, INFINITE);
                                                                            ^
conditional_var.cpp:64:33: error: 'BufferNotEmpty' was not declared in this scope
         WakeConditionVariable (&BufferNotEmpty);
                                 ^
conditional_var.cpp:64:47: error: 'WakeConditionVariable' was not declared in this scope
         WakeConditionVariable (&BufferNotEmpty);
                                               ^
conditional_var.cpp: In function 'DWORD ConsumerThreadProc(PVOID)':
conditional_var.cpp:82:40: error: 'BufferNotEmpty' was not declared in this scope
             SleepConditionVariableCS (&BufferNotEmpty, &BufferLock, INFINITE);
                                        ^
conditional_var.cpp:82:77: error: 'SleepConditionVariableCS' was not declared in this scope
             SleepConditionVariableCS (&BufferNotEmpty, &BufferLock, INFINITE);
                                                                             ^
conditional_var.cpp:111:33: error: 'BufferNotFull' was not declared in this scope
         WakeConditionVariable (&BufferNotFull);
                                 ^
conditional_var.cpp:111:46: error: 'WakeConditionVariable' was not declared in this scope
         WakeConditionVariable (&BufferNotFull);
                                              ^
conditional_var.cpp: In function 'int main()':
conditional_var.cpp:124:35: error: 'BufferNotEmpty' was not declared in this scope
     InitializeConditionVariable (&BufferNotEmpty);
                                   ^
conditional_var.cpp:124:49: error: 'InitializeConditionVariable' was not declared in this scope
     InitializeConditionVariable (&BufferNotEmpty);
                                                 ^
conditional_var.cpp:125:35: error: 'BufferNotFull' was not declared in this scope
     InitializeConditionVariable (&BufferNotFull);
                                   ^
conditional_var.cpp:141:45: error: 'WakeAllConditionVariable' was not declared in this scope
     WakeAllConditionVariable (&BufferNotFull);
                                             ^
make.exe[2]: *** [build/Debug/MinGW-Windows/conditional_var.o] Error 1
make.exe[2]: Leaving directory `/c/Users/Linda/Documents/NetBeansProjects/CppApplication_2'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/Linda/Documents/NetBeansProjects/CppApplication_2'
make.exe": *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 4s)
meta
  • 153
  • 12
  • Can you post the actual text of the error you are seeing? – 0x5453 Mar 24 '17 at 15:29
  • @0x5453 sure, added the entire thing to the original post. – meta Mar 24 '17 at 15:34
  • Why you type CONDITION_VARIABLE in the uppercases? – Dmitry J Mar 24 '17 at 15:43
  • @Dmitry J, I didn't as said copied the thing from the given link – meta Mar 24 '17 at 15:44
  • @meta, Maybe [this one](http://stackoverflow.com/questions/18564459/error-compile-condition-variable-undeclared) is related. – Dmitry J Mar 24 '17 at 15:45
  • The list of headers includes one you are not including (seems to be Windows version specific) https://msdn.microsoft.com/en-us/library/windows/desktop/ms686301(v=vs.85).aspx Try adding `Synchapi.h` – Richard Critten Mar 24 '17 at 15:53
  • @Richard Critten, Tried it and got an "cannot find include file " error (this one probably since I am running Windows 10). Added from the mentioned article, but the code still doesn't work. – meta Mar 24 '17 at 16:01
  • 2
    You probably need to get the correct platform define in, the WINNT etc. Those condition variables are only available on later versions of Windows, so if the compiler defines are missing or wrong, it wouldn't pick them up – Niall Mar 24 '17 at 16:02
  • @Niall, good idea, I tried the corresponding values from [link](https://msdn.microsoft.com/en-us/library/6sehtctf.aspx), but with no effect. I was supposed to define them in my source code, right? – meta Mar 24 '17 at 16:11
  • @DmitryJ, thanks for the suggestion, but none of the solutions from there worked. I even made sure my SDK is up to date. – meta Mar 24 '17 at 16:13
  • 2
    Use "find in files" from your favourite editor to find `CONDITION_VARIABLE` in your system include directory. Either add the include (if you haven't already) or workout what #define(s) need to be set. – Richard Critten Mar 24 '17 at 16:18

1 Answers1

1

What happens if you add

#include <SDKDDKVer.h>

Before the #include <windows.h>?

The CONDITION_VARIABLE definition should be brought in by <windows.h>, but only if building against Vista-or-newer. <sdkddkver.h> should ensure you're always building against the newest target supported by the SDK.

If that doesn't help, or if <sdkddkver.h> isn't found, then it means that your build environment is using a very old, very stale SDK. I don't know what NetBeans/mingw uses for its Windows SDK, so it's quite plausible that they use a stale version.

If this proves to be the case then I think your best option would be to use Visual Studio 2015 or 2017 Community Edition along with the current Windows 10 SDK.

DrPizza
  • 17,882
  • 7
  • 41
  • 53
  • thanks. running on win10, so that shouldn't be an issue. added the include, but doesn't look like it changed anything, so I'll give Visual Studio a try a little later. still seems weird that semaphores and mutex (that both should go under windows.h) run with no issues, but conditional variables get completely stuck like this. – meta Mar 24 '17 at 17:56
  • and it worked like a charm! switching to Visual Studio may not be the solution I was hoping for, but it appears to be the only one that can make the code work on my machine. – meta Mar 24 '17 at 20:50
  • Semaphores and mutexes are much older and have been supported by Windows for much longer. Any SDK will support them. – DrPizza Mar 24 '17 at 21:57