CMake and MinGW cannot disable "is deprecated" warnings also setting CMAKE_WARN_DEPRECATED - cmake

I am using CMake version 3.23 with MinGW compiler
This are the basic CMAKE settings:
CMAKE_MINIMUM_REQUIRED(VERSION 3.23)
## GENERAL INITIALIZATION
SET(CMAKE_CXX_STANDARD 11)
SET(CMAKE_CXX_STANDARD_REQUIRED ON)
SET(CMAKE_CXX_EXTENSIONS OFF)
I use CMake -G "MinGW Makefiles" .. to build the project with MinGW generator and everything works. To compile I can use both CMake --build . or mingw32-make.exe: they both warks but gives the warnings that I am describing.
The problem is that I am using some external libraries (boost, turtle) that uses the template<class> class std::auto_ptr that is deprecated.
For this reason, when I compile my project, I get this warning:
C:/turtle/include/turtle/detail/action.hpp:220:22: warning: 'template<class> class std::auto_ptr' is deprecated [-Wdeprecated-declarations]
mutable std::auto_ptr< Result > v_;
^~~~~~~~
And many other of this type as well.
As I mentioned, those warnings are not a problem for compilation, but I would like to disable them.
I have tried to set CMAKE_WARN_DEPRECATED:
SET(CMAKE_WARN_DEPRECATED OFF CACHE BOOL "" FORCE)
but it does not work.
I have also tried
add_compile_options(-Wno-deprecated-declarations)
but it does not work as well.
EDIT:
As suggested in the comments by Alex Reinking CMAKE_WARN_DEPRECATED is not related to compilation warning but to CMAKE warning.
The only way I've found to eliminate the warnings was to redefine CMAKE_CXX_FLAGS:
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-deprecated-declarations")
Hope it can be helpful

Related

How to create CMakeLists for this make file [duplicate]

Work on Ubuntu 16
I used g++ main.cpp -lpq command for compiler my small project. Now I use Clion and wanna do same what I do with g++. But I can't add compiler flags in cmake file and get compile error.
cmake_minimum_required(VERSION 3.5.1)
project(day_g)
set(CMAKE_CXX_FLAGS "-lpq")
add_definitions(-lpq)
message("CMAKE_CXX_FLAGS is ${CMAKE_CXX_FLAGS}")
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(day_g ${SOURCE_FILES})
Also I run only cmake file and get CMAKE_CXX_FLAGS with -lpq flag.
CMAKE_CXX_FLAGS is -lpq
-- Configuring done
-- Generating done
How properly add compiler flags to cmake file?
Flag -l is for linker, not for compiler. This flag is used for link with libraries. CMake has special command target_link_libraries for that purpose:
target_link_libraries(day_g pq)
-lq is not a compiler flag (CFLAGS) but a linker flag.
To pass a library in a CMake project you should use:
target_link_libraries(target_name libraries...)
Note that if you specify 'q' as library the project will link with libq.a or, if you are on windows q.dll.
... in your CMakeLists.txt the correct line to add is:
target_link_libraries(day_g pq)
Note also that when you add a CFLAG you should also "remember" the previous ones that may be added by libraries or by your platform, ie:
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -O3")
To check the exact flags cmake is passing to compiler or linker you can always run, from the build directory, the following command:
make VERBOSE=1

Unknown CMake command "CMAKE_DEPENDENT_OPTION"

I faced with an issue that my installed on Ubuntu cmake
cmake --version
cmake version 3.17.2
doesn't recognize CMAKE_DEPENDENT_OPTION command.
So, my CMakeLists.txt with dependent option example from cmake.org/v3.16:
cmake_minimum_required(VERSION 3.4.1)
project(myexe)
CMAKE_DEPENDENT_OPTION(USE_FOO "Use Foo" ON
"USE_BAR;NOT USE_ZOT" OFF)
file(GLOB SRC_FILES "src/*.cpp")
add_executable(${PROJECT_NAME} ${SRC_FILES})
The following error is displayed when running cmake:
CMake Error at CMakeLists.txt:5 (CMAKE_DEPENDENT_OPTION):
Unknown CMake command "CMAKE_DEPENDENT_OPTION".
Why does it happen because the spec says it is supported? Thanks for any help!
You need to add
include(CMakeDependentOption)
before accessing this function.

Compiling libtorch to WebAssembly with Emscripten using CMake build

I am trying run my PyTorch model (exported to TorchScript) in the browser using WebAssembly. To do this, I need to compile libtorch using Emscripten. As PyTorch docs suggest using CMake as a compiler I stuck with that.
I've manage to compile libtorch using CMake without issues as specified here. Secondly, I also manage to compile a simple test program to WASM using Emscripten and CMake (for conciseness and focusing on the problem-at-hand I haven't included the CMakeLists.txt files for either of these)
Where the issues start is when I try to compile both together, here's my CMake file:
set(project "wasm-example")
cmake_minimum_required(VERSION 3.0.0 FATAL_ERROR)
set(CMAKE_TOOLCHAIN_FILE $HOME/.local/share/emsdk/upstream/emscripten/cmake/Modules/Platform/Emscripten.cmake)
set(CMAKE_BUILD_TYPE Debug)
project(${project})
SET_PROPERTY(GLOBAL PROPERTY TARGET_SUPPORTS_SHARED_LIBS TRUE)
set(CMAKE_FIND_ROOT_PATH_MODE_PACKAGE BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY BOTH)
set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE BOTH)
set(CMAKE_PREFIX_PATH $PWD/libtorch)
message(STATUS "Build type: ${CMAKE_BUILD_TYPE}")
message(STATUS "Using prefix path: ${CMAKE_PREFIX_PATH}")
message(STATUS "Using toolchain file: ${CMAKE_TOOLCHAIN_FILE}")
include(CTest)
enable_testing()
set(SOURCE_FILES main.cpp)
# process exported functions
set(exports_string "")
list(JOIN exports "," exports_string)
# set compiler and flags
set(CMAKE_C_COMPILER emcc)
set(CMAKE_CPP_COMPILER em++)
set(CMAKE_CXX_FLAGS "-s WASM=1 -s EXIT_RUNTIME=1 -s EXTRA_EXPORTED_RUNTIME_METHODS=\"[\"cwrap\", \"getValue\", \"setValue\"]\" ${TORCH_CXX_FLAGS}")
message(STATUS "Set CMAKE_CXX_FLAGS to: ${CMAKE_CXX_FLAGS}")
# find external target_link_libraries
find_package(Torch REQUIRED)
# specify the project
add_executable(${project} ${SOURCE_FILES})
target_link_libraries(${project} "${TORCH_LIBRARIES}")
set_property(TARGET ${project} PROPERTY CXX_STANDARD 14)
Note that I'm using the CPU-only distribution of libtorch, the CUDA-enabled version leads to even more issues because cuda is dynamic-only.
cmake ..src passes, however make throws several errors:
wasm-ld: error: unknown argument: --no-as-needed
wasm-ld: error: unknown argument: --as-needed
wasm-ld: error: unknown argument: --no-as-needed
wasm-ld: error: unknown argument: --as-needed
wasm-ld: error: unknown file type: /home/max/repos/sandbox/cpp/wasm-example/libtorch/lib/libtorch_cpu.so
wasm-ld: error: unknown file type: /home/max/repos/sandbox/cpp/wasm-example/libtorch/lib/libtorch.so
Clearly Emscripten is having some trouble with the dynamic nature of libtorch, so my first question would be if it's even possible to compile dynamic libraries using Emscripten? If this is possible, it would be interesting to try and compile GPU-enabled libtorch using Emscripten.
UPDATE
I tried to compile with the static CPU-only distribution of libtorch, but this leads to the exact same errors. However, an old GitHub issue on the Pytorch github was just revived stating that I'm not the only one with this problem. Hopefully this GitHub issue will address the issues I'm having.

How to get cmake generate -std=c++14 flag for qcc compiler

I'm trying to cross compile some c++ library for QNX neutrino using cmake. In CMakeLists.txt file I specified CMAKE_CXX_STANDARD 14 required, but the resulting compiler command line does not contain the -std=c++14 option.
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
I've tried using target compile features:
target_compile_features(my_library PRIVATE cxx_std_14)
but that gives me the following error:
CMake Error at CMakeLists.txt:53 (target_compile_features):
target_compile_features no known features for CXX compiler
"QCC"
version 5.4.0.
When I'm using check_cxx_compiler_flag feature, it seems to recognize the option:
include(CheckCXXCompilerFlag)
check_cxx_compiler_flag(-std=c++14 CXX14_SUPPORT)
if(CXX14_SUPPORT)
message("c++14 support found")
else()
message("c++14 unsupported")
endif()
This outputs message
c++14 support found
Running qcc manually it accepts the -std=c++14 option just fine and the code using std::make_unique compiles just fine.
Also using the native compiler (Ubuntu 18.04, gcc) everything work fine with cmake generated makefiles. make VERBOSE=1 displays the following command line (I removed some directories):
/usr/local/bin/c++ -Dshm_transfer_EXPORTS -I... -fPIC -std=gnu++14 -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp
as opposed to the command line using qcc toolchain:
.../qnx700/host/linux/x86_64/usr/bin/qcc -lang-c++ -Vgcc_ntox86_64 -lang-c++ -Dshm_transfer_EXPORTS -I... -fPIC -o CMakeFiles/shm_transfer.dir/src/SharedMemoryTransfer.cpp.o -c .../SharedMemoryTransfer.cpp
I would have expected the cmake command to recognize that qcc supports the -std=c++14 option and generates the corresponding command lines because of the CMAKE_CXX_STANDARD setting.
Use
set_property(TARGET ${PROJECT_NAME} PROPERTY LINKER_LANGUAGE CXX)
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14)
. Using this you can stick the compiler setting to the target, while global flags are dis encouraged and can be overwritten by other cmake consumers. This the reason I assume why the deprecated set(CMAKE_CXX_STANDARD 14) did not help you: I can not see your full CMakeLists.txt and bet you have many sub folders and other targets, which could reset the CMAKE_CXX_STANDARD them selfs. Also make sure of the ordering of the CMake commands.
And you can replace ${PROJECT_NAME} with my_library if you want.
add_compile_options(-std=gnu++14)
Add this to your project level CMakeLists.txt file, not in toolchain.

CMake cannot find OpenMP

I am trying to compile with OpenMP. My CMakeLists.txt contains the line
find_package(OpenMP REQUIRED)
and CMake errors out with
CMake Error at /opt/ros/groovy/share/catkin/cmake/catkinConfig.cmake:72 (find_package):
Could not find a configuration file for package openmp.
Set openmp_DIR to the directory containing a CMake configuration file for
openmp. The file will have one of the following names:
openmpConfig.cmake
openmp-config.cmake
Checking my filesystem, I see that I have /usr/share/cmake-2.8/Modules/FindOpenMP.cmake but no openmpConfig.cmake or openmp-config.cmake. What do I need to do to fix this?
CMake has a FindOpenMP module even in 2.x versions. See http://www.cmake.org/cmake/help/v3.0/module/FindOpenMP.html
So I'll do this:
OPTION (USE_OpenMP "Use OpenMP" ON)
IF(USE_OpenMP)
FIND_PACKAGE(OpenMP)
IF(OPENMP_FOUND)
SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${OpenMP_C_FLAGS}")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${OpenMP_CXX_FLAGS}")
ENDIF()
ENDIF()
According to the Modern CMake online book, this is how you configure OpenMP support with CMake:
find_package(OpenMP)
if(OpenMP_CXX_FOUND)
target_link_libraries(MyTarget PUBLIC OpenMP::OpenMP_CXX)
endif()
What you definitely should not do is to add flags like -fopenmp manually (like the accepted answer recommends) because that may not be portable.
OpenMp is not a package, if it's supported, it comes as a part of the your compiler. Try setting CMAKE_C_FLAGS or CMAKE_CXX_FLAGS accordingly. e.g:
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fopenmp") activates OpenMP for compiling C sources when gcc is used. For other compilers, you should first detect the compiler and then add appropriate flags
iNFINITEi's answer doesn't work for me.
I use Ubuntu, trying to compile some code with OpenCV static library. After linking, I got this:
'"/usr/bin/ld: /usr/local/lib/libopencv_core.a(parallel.cpp.o): undefined reference to symbol 'omp_set_dynamic##OMP_1.0'"'
So I tried iNFINITEi's approach, then I have:
'CMake Error at /usr/local/share/cmake-3.13/Modules/FindPackageHandleStandardArgs.cmake:211 (message):
No REQUIRED_VARS specified for FIND_PACKAGE_HANDLE_STANDARD_ARGS()
Call Stack (most recent call first):
/usr/local/share/cmake-3.13/Modules/FindOpenMP.cmake:513 (find_package_handle_standard_args)
CMakeLists.txt:8 (FIND_PACKAGE)'
At last, I add "-fopenmp=libomp" to CMAKE_CXX_FLAGS, solved my problem.
You should install libomp with brew install libomp
i use macOS and it worked smoothly for me.