How to change (and account for) default Visual Studio compiler flags

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

How to change (and account for) default Visual Studio compiler flags

Jason Heeris
I'm using CMake (3.12 currently) in Windows 10 to build a DLL to be compatible with what an existing program expects. To this end, I need near-total control over the compiler and linker flags used to generate that DLL.

I created a very simple project, with a single compilation unit, main.c, containing

int main(void) { return 0; }

My CMakeLists.txt started off containing only:

cmake_minimum_required(VERSION 3.8)
add_executable(main "main.c")

I then generated the build files for Visual Studio 2017 and ran "cmake --build .". The cl.exe contained a number of flags, presumably sensible defaults for VS 2017. I have no issue with that. But now I want to change those flags, starting with removing all of them completely. Obviously not super useful, but at this point I just want to know how to start from a clean slate.

Adding target_compile_options() won't work because it just appends to the defaults. Also totally understandable, but not what I need here.

I tried adding this to CMakeLists.txt:

set_target_properties(main PROPERTIES COMPILER_OPTIONS "")

I still end up with a bunch of compiler flags. So I try:

set(CMAKE_C_FLAGS "")
set(CMAKE_C_FLAGS_DEBUG "")

I'm not 100% that the latter is relevant, because I think the "_DEBUG" affects the build *type* (a Makefile generator thing) and not the build *configuration* (a VS thing). But I try it anyway. I still get lots of compiler flags. Specifically, at this point I have:

/c /nologo /W1 /WX- /diagnostics:classic /O2 /Oy- /D "CMAKE_INTDIR=\"Debug\"" /D _MBCS /Gm- /MD /GS /fp:precise /Zc:wchar_t /Zc:forScope /Zc:inline /Fo"main.dir\Debug\\" /Fd"main.dir\Debug\vc141.pdb" /Gd /TC /analyze- /FC /errorReport:queue

Some flags I recognise as perfectly harmless, others could in theory be overridden by appending contrasting flags. However some like /fp:precise have (AFAIK) a real effect on code generation. Others might not have an override, or might produce warnings if overridden.

Finally, I try something I saw on Stackoverflow[1]. I create a file "ClearVS.cmake":

if (MSVC)
    set(CMAKE_C_FLAGS_INIT "")
    set(CMAKE_CXX_FLAGS_INIT "")
    set(CMAKE_C_FLAGS_DEBUG_INIT "")
    set(CMAKE_CXX_FLAGS_DEBUG_INIT "")
endif()

In my CMakeLists.txt I add:

set(CMAKE_USER_MAKE_RULES_OVERRIDE "ClearVS.cmake")

...right after cmake_minimum_required(). The compiler flags remain.

So I have two questions:

1. How do I clear default compiler flags for the Visual Studio generator? I would strongly prefer a solution that can apply to specific targets, but if not, so be it.

2. How do I... I guess, research this behaviour? Where is it documented, what these default flags are, where they come from eg. which ones apply to all build types, which ones are there because I selected Debug instead of Release, which ones are there because it's VS 2017 not VS 2015, etc?

Again, let me emphasise that I absolutely get the need for a set of sensible default (and build type-specific) flags. It's a good thing that CMake has this. But in my situation, I need a bit more control.

- Jason


--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to change (and account for) default Visual Studio compiler flags

Innokentiy Alaytsev
Hello!


Jason Heeris wrote
> I'm not 100% that the latter is relevant, because I think the "_DEBUG"
> affects the build *type* (a Makefile generator thing) and not the build
> *configuration* (a VS thing).

AFAIK, the CMAKE_C[XX]_FLAGS_<CONFIG> work for all generators, so if you are
using MSVS they will apply to respective configurations (i.e. Release,
Debug, RelWithDebInfo). You can provide the desired build configuration with
the --config option to cmake in the command line:

cmake --build . --config Release [--target <target name]>

It may also worth looking at CMAKE_C[XX]_FLAGS_RELEASE as well as
CMAKE_EXE_LINKER_FLAGS[_<CONFIG>], CMAKE_MODULE_LINKER_FLAGS[_<CONFIG>] (if
you are building MODULE_LIBRARY targets),
CMAKE_SHARED_LINKER_FLAGS[_<CONFIG>] and
CMAKE_STATIC_LINKER_FLAGS[_<CONFIG>]. These flags are visible in cmake-gui
if you set (I don't know/remember the right word) the Advanced combo box
(setting Grouped will dramatically improve readability).

Hope this will be helpful in any way.

Best regards,
Innokentiy Alaytsev




--
Sent from: http://cmake.3232098.n2.nabble.com/
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to change (and account for) default Visual Studio compiler flags

Jason Heeris
On Thu., 28 Jun. 2018, 6:07 pm Innokentiy Alaytsev, <[hidden email]> wrote:
> These flags are visible in cmake-gui
> if you set (I don't know/remember the right word) the Advanced combo box
> (setting Grouped will dramatically improve readability).

So after sending my last email I actually tried that; not only all of the flag sets visible in the GUI's advanced mode but all of the _INIT ones I could deduce the existence of. The compiler flags are *still* there. Where are they coming from?

— Jason

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to change (and account for) default Visual Studio compiler flags

Innokentiy Alaytsev
Jason Heeris wrote
> On Thu., 28 Jun. 2018, 6:07 pm Innokentiy Alaytsev, &lt;

> alaitsev@

> &gt;
> wrote:
>> These flags are visible in cmake-gui
>> if you set (I don't know/remember the right word) the Advanced combo box
>> (setting Grouped will dramatically improve readability).
>
> So after sending my last email I actually tried that; not only all of the
> flag sets visible in the GUI's advanced mode but all of the _INIT ones I
> could deduce the existence of. The compiler flags are *still* there. Where
> are they coming from?

Are you setting CMAKE_<...> variables before creating you targets of after
that?

Also, try to output the values for the CMAKE_<...> flags and of the
COMPILE_OPTIONS property of each target (i.e. "message ("CMAKE_<...>
${CMAKE_<...>}")") to see where the flags appear first. You may try to
perform experiments, e.g. create a target without setting any CMAKE_<...>
variables and output its properties, set CMAKE_<...> variables and then
create a target and output its properties, etc. This way you will understand
the consequences of different actions.



--
Sent from: http://cmake.3232098.n2.nabble.com/
--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to change (and account for) default Visual Studio compiler flags

Jason Heeris
On Thu., 28 Jun. 2018, 8:45 pm Innokentiy Alaytsev, <[hidden email]> wrote:
Are you setting CMAKE_<...> variables before creating you targets of after
that?

I used the technique in that Stackoverflow link to print and override them before anything else happens in the project. They are still empty immediately before the add_executable() call. Certain options (eg. /Wx-) do not appear in any of those variables but still show up in the generated output.

— Jason 

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake
Reply | Threaded
Open this post in threaded view
|

Re: How to change (and account for) default Visual Studio compiler flags

Jason Heeris
In reply to this post by Jason Heeris
On Thu, 28 Jun 2018 at 15:19, Jason Heeris <[hidden email]> wrote:
> 1. How do I clear default compiler flags for the Visual Studio generator? I would strongly prefer a solution that can apply to specific targets, but if not, so be it.

I have created an issue about this on Gitlab: https://gitlab.kitware.com/cmake/cmake/issues/18137

- Jason
 

--

Powered by www.kitware.com

Please keep messages on-topic and check the CMake FAQ at: http://www.cmake.org/Wiki/CMake_FAQ

Kitware offers various services to support the CMake community. For more information on each offering, please visit:

CMake Support: http://cmake.org/cmake/help/support.html
CMake Consulting: http://cmake.org/cmake/help/consulting.html
CMake Training Courses: http://cmake.org/cmake/help/training.html

Visit other Kitware open-source projects at http://www.kitware.com/opensource/opensource.html

Follow this link to subscribe/unsubscribe:
https://cmake.org/mailman/listinfo/cmake