Jebbs / DSFML-C

The back end for DSFML
4 stars 3 forks source link

MSVC - force unicode over multi-byte characterset #8

Closed neagix closed 9 years ago

neagix commented 10 years ago

A small improvement: by default CMake will build VC++ projects that build with the default multi-byte characterset instead of Unicode. Since Windows XP all multi-byte strings are converted internally to Unicode before usage in Windows APIs, thus going native is good both for compatiblity (long live UTF-8!) and performance reasons.

--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -18,6 +18,11 @@ set(VERSION_MAJOR 2)
 set(VERSION_MINOR 0)
 set(VERSION_PATCH 0)

+# force Unicode over Multi-byte
+if(COMPILER_MSVC)
+    add_definitions(-DUNICODE -D_UNICODE)
+endif()
+
 # add the CSFML header path
 include_directories(${CMAKE_SOURCE_DIR}/include)

NOTE: I tested this only with Visual Studio 2010, targeting 64bit:

cmake -G "Visual Studio 2010 Win64" ..
Jebbs commented 10 years ago

Well, I don't know a whole lot about CMake to be honest. This offers a speed improvement for compiling?

neagix commented 10 years ago

no, it makes compiled binaries represent in memory their strings as unicode instead of multi-byte. see also http://stackoverflow.com/questions/3064052/c-project-type-unicode-vs-multi-byte-pros-and-cons

Jebbs commented 10 years ago

Interesting stuff. Sounds like it definitely makes sense to do this. I'll see if I can get this added to SFML's CMake file too.

neagix commented 10 years ago

@Jebbs precisely! thanks. I also compiled SFML with that

Jebbs commented 10 years ago

Finally remembered to mention this to the SFML guys. Apparently they already do this, just in a different part of their CMake files.

https://github.com/LaurentGomila/SFML/blob/2215f55ef975843c70cfca0c42d06e4559c53534/src/SFML/Window/CMakeLists.txt#L69

neagix commented 10 years ago

@Jebbs so it was not needed? or was it added recently?

edit: is it only in Window class but not in all the others?

Jebbs commented 10 years ago

Looks like it was added about 8 months ago, so it isn't even a part of the 2.1 source. I'll open this back up and will make sure it get's added to DSFML at least until I get everything updated to SFML's HEAD.

And it only add's the definition to the Window submodule because that is where all the WIN32 api calls that deal with strings take place I'm guessing. The graphics module is just built on top of it.

neagix commented 10 years ago

@Jebbs do not agree, it should be defined at topmost level (for all submodules) because it affects all API calls

Jebbs commented 10 years ago

This is the issue I opened: https://github.com/LaurentGomila/SFML/issues/657

They would be able to answer your questions about this better than I could, so I would ask the SFML team directly.

KyrietS commented 1 year ago

Sorry for digging up this old thread but it appears to be well-indexed by Google when you search "CMake unicode character set". I just want CMake to generate Visual Studio Solution with this value set to "Use Unicode Character Set": image

It is a shame that in 2023 CMake still generates Visual Studio Solutions with Character Set set to "Use Multi-Byte Character Set" and the only option to override this is adding preprocessor definitions in CMake.

Anyway, in "mordern" CMake the use of add_definitions is discouraged. So now it would be:

 if (MSVC)
     target_compile_definitions(my_target PUBLIC UNICODE _UNICODE)
 endif()