Add Compiler Flags
已回答
I need to add Compiler Flags like -Wall etc. for my C Project. How can I do that?
请先登录再写评论。
@Michael you can do it by specifying the appropriate CMake options in File | Settings | Build, Execution, Deployment | CMake or in the CMakeLists.txt like this:
Update: this variant (with CMAKE_CXX_FLAGS) is for C++, not C.
I've added those lines to the CMakeLists.txt but I still can't see warnings when building. Where can I see them?
@Michael sorry for misleading, for C project it should be CMAKE_C_FLAGS instead of CMAKE_CXX_FLAGS.
Actually you just need to append CMAKE_C_FLAGS with the desired options, so you can do it in one line like this:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall")
Thanks!
I've added the following lines to the CMakeLists.txt file and it worked:
Now it works and I can see the warnings I wanted to see when I'm compiling.
Isn't there a GUI based method for setting the flags?
Like in Visual Studio?
Moreover, the above mentioned methods are old.
I think CMake promotes different solutions now.
There is no special GUI based method for setting such flags. Such settings can be set directly in a CMakeLists.txt or in a CMake profile.
@Anna, Could you add that as a feature request to have a wizard which can create the correct parameters in CMakeLists.txt bases on choices made using a GUI?
@Royi we already have several related feature requests: https://youtrack.jetbrains.com/issue/CPP-423, https://youtrack.jetbrains.com/issue/CPP-71 (it may look like a request about the find packages case, but it's about improving CMake GUI in general). Feel free to comment or upvote.
When you say CMake GUI, do you mean CMake's supplied UI or something inside CLion?
In Visual Studio most Compiler options can be altered using UI which is much more welcoming than dealing with CMake's files.
As far as I understand, CMake GUI gives an option to view in a graphic way what was defined in text.
It doesn't give control over things that weren't defined.
For instance I want a drop down box to set the optimization level, the vectorization level, the OpenMP support, etc...
I tried to edit cmake profile but it does not change existing cmake build, also it is not possible to make it all project setting. I looked into alternative - code inspection. On my ubuntu 18 it does not find many things like missing returns, incompatible types, etc. Compiler flags are much better in doing the latter with the flags and a couple of UI flags (wall, werror) would be very convincing to change from other IDEs.
@Royi, by CMake GUI I mean GUI inside CLion using which a user can specify CMake options instead of using CMakeLists.txt. We collect such requests in https://youtrack.jetbrains.com/issue/CPP-71, feel free to comment.
@Vlad,
>I tried to edit cmake profile but it does not change existing cmake build, also it is not possible to make it all project setting.
CMake project should be automatically reloaded when CMake profile is changed. Doesn't it happen in your case?
>On my ubuntu 18 it does not find many things like missing returns, incompatible types, etc.
What CLion version do you use? Do you have options in File | Settings | Languages & Frameworks | C/C++ | Clangd enabled or disabled? Please share code snippets illustrating the cases when CLion doesn't find problems.
I am wondering if there is a way to apply flag to every input file, instead of once for the whole project
@Jvinkemulder this thread is about Compiler Flags like -Wall. What flags are you asking about?