Caffe: Use caffe API together with windows.h

Created on 29 Nov 2016  ·  16Comments  ·  Source: BVLC/caffe

Issue summary

Compilation failed when using caffe API and windows.h.

Steps to reproduce

Here is the sample code

#define GLOG_NO_ABBREVIATED_SEVERITIES
#include <windows.h>
#include <caffe/caffe.hpp>

using namespace std;
int main() {

    return 0;
}

Your system configuration

Operating system:windows 10
Compiler: Visual Studio 2013
CUDA version (if applicable): 8.0
CUDNN version (if applicable):5.1

CMake configurations

C:\Users\GTA-01\hookv\build>cmake -G"Visual Studio 12 2013 Win64"  ..\
-- The C compiler identification is MSVC 18.0.21005.1
-- The CXX compiler identification is MSVC 18.0.21005.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 12.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found CUDA: C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v8.0 (found version "8.0")
-- Configuring done
-- Generating done
-- Build files have been written to: C:/Users/GTA-01/hookv/build

CMake output

Cmake-Output.txt

windows

Most helpful comment

Thanks alot, that works like a charm, and kept me from tampering that caffe.pb.h file.
In resume, and for future reference, the following compiler defs worked for me:
GLOG_NO_ABBREVIATED_SEVERITIES
NOMINMAX
WIN32_LEAN_AND_MEAN
NO_STRICT

Again, thank you willyd

All 16 comments

Im getting the exact same thing. Anyone has a workaround for this? I already tried to use GOOGLE_GLOG_DLL_DECL=GLOG_NO_ABBREVIATED_SEVERITIES as a preprocessor to no avail.

Try to use any combination of

#define NOMINMAX
#define WIN32_LEAN_AND_MEAN 

before including windows.h or even better setting it globally in the compiler flags.

Setting up the preprocessors:
GLOG_NO_ABBREVIATED_SEVERITIES
NOMINMAX
WIN32_LEAN_AND_MEAN

got me down from 102 errors on caffe.pb.h to just 4. Not bad. Thank you.

remained 2 syntax errors:
C2059: syntax error : 'constant'

pointing to two typedef on:
typedef V1LayerParameter_DimCheckMode DimCheckMode;
and
typedef ParamSpec_DimCheckMode DimCheckMode;

Actually is to:

typedef ParamSpec_DimCheckMode DimCheckMode;
static const DimCheckMode STRICT = ParamSpec_DimCheckMode_STRICT;
static const DimCheckMode PERMISSIVE = ParamSpec_DimCheckMode_PERMISSIVE;

and

typedef V1LayerParameter_DimCheckMode DimCheckMode;
static const DimCheckMode STRICT = V1LayerParameter_DimCheckMode_STRICT;
static const DimCheckMode PERMISSIVE = V1LayerParameter_DimCheckMode_PERMISSIVE;

and it seems that comment out STRICT and PERMISSIVE did it.
Thank you willyd

There must be a preprocessor definition that conflicts with those declarations too.

Looking at MSDN you might need to #define NO_STRICT

https://msdn.microsoft.com/en-us/library/windows/desktop/aa383732(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/aa383679(v=vs.85).aspx

Thanks alot, that works like a charm, and kept me from tampering that caffe.pb.h file.
In resume, and for future reference, the following compiler defs worked for me:
GLOG_NO_ABBREVIATED_SEVERITIES
NOMINMAX
WIN32_LEAN_AND_MEAN
NO_STRICT

Again, thank you willyd

@bnascimento @twmht If the issue is solved could you please close the issue and ideally send a PR with a comment in the README.md about this. Thanks

I have the same issue but with VS 2015 .
I have ATL Component in which I include caffe .
With NO_STRICT I get the following error.

Severity Code Description Project File Line Suppression State
Error C2440 'return': cannot convert from 'INT_PTR (__stdcall *)(HWND,UINT,WPARAM,LPARAM)' to 'DLGPROC' c:\program files (x86)\microsoft visual studio 14.0\vc\atlmfc\include\atlwin.h 3761

i have same error but not with windows.hi use QT 5.8 for GUI design. and define PreprocessorDefinitions :

NO_STRICT
NOMINMAX
WIN32_LEAN_AND_MEAN

but not solved issue
the errors:

Severity    Code    Description Project File    Line    Suppression State
Error   C2059   syntax error: '='   faceRecognition D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error   C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error   C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   

You can import all interfaces you need with extern "C" into a namespace and Caffe will not be interfered. For example:

namespace Windows
{
    extern "C" __declspec(dllimport)
        Bool __stdcall SetWindowPos(Handle window,
            Handle windowInsertAfter,
            int x, int y, int width, int height, unsigned flags);
}

@aligoglos Have you solved this problem? i have same error as you

i have same error use QT 5.9 for GUI design。 How can I solve this problem
1>D:\old_rcnn\include\caffe/proto/caffe.pb.h(2525): error C2059: syntax error:“=”
1>D:\old_rcnn\include\caffe/proto/caffe.pb.h(2525): error C2238: 意外的标记位于“;”之前
1>D:\old_rcnn\include\caffe/proto/caffe.pb.h(9806): error C2059: syntax error :“=”
1>D:\old_rcnn\include\caffe/proto/caffe.pb.h(9806): error C2238: 意外的标记位于“;”之前

@bnascimento my error is as same as yours. But define PreprocessorDefinitions did not work.

@cjhonlyone
i have solved this problem but not a best way.
here is the problem location:
syntax error : 'constant'。
static const DimCheckMode STRICT = V1LayerParameter_DimCheckMode_STRICT;
static const DimCheckMode PERMISSIVE = V1LayerParameter_DimCheckMode_PERMISSIVE

And you just change the name of "STRICT" and "PERMISSIVE" here, what am i do is just add "_" in front of "STRICT" :)

i have same error but not with windows.hi use QT 5.8 for GUI design. and define PreprocessorDefinitions :

NO_STRICT
NOMINMAX
WIN32_LEAN_AND_MEAN

but not solved issue
the errors:

Severity  Code    Description Project File    Line    Suppression State
Error C2059   syntax error: '='   faceRecognition D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2734    
Error C2059   syntax error: '='   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10265   
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   2735    
Error C2238   unexpected token(s) preceding ';'   myqtProject D:\caffe-windows\build\include\caffe\proto\caffe.pb.h   10266   

hi,aligoglos,I encouter the same problem,have you solved the problem ? How do you solved it ?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

shiorioxy picture shiorioxy  ·  3Comments

greatgao picture greatgao  ·  3Comments

OpenHero picture OpenHero  ·  3Comments

malreddysid picture malreddysid  ·  3Comments

kelvinxu picture kelvinxu  ·  3Comments