visual c++ create text file - visual-studio-2010

How to create text file?
CreateFile("1",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
CREATE_NEW,
FILE_FLAG_OVERLAPPED,
NULL);
throw
1>------ Build started: Project: test2, Configuration: Debug Win32 ------
1> test2.cpp
1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(126): error C2065: 'GENERIC_READ' : undeclared identifier
1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(126): error C2065: 'GENERIC_WRITE' : undeclared identifier
1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(128): error C2065: 'NULL' : undeclared identifier
1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(129): error C2065: 'CREATE_NEW' : undeclared identifier
1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(130): error C2065: 'FILE_FLAG_OVERLAPPED' : undeclared identifier
1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(131): error C2065: 'NULL' : undeclared identifier
1>c:\users\kredkołamacz\documents\visual studio 2010\projects\test2\test2\Form1.h(125): error C3861: 'CreateFile': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Include the Windows header file as follows at the top of your .h or .cpp files:
#include <windows.h>
This should solve the problems related to undefined symbols such as GENERIC_WRITE and CreateFile. As another poster mentioned, you should typically write your code in .cpp files and only declare constants or classes in header files while placing the method implementations in .cpp files along with regular functions.
The issues related to CreateFileW once you get beyond this point need some more explanation:
By default, Windows applications generated from Visual Studio templates link against Unicode (wide character) versions of Windows APIs and have the UNICODE C/C++ preprocessor macro defined to indicate this. When UNICODE is defined, the preprocessor defines the symbol CreateFile to expand to the name of the actual underlying Windows function name which is CreateFileW where the W suffix indicates it is a "wide-character", i.e. Unicode, function. If the UNICODE macro is not defined (which can be overridden through various Visual Studio project settings), then CreateFile will expand to the CreateFileA symbol which is the name of the ANSI string version (A for ANSI) of the function. 99% of the time you should use the default settings for UNICODE as all modern versions of windows use Unicode characters internally.
Since CreateFileW takes Unicode string arguments you need to pass L"1" (i.e. a wide-character string literal) or use the TEXT macro (e.g. TEXT("1")) which will generate the correct string type corresponding to whether the UNICODE compiler switch is defined or not.
Here's a link to the MSDN article about TEXT: link.

Related

How do I compile a TensorFlow Lite GPU delegate for Windows

I have a TensorFlow Lite C API library that I am using on Windows and I want it to use a GPU delegate. I do not have any trouble compiling the TensorFlow Lite C API library with bazel on my Windows machine, but I cannot get the GPU delegate to compile.
TensorFlow version: 2.10.0
Windows version: 10.0.19044
Even though TFLite does not support the Windows platform there is a clear indication that it should be possible to compile a GPU delegate for the Windows platform using ANGLE library (see https://github.com/tensorflow/tensorflow/issues/28830#issuecomment-522325199).
It feels like I am close to the solution but I must be missing something fundamental. I am quite new to most of the tools and concepts involved, but I managed to stitch together the following procedure:
Installed ANGLE according to https://chromium.googlesource.com/angle/angle/+/main/doc/DevSetup.md (COMMIT=596c2acfa5071eb643eaffe855a5ccb51c345427)
Built ANGLE libraries with following args (args.gn):
is_component_build = false
target_cpu = "x64"
is_debug = false
angle_assert_always_on = false
Added ANGLE build output directory (containing libEGL.dll, libEGL.dll.lib, libGLESv2.dll, libGLESv2.dll.lib, d3dcompiler_47.dll) to PATH environment variable.
Added ANGLE library dependency in bazel:
In WORKSPACE file:
new_local_repository(
name = "angle",
path = "../../../angle/angle/include",
build_file_content = """
package(default_visibility = ["//visibility:public"])
cc_library(
name = "headers",
srcs = glob(["**/*.h"])
)
"""
)
Added necessary dependencies (copts = ["-Iexternal/angle"] and "#angle//:headers") to targets in bazel BUILD files (all targets that failed compilation due to missing OpenGL headers):
Example:
cc_library(
name = "gl_errors",
srcs = ["gl_errors.cc"],
hdrs = ["gl_errors.h"],
copts = ["-Iexternal/angle"], <--- ADDED THIS
deps = [
"#angle//:headers", <--- ADDED THIS
":portable",
"//tensorflow/lite/delegates/gpu/common:status",
"#com_google_absl//absl/strings",
],
)
Updated .bazel to use '/std:c++latest'
---build:windows --cxxopt=/std:c++17
---build:windows --host_cxxopt=/std:c++17
+++build:windows --cxxopt=/std:c++latest
+++build:windows --host_cxxopt=/std:c++latest
Build TensorFlow Lite GPU delegate (v2.10.0) //tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so
bazel2 build --local_cpu_resources=3 --features=windows_export_all_symbols -s -c opt --copt="-DMESA_EGL_NO_X11_HEADERS" --copt="-DEGL_NO_X11" //tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so
I expected to get the two files libtensorflowlite_gpu_delegate.so.dll and libtensorflowlite_gpu_delegate.so.if.lib but I get errors that I do not know how to mitigate.
ERROR: C:/users/magnusg/src/tensorflow_lite/tflite_2.10.0_gpu_delegate_build_bazel/tensorflow_src/tensorflow/lite/delegates/gpu/cl/kernels/BUILD:129:11: Compiling tensorflow/lite/delegates/gpu/cl/kernels/converter.cc failed: (Exit 2): cl.exe failed: error executing command
cd /d C:/users/magnusg/_bazel_magnusg/4du655w4/execroot/org_tensorflow
SET INCLUDE=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\include;C:\Program Files (x86)\Windows Kits\NETFXSDK\4.8\include\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.20348.0\ucrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.20348.0\shared;C:\Program Files (x86)\Windows Kits\10\include\10.0.20348.0\um;C:\Program Files (x86)\Windows Kits\10\include\10.0.20348.0\winrt;C:\Program Files (x86)\Windows Kits\10\include\10.0.20348.0\cppwinrt
SET PATH=C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\VC\VCPackages;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\TestWindow;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\TeamFoundation\Team Explorer;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\bin\Roslyn;C:\Program Files (x86)\Microsoft SDKs\Windows\v10.0A\bin\NETFX 4.8 Tools\x64\;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\devinit;C:\Program Files (x86)\Windows Kits\10\bin\10.0.20348.0\x64;C:\Program Files (x86)\Windows Kits\10\bin\x64;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\\MSBuild\Current\Bin;c:\Windows\Microsoft.NET\Framework64\v4.0.30319;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\Tools\;;C:\Windows\system32;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\CMake\bin;C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\Common7\IDE\CommonExtensions\Microsoft\CMake\Ninja
SET PWD=/proc/self/cwd
SET PYTHON_BIN_PATH=C:/Users/magnusg/anaconda3/envs/sa/python.exe
SET PYTHON_LIB_PATH=C:/Users/magnusg/anaconda3/envs/sa/lib/site-packages
SET RUNFILES_MANIFEST_ONLY=1
SET TEMP=C:\Users\magnusg\AppData\Local\Temp
SET TF2_BEHAVIOR=1
SET TMP=C:\Users\magnusg\AppData\Local\Temp
SET avariable=3
C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\VC\Tools\MSVC\14.28.29910\bin\HostX64\x64\cl.exe #bazel-out/x64_windows-opt/bin/tensorflow/lite/delegates/gpu/cl/kernels/_objs/converter/converter.obj.params
# Configuration: 4692499b740a707abb55d09dbd7c915fbefe7b433a9f783356c086c94c980334
# Execution platform: #local_execution_config_platform//:platform
cl : Command line warning D9035 : option 'experimental:preprocessor' has been deprecated and will be removed in a future release
cl : Command line warning D9036 : use 'Zc:preprocessor' instead of 'experimental:preprocessor'
cl_version.h: CL_TARGET_OPENCL_VERSION is not defined. Defaulting to 300 (OpenCL 3.0)
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(526): error C2059: syntax error: '__cdecl'
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(532): error C2065: 'cl_command_buffer_khr': undeclared identifier
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(532): error C2165: 'left-side modifier': cannot modify pointers to data
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(532): error C2513: 'cl_int *': no variable declared before '='
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(535): error C2065: 'cl_command_buffer_khr': undeclared identifier
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(535): error C2165: 'left-side modifier': cannot modify pointers to data
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(535): error C2513: 'cl_int *': no variable declared before '='
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(538): error C2065: 'cl_command_buffer_khr': undeclared identifier
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(538): error C2165: 'left-side modifier': cannot modify pointers to data
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(538): error C2513: 'cl_int *': no variable declared before '='
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(542): error C2061: syntax error: identifier 'cl_command_buffer_khr'
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(547): error C2065: 'cl_command_buffer_khr': undeclared identifier
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(548): error C2275: 'cl_command_queue': illegal use of this type as an expression
external/angle\CL/cl.h(32): note: see declaration of 'cl_command_queue'
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(549): error C2059: syntax error: 'const'
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(559): error C2065: 'cl_command_buffer_khr': undeclared identifier
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(560): error C2065: 'cl_command_buffer_info_khr': undeclared identifier
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(560): error C2275: 'size_t': illegal use of this type as an expression
tensorflow/lite/delegates/gpu/cl/kernels/converter.cc: note: see declaration of 'size_t'
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(561): error C2062: type 'void' unexpected
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(682): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(682): error C2146: syntax error: missing ';' before identifier 'clCreateCommandBufferKHR'
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(687): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(687): error C2146: syntax error: missing ';' before identifier 'clCommandNDRangeKernelKHR'
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(688): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
.\tensorflow/lite/delegates/gpu/cl/opencl_wrapper.h(688): error C2146: syntax error: missing ';' before identifier 'clGetCommandBufferInfoKHR'
Target //tensorflow/lite/delegates/gpu:libtensorflowlite_gpu_delegate.so failed to build
INFO: Elapsed time: 375.856s, Critical Path: 62.04s
INFO: 686 processes: 15 internal, 671 local.
FAILED: Build did NOT complete successfully
I do not know if I am on the right track and simply missing a minor detail or if I am missing the target completely. Any feedback would be helpful.

Error: xkeycheck.h(179): warning C4005: 'char16_t': macro redefinition” | Fatal error C1189

My question is similar to this: Error: "warning C4005: 'SWIGTEMPLATEDISAMBIGUATOR': macro redefinition"
I have a similar warning code (changing on 'char16_t'):
xkeycheck.h(179): warning C4005: 'char16_t': macro redefinition
Then:
xkeycheck.h(179): note: argomenti della riga di comando: vedere la precedente definizione
di 'char16_t'
(Translation: "note: arugments of the command line: look at the previous definition of 'char16_t')
But it keeps on giving this:
xkeycheck.h(250): fatal error C1189: #error: The C++ Standard Library forbids macroizing
keywords. Enable warning C4005 to find the forbidden macro.
Compiler: Visual Studio 2015
Support for char16_t and char32_t was added to Visual Studio 2015. This means you can no longer create symbols with these names, but then again you don't need them anymore as they already exist.
The solution is to remove the creation of the typedef or macro, or at least guard it when defined(MSC_VER) && _MSC_VER < 1900.
See MSDN:
char_16_t and char32_t You can no longer use char16_t or char32_t as aliases in a typedef, because these types are now treated as built-in. It was common for users and library authors to define char16_t and char32_t as aliases of uint16_t and uint32_t, respectively.
To update your code, remove the typedef declarations and rename any other identifiers that collide with these names.

Error during compilation of the code using make file in VC++

I am using below snippet in my code.
//from Vista WinNT.h
//
typedef struct _TOKEN_MANDATORY_LABEL {
SID_AND_ATTRIBUTES Label;
} TOKEN_MANDATORY_LABEL, *PTOKEN_MANDATORY_LABEL;
When I build this through VS2005 it give no error. But when I am building using make file i am getting below error in cmd:
Test.cpp(337) : error C2011: '_TOKEN_MANDATORY_LABEL' : 'struct' type redefinition
C:\Program Files\Microsoft SDKs\Windows\v6.1\include\winnt.h(7351) : see declaration of '_TOKEN_MANDATORY_LABEL'
I am using Opus software to use make file. Please let me know if I am missing something or needs to add some libs to overcome this error.

Fatal error (1017) invalid integer constant expression in ctype.h during migration from VS6 to VS5

While migrating VC++ code from VS6 (1998) to VS5(later year) I had to remove __STDC__ from project properties->Preprocessors. It fix lot of 'tagVariant' related errors.
Next what happened is function definitions were not recognized in many of the project file.
so I added #define __STDC__ in problem files which also took care of many unrecognized function definition related errors.
Now the project is left with only one error in compiling one file which says
"Fetal error (1017) invalid integer constant expression in ctype.h" and the code its pointing to line number 362 in ctype.h #define !__STDC__ etc
Any ideas - suggestions ?
I tried to - Remove any #define __STDC__ that you added locally. Also remove __STDC__ from Preprocessor definitions if you haven't done so already.
Select all the .c files in the solution explorer and select properties in the solution explorer context menu. In the properties dialog select /Za for C/C++\Language\Disable Language Extensions property.
Which took me back to ->
error C2065: ‘xyz’: undeclared identifier
: error C2182: ‘pqr’ : illegal use of type 'void'
: error C2065: ‘some variable’ : undeclared identifier
: error C2146: syntax error : missing ';' before identifier ‘classname’
: error C2143: syntax error : missing ';' before '*'
: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
: error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
: error C2040: ‘variable2’ : 'int *' differs in levels of indirection from ''unknown-type''
.
.
.
all the errors are in my files.
Reference :
http://social.msdn.microsoft.com/Forums/en-US/e68b0af5-ed25-4953-80e7-e88463149b77/fatal-error-1017-invalid-integer-constant-expression-in-ctypeh-during-migration-from-vs6-to-vs5?forum=vcgeneral
Wow...I could compile the project. Big Grin | :-D I do have lot of LINK errors but I will work on it.
I learnt this "Use precompiled headers ("#include "). This must be the first include in your .cpp file."
Changed the sequence of the .h files and I am all set now.

IntelliSense: argument of type "_TCHAR *" is incompatible with parameter of type "const char *"

Sorry for asking such a beginner question but I'm really new to openCV and VC++2010. I searched the net for an answer but I did not get an appropriate one.
Building the first project "First Program-Display a Picture" of the OReilly-LearningOpenCV book, I encountered these errors:
1-error C2664: 'cvLoadImage' : cannot convert parameter 1 from '_TCHAR *' to 'const char *'
2-IntelliSense: argument of type "_TCHAR *" is incompatible with parameter of type "const char *"
here's the code:
#include "stdafx.h"
#include "opencv\highgui.h"
int _tmain(int argc, _TCHAR* argv[])
{ IplImage* img = cvLoadImage( argv[1] );
cvNamedWindow( "Example1", CV_WINDOW_AUTOSIZE );
cvShowImage( "Example1", img );
cvWaitKey(0);
cvReleaseImage( &img );
cvDestroyWindow( "Example1" );
system("pause");
return 0;
}
and here's the message that I get from Microsoft Visual Studio 2010:
1>------ Build started: Project: FirstProgram_DisplayAPicture3, Configuration: Debug x64 ------
1>Build started 7/6/2013 11:13:00 AM.
1>InitializeBuildStatus:
1> Touching "x64\Debug\FirstProgram_DisplayAPicture3.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1> FirstProgram_DisplayAPicture3.cpp
1>FirstProgram_DisplayAPicture3.cpp(9): error C2664: 'cvLoadImage' : cannot convert parameter 1 from '_TCHAR *' to 'const char *'
1> Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
1>
1>Build FAILED.
1>
1>Time Elapsed 00:00:00.68
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Additional information that may be required:
I have made a win32 Console Application and in the second page of the wizard I have chosen precompiled header setting.
I have changed the platform of the project to x64 because I'm using win7x64 as the OS.
I have linked openCV library to my project properly.
I have just introduced highgui.lib as the additional dependencies
Any help would be appreciated.
What can I do to solve the two errors?
Edited section:
Note that these errors occur in line 6, I mean the line IplImage* img = cvLoadImage( argv[1] );
My edited section based on Roger Rowland's suggestion:
Based on what Roger Rowland said, I changed the Character Set property of the project to Not Set. The two errors mentioned above got solved but these errors rose up.
error LNK1120: 1 unresolved externals
error LNK2019: unresolved external symbol cvReleaseImage referenced in function main
How can I solve the problem?
Your project is configured for Unicode which means that the _TCHAR macro evaluates to wchar_t, which is a 16 bit UTF-16 data type on Windows. But the library you are calling accepts 8 bit char data.
So, you will need to make both sides of the interface match. Lots of ways to do that. The obvious options are:
Change your project to target ANSI (change the character set to multi byte in the VS project configuration).
Convert the input argument from UTF-16 to ANSI before calling the library.
It seems to me to be needlessly complex to use _TCHAR these days. That was useful when we needed to support Win9x (no Unicode support) and WinNT (supports Unicode) from a single code base. But I expect that nowadays you are targeting NT based systems and so you are safe to assume support for Unicode. In which case you can use wchar_t, wstring etc.
On the other hand, perhaps this is just a simple program for your personal use. In which case, since your library wants 8 bit characters, maybe it's simplest for you to target ANSI.
But either way I rather suspect that _TCHAR is just going to confuse you. I'd abandon that if I were you.
One final point is that you mention that you target x64 since your system is 64 bit. Do be aware that 64 bit systems can run 32 bit code perfectly well. If you want your program to be capable of running on a 32 bit system, and you don't have a pressing need to run under 64 bit, then it may be easier to target x86.
Your edit to the question asks a separate question, which I'll attempt to answer in spite of my reservations.
The unresolved external symbol error indicates that the linker could not resolve that particular symbol. In order for the linker to resolve it, it needs to be passed the .lib file that defines that symbol. You should double check that you have included all required .lib files in your additional dependencies. From what I can glean, there are multiple .lib files for OpenCV and you are probably missing the one which defines cvReleaseImage.
In my case I was including a header file twice.
I modified my library to be like:
#ifndef MY_LIB
#define MY_LIB
// code
#endif

Resources