Please see Building On Linux#Build using CMake and Ninja. 4.5 How can I check in the CMakeLists.txt code whether I'm on Windows or Linux?.4.4 Which C preprocessor macros tell me whether I'm on Windows or Linux?.4.3 Which variables are set in CMake when running CMakeLists.txt?.4.2 How can I see the actual compiler commands: Verbose mode?.4.1 How can you change between a Debug and Release build.3 GnuCash Program Configuration Variables.2 GnuCash Documentation Configuration Variables.Or you could just put the flag in a toolchain/preset where you already know what compiler you're using. Target_compile_options(lib PRIVATE -Wsuggest-override) If (CMAKE_CXX_COMPILER_ID MATCHES "GNU" AND CMAKE_CXX_COMPILER_VERSION VERSION_GREATER_EQUAL 5.1) Must use a genex for INTERFACE/PUBLIC because the whole genex gets exported, whereas this flag will get exported verbatim. You can work around this with generator expressions and/or complex logic (like the _UNDEF trick above), but it's generally just easier and more convenient to use a toolchain or these new presets.įor instance, to correctly add -Wsuggest-override, you would need to write: target_compile_options(lib PRIVATE $,5.1>,$>:-Wsuggest-override>) If someone tries to compile your library with a different compiler (or even a different version of the same compiler) they could run into issues if, for example, you add a warning flag that's too new or not supported. Presets are opt-in for the user, which is important if you're distributing a library.Įxpanding on points 4 and 5: it is a bad idea to add flags unless they absolutely must be there to compile correctly and there isn't a built-in feature for reaching those flags (eg.You don't have to write awkward logic in your CMakeLists.txt.It can override flags that are usually unconditionally added to the *_INIT flags.It's compatible with other toolchain files (like vcpkg's in the second preset).It makes the command line a lot simpler.Then, from the source directory, your CMake command line would just become: $ cmake -preset=default set(CMAKE_CXX_FLAGS_DEBUG "_UNSET" CACHE STRING "") You must set the flags variables to something 'special' before the project command. The compiler isn't determined, however, until it has already filled the variables for me. I'd seen other comments that solve the problem of setting CMAKE_CXX_FLAGS_DEBUG and friends without having them get clobbered, but this originally didn't work for me because I was trying to also select them based on the compiler that was in use. I've created a solution that works, though I still am not super thrilled about what I have to do. I could add other build types as well, but I don't really see why that should be necessary just to change a few simple things. Obviously, this is a nasty hack and doesn't completely work(in the case of cmake -Dwhatever=thisorthat). Set(DEFAULTS_SET TRUE CACHE BOOL "" FORCE) Set(CMAKE_CXX_FLAGS_DEBUG "-this -that" CACHE STRING "" FORCE) I managed to hack it to work with CCMAKE by doing the following, but this still doesn't work with cmake -DCMAKE_CXX_FLAGS_DEBUG as it overwrites the user change AFTER he's done it: set(DEFAULTS_SET FALSE CACHE BOOL "") If I use FORCE in my set command: set(CMAKE_CXX_FLAGS_DEBUG blah CACHE STRING "" FORCE), it will overwrite it every time the script is run, eliminating the possibility for the user to change it if he wishes. The problem being that CMAKE sets and caches its own defaults for these, and if you try to overwrite the variables without using FORCE, the "defaults" are never changed. Now, I know how to set these values, but I don't know how to make them user editable in the two usual ways: by using - DCMAKE_CXX_FLAGS_DEBUG=yourflags on the command line or by configuring it with ccmake or CMakeSetup. Basically, I have some project defaults that differ just slightly from CMake's (for release, for instance), and I shouldn't have to ask myself "Oh, does their -O3 or our -O2 take precedence when added with add_compile_options." I would like to change the default values for CMAKE_CXX_FLAGS_RELEASE or CMAKE_CXX_FLAGS_DEBUG in CMake.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |