cxong / tinydir

Lightweight, portable and easy to integrate C directory and file reader

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Compilation error with MSVC 14 x86

Furkanzmc opened this issue · comments

UNICODE is defined in my project. But I get the following errors.

E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(263): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(263): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(268): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(268): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(286): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(286): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(288): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(288): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(295): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(295): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(296): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(296): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(497): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(497): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(504): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const WCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(504): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(517): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const WCHAR [260]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(517): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(523): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(523): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(524): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(524): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(531): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(531): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(532): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(532): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(609): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(609): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(635): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(635): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(640): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(640): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(654): error C2664: 'errno_t _splitpath_s(const char *,char *,std::size_t,char *,std::size_t,char *,std::size_t,char *,std::size_t)': cannot convert argument 1 from 'const TCHAR *' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(654): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(681): error C2664: 'char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(681): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(684): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [3]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(684): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(687): error C2664: 'char *strcat(char *,const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(687): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(710): error C2664: 'int strcmp(const char *,const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(710): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(732): error C2665: 'strrchr': none of the 2 overloads could convert all the argument types
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(732): note: while trying to match the argument list '(TCHAR [256], char)'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(735): error C2664: 'size_t strlen(const char *)': cannot convert argument 1 from 'TCHAR [256]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(735): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(752): error C2664: 'int strncmp(const char *,const char *,std::size_t)': cannot convert argument 1 from 'const TCHAR [256]' to 'const char *'
E:\Development\SourceTree\Utilities\external\tinydir\tinydir.h(752): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\string.h(529): note: could be 'char *strrchr(char *const ,const int)'
D:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\INCLUDE\vcruntime_string.h(73): note: or       'const char *strrchr(const char *,int)'

Take the first error as an example:
site_t strlen(const char *)': cannot convert argument 1 from 'const TCHAR *' to 'const char *

This means _tcslen is equal to strlen now, so _UNICODE is not defined when including tchar.h.
And cannot convert TCHAR* to char*, which means _UNICODE is defined when defining TCHAR.

This seems impossible.

I think you may include tchar.h or wchar.h or something else about widechar several times.
Some before define UNICODE and some after that.
Or maybe you have defined TCHAR yourself.

It seems that Visual Studio uses several macros to determine what TCHAR should be,
which makes it possible that TCHAR is UNICODE version but _tcslen is ANSI version.
But programmers should make efforts to avoid this situation.

I don't know what's wrong exactly without code.
But I've tested list_to_files.c in samples in Visual Studio 2010 and it works good.

Though we add unicode support to tinydir, it's unstable and not perfect.
ESPECIALLY with Visual Studio since I have little knowledge about it.

Here are some solutions:

  1. Check everything about widechar in your program, including external libraries.
    Make sure that the headers about widechar are not multiply defined.
    (In fact you don't need to define these headers as tinydir.h will include tchar.h)

  2. Try to define UNICODE and include tinydir.h before any other thing, even before stdio.h.
    There may be some headers, especially external headers, that make it down.

  3. Comment all other codes to test tinydir.h.
    Add some code to test what TCHAR and _tcslen refer to this time and that time.
    This should help you find what's wrong in it.

Thanks for your trust in tinydir.h.
Hope this project could help anyone who needs it.

And the third error:
char *strcpy(char *,const char *)': cannot convert argument 1 from 'TCHAR [260]' to 'char *
It's something about casting in C++.
Maybe Visual Studio 2015 add some C++ limit and implicit casting doesn't work anymore.
I'll check the Visual Studio 2015 features if it's true.

There should be C++ style switch in Visual Studio. What about trying an old style?
Have you tested list_to_files.c in samples?

Thank you both for the quick reply. These errors occur just by compiling tinydir.h file, even though I do not use it anywhere from within the project. I'm using CMake to compile the project (I'm sorry, I should have mentioned that.). And tinydir is inside a utility project that I add it to my Cocos2dx project with add_subdirectory.
In tinydir.h, it seems that UNICODE (It is defined in my CMakeLists.txt file) is defined but _UNICODE is not. But tinydir defined _UNICODE if UNICODE is already defined here:

// Starting from line #36
#if ((defined _UNICODE) && !(defined UNICODE))
#define UNICODE
#endif

#if ((defined UNICODE) && !(defined _UNICODE))
#define _UNICODE
#endif

So, I didn't think it was necessary to define _UNICODE explicitly. But turns out I needed to do that.
I'm sorry to have bothered you with such a trivial question but I learned an important lesson here. I hope someone else can benefit from my mistake. 😄

Also, for those wondering about the difference between UNICODE and _UNICODE here's the explanation.

commented

That's very strange; I can't imagine why UNICODE would be defined and not _UNICODE. Unless it's being undefd somewhere else? How are you adding the definition, with add_definitions(-DUNICODE)?

I use visual studio 2015 and the samples work fine, maybe it's some way you've set your project up.

It's strange.

One guess:
CMake does some initial works for Visual Studio.
And it restrictly require both UNICODE and _UNICODE to be defined.

I've never tested the codes using CMake.

PS: it's better to change project character setting to unicode.

Yes, I am adding the definitions with add_definitions(-DUNICODE). I searched for any occurrences of #undef in the project but none undefines _UNICODE.

Here's the CMakeLists.txt file I'm using for the project