Clion: Don't put `/nologo` flag to build.make - build

Question about MSVC Toolchain.
After generation, cmake makes files - one of them named build.make. See hello world project screen below:
Is it possible to prevent appending by default the /nologo (for cl.exe, link.exe) key in build.make ?

Check the value of CMAKE_EXE_LINKER_FLAGS variable. If it does contain /nologo flag, remove it and reconfigure the cache.
Otherwise, add /logo flag to CMAKE_EXE_LINKER_FLAGS variable.
also
set(CMAKE_VERBOSE_MAKEFILE TRUE CACHE BOOL nologo FORCE)

Related

target_link_libraries adds unwanted optimization flag when compiling in Debug mode, how-to remove?

I am including this external library https://github.com/S-Dafarra/alglib-cmake when compiling one of my projects.
In my CMakeLists.txt, I add link this library as PRIVATE:
find_package(ALGLIB REQUIRED)
target_link_libraries(<target> PRIVATE ALGLIB)
When I compile my project in debug mode with make VERBOSE=1, I find an -O3 flag added to my (g++) compiler flags, that I would prefer to avoid.
Is someone aware of a way to 'remove it' with some specific command in the CMakeLists?
I tried printing COMPILE_OPTIONS or CMAKE_CXX_FLAGS*, but they do not contain this flag. In the original ALGLIB.cmake file, they are defined as:
INTERFACE_COMPILE_OPTIONS "-O3;-DAE_OS=AE_POSIX;-pthread;-DAE_CPU=AE_INTEL"
Could I edit this property in my CMakeLists somehow? (I do not want to edit the original library). Thanks in advance.
If I remove the library from my project, or set it to INTERFACE instead of PRIVATE, then the O3 flag is gone.
Thanks for your inputs.
I followed your suggestion and came up with this solution:
find_package(ALGLIB REQUIRED)
if (CMAKE_BUILD_TYPE STREQUAL Debug)
get_target_property(ICO ALGLIB INTERFACE_COMPILE_OPTIONS)
string(REPLACE "-O3" "" ICO ${ICO})
string(REPLACE "-" " -" ICO ${ICO})
separate_arguments(ICO)
set_target_properties(ALGLIB PROPERTIES INTERFACE_COMPILE_OPTIONS "${ICO}")
endif()
Follow-up: https://github.com/S-Dafarra/alglib-cmake/issues/4

Visual Studio 2019 and googletest: Linker errors [duplicate]

I am trying to integrate GTest with CMake as seamlessly as possible. But the default build type for my test projects are /MDd and GTest defaults to /MTd. I am manually changing GTest project properties to emit debug DLL.
But every time I make changes to my CMakeLists.txt, GTest defaults back to /MTd. How do I stop this?
You can define gtest_force_shared_crt to ON before including gtest to achieve this. You can either do this via the command line:
cmake . -Dgtest_force_shared_crt=ON
or in your CMakeLists.txt:
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
I think a better option is #Fraser's answer - in that case, cmake + gtest 'just work'.
It's worth mentioning that in order to override the internal gtest option setting, you need to put the variable in the cmake cache:
set( gtest_force_shared_crt ON CACHE BOOL "Always use msvcrt.dll" )
If Ted Middleton's answer doesn't work, try to use FORCE:
set( gtest_force_shared_crt ON CACHE BOOL "Always use msvcrt.dll" FORCE)
It worked for me
We solved the problem by bypassing GTest's own build system and compiling GTest as a CMake object library from its unity build source file gtest-all.cc:
# compile Google Test as an object library
add_library(gtest OBJECT "${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.6.0/src/gtest-all.cc")
set_property(TARGET gtest PROPERTY INCLUDE_DIRECTORIES
"${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.6.0"
"${CMAKE_CURRENT_SOURCE_DIR}/gtest-1.6.0/include")
That way GTest will always be compiled with the same options that we use for the project.
A test executable that uses GTest can then be built in the following way:
add_executable(test_executable ${TESTS_SRC} $<TARGET_OBJECTS:gtest>)
add_test(NAME test COMMAND test_executable)

Compile error CMAKE with CUDA on Visual Studio C++

I am having a problem compiling a project with CUDA on VS2017 c++.
I can get rid of this error by just changing the line in
Properties->CUDA C/C++->Command Line-> Additional Options:
%(AdditionalOptions) -Zi /W3 /wd4005 /wd4003 /wd4996 /nologo -Xcompiler="/EHsc -Zi -Ob0"
changed to
%(AdditionalOptions) -Xcompiler="/EHsc -Ob2"
After changing this option, the project compiles without any errors.
My Question is, how can I do it from CMAKE so that I don't have to change this property every time I regenerate the project?
Your .sln file is generated from cmake. You can use cmake-gui.exe to load the cmake cache file and query strings, then override that string in your CMakeLists.txt.
For your case, you would like to remove the annoying "/W3 /wd4005 /wd4003 /wd4996 /nologo " options. You can do like this:
Here, I open cmake-gui.exe, fillin the source folder and build folder of your project, and type "w3" in search box, and get "CMAKE_C_FLAGS". So you can override "CMAKE_C_FLAGS" in your CMakeLists.txt:
set(CMAKE_C_FLAGS "") # set it to empty
# you may also replace some options inside it, go and query cmake docs to see how to do that
Then, clean all the previous generated build files and re-cmake.

CMake: how to clear target compile options

In my project I have defined some general compile options using CMAKE_CXX_FLAGS globally. Some other options, that in common case should be applied to all targets, are specified using add_compile_options() in my main CMakeLists file.
For example I want the flag -Wconversion be applied to all targets.
But I have one external library that produces to many warnings with this option enabled. So I want to disable option only for this particular lib:
get_target_property(EXTLIB_COMPILE_FLAGS ext_lib COMPILE_OPTIONS )
list(REMOVE_ITEM EXTLIB_COMPILE_FLAGS -Wconversion)
set_target_properties(ext_lib PROPERTIES COMPILE_OPTIONS ${EXTLIB_COMPILE_FLAGS } )
But right now only -Wconversion was setted using add_compile_options().
And target does not have any own additional flags. So, after removing the only entry from the list I will get an empty list. Call to set_target_properties() fails with error:
set_target_properties called with incorrect number of arguments.
Is any way to clear some of target properties completly?
I'm using CMake 3.11
Turning my comment into an answer
Just add quotes:
set_target_properties(ext_lib PROPERTIES COMPILE_OPTIONS "${EXTLIB_COMPILE_FLAGS}")
Now - if EXTLIB_COMPILE_FLAGS variable is empty - you end-up having an empty string and just not an "missing argument".

cmake if else with option

I have a problem using option together with if-else statement in cmake.
project(test)
option(TESTE "isso é um teste" OFF)
if(TESTE)
message("true")
else()
message("false")
endif()
add_executable(test main.cpp)
It always displays true even if I put OFF in the options, what am I doing wrong?
That's because the value of the option is stored in the cache (CMakeCache.txt).
If you change the default value in the CMakeLists but the actual value is already stored in the cache, it will just load the value from the cache.
So to test the logic in your CMakeLists, delete the cache each time before re-running CMake.
I had a similar problem and was able to solve it using a slightly different approach.
I needed some compilation flags to be added in case cmake was invoked with an option from the command line (i.e cmake -DUSE_MY_LIB=ON).
If the option was missing in the cmake invocation I wanted to go back to default case which was turning the option off.
I ran into the same issues, where the value for this option was being cached between invocations:
cmake -DUSE_MY_LIB=ON .. #invokes cmake and puts USE_MY_LIB=ON in CMake's cache.
cmake .. #invokes cmake with the cached option ON, instead of OFF
The solution I found was clearing the option from within CMakeLists.txt after the option was used:
option(USE_MY_LIB "Use MY_LIB instead of THEIR_LIB" OFF) #OFF by default
if(USE_MY_LIB)
#add some compilation flags
else()
#add some other compilation flags
endif(USE_MY_LIB)
unset(USE_MY_LIB CACHE) # <---- this is the important!!
Note:
The unset option is available since cmake v3.0.2
Try this, it works for me
unset(USE_MY_LIB CACHE)