0

I want to generate a visual studio project 2010 with cmake from a CMakeLists.txt. Basically this works fine. However, one detail lead to my following observation:

The default character set for the generated visual studio project is 'Multibyte Character'. I want to change it to Unicode. As far as I found out, I need to define either _UNICODE or UNICODE, but I also need to undefine _MBCS. This works out, if I put it in the CMakeLists.txt, but I can't get it working, if I want to set these definitions by command line:

CMakeList.txt, works fine:

add_definitions ( -D_UNICODE )
remove_definitions ( -D_MBCS )

Command line, definitions are ignored by cmake, if I do it like this:

cmake -D_UNICODE="" -U_MBCS=""

Command line, definitions are ignored by cmake, if I do it like this:

cmake -DCMAKE_CXX_FLAGS_INIT="-D_UNICODE -U_MBCS"

I assumed that both ways are the same, but obviously the handling of the definitions from command line is different. I am doing something wrong or is it only possible by using add_definitions / remove_definitions ?

By the way, I'm using cmake 3.10.

tangoal
  • 724
  • 1
  • 9
  • 28

1 Answers1

1

The -D flags passed to cmake are completely unrelated to the -D flags passed to the compiler. See cmake(1). In short, cmake -DVARIABLE=VALUE ... is roughly equivalent to using set(VARIABLE VALUE CACHE STRING "") inside your CMakeLists.txt.

If you cannot use add_definitions or target_compile_definitions, you can still pass flags to the compiler by setting CMAKE_CXX_FLAGS_INIT the first time you invoke cmake or by changing CMAKE_CXX_FLAGS on later invocations of cmake:

cmake -DCMAKE_CXX_FLAGS_INIT="-D_UNICODE -U_MBCS" ...
Justin
  • 24,288
  • 12
  • 92
  • 142
  • Ok, I see that it is unrelated. CMAKE_CXX_FLAGS_INIT looks promising for me. I will try it. I'm not sure, if I need to set -D_MBCS or if I need to unset it with -U_MBCS. – tangoal Mar 21 '19 at 22:56
  • It does not work with CMAKE_CXX_FLAGS_INIT. But it works with CMAKE_CXX_FLAGS. – tangoal Mar 22 '19 at 09:22