Basic CMakefile question for building OpenCV - c++

I am a novice in C++ programming, now having to build the OpenCV from source. I am getting an error related to the cudatoolkit installation
[ 13%] Built target opencv_cudev
[ 13%] Building NVCC (Device) object modules/core/CMakeFiles/cuda_compile.dir/src/cuda/cuda_compile_generated_gpu_mat.cu.o
nvcc fatal : Path to libdevice library not specified
CMake Error at cuda_compile_generated_gpu_mat.cu.o.cmake:266 (message):
Error generating file
/home/bruce/opencv-4.2.0/build/modules/core/CMakeFiles/cuda_compile.dir/src/cuda/./cuda_compile_generated_gpu_mat.cu.o
I look inside the cuda_compile_generated_gpu_mat.cu.o.cmake file and see the following lines.
# Generate the code
cuda_execute_process(
"Generating ${generated_file}"
COMMAND "${CUDA_NVCC_EXECUTABLE}"
"${source_file}"
${format_flag} -o "${generated_file}"
${CCBIN}
${nvcc_flags}
${nvcc_host_compiler_flags}
${CUDA_NVCC_FLAGS}
-DNVCC
${CUDA_NVCC_INCLUDE_ARGS}
)
I want to dig into where it's looking for the libdevice library. I don't know the technique for printing out the meaning of all these parameters. How do I print them out as I execute make?

If you want to debug this, and print the values in each of these CMake variables, you can add the message command to this CMake file:
# Generate the code
cuda_execute_process(
"Generating ${generated_file}"
COMMAND "${CUDA_NVCC_EXECUTABLE}"
"${source_file}"
${format_flag} -o "${generated_file}"
${CCBIN}
${nvcc_flags}
${nvcc_host_compiler_flags}
${CUDA_NVCC_FLAGS}
-DNVCC
${CUDA_NVCC_INCLUDE_ARGS}
)
# Print all the above variables to the console.
message("generated_file: ${generated_file}")
message("CUDA_NVCC_EXECUTABLE: ${CUDA_NVCC_EXECUTABLE}")
message("source_file: ${source_file}")
message("format_flag: ${format_flag}")
message("CCBIN: ${CCBIN}")
message("nvcc_flags: ${nvcc_flags}")
message("nvcc_host_compiler_flags: ${nvcc_host_compiler_flags}")
message("CUDA_NVCC_FLAGS: ${CUDA_NVCC_FLAGS}")
message("CUDA_NVCC_INCLUDE_ARGS: ${CUDA_NVCC_INCLUDE_ARGS}")
If you can modify how cmake is called from the command line, you can add the trace-expand option to print all the CMake calls, and expand the variables used:
cmake --trace-expand ..

Related

How to generate preprocess and assmeble code by cmake?

I am trying to get intermediate .i .s file by CMake when compiling .cpp file, but cmake default only output .o file. Is there any command to manipulate cmake to keep these intermediate file, thanks a lot!
If you are using gcc, try adding this line.
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -save-temps=obj")
Which flag to use depends on the compiler you are using. Also, you should strongly prefer to inject such compiler-and-scenario-specific flags into the build externally, rather than set()-ing them inside the build.
For g++ or clang++, the following invocation would be appropriate:
$ cmake -S . -B build -DCMAKE_BUILD_TYPE=Debug \
-DCMAKE_CXX_FLAGS="-save-temps=obj"
For MSVC it would be:
> cmake -S . -B build "-DCMAKE_CXX_FLAGS=/FA"

How to pre-build a library in CMake

I have been learning CMake and am having persistent build issues with the library Catch2.
I am using Qt as my IDE and have downlaoded Qt6 and MinGW 8 via the Qt maintainance tool.
My issue seems to be that during compilation my test/main.cpp, which includes Catch2, is bloating to the point where the compiler errors.
14:57:22: Running steps for project CMakeTestbed...
14:57:22: Starting: "<dir>\CMake\bin\cmake.exe" --build . --target all
[0/1 ?/sec] Re-running CMake...
-- Could NOT find Vulkan (missing: Vulkan_INCLUDE_DIR)
-- Using the single-header code from <dir>/build-CMakeTestbed-Desktop_Qt_6_0_0_MinGW_64_bit-Debug/_deps/json-src/single_include/
-- Version: 7.1.3
-- Build type: Debug
-- CXX_STANDARD: 17
-- Required features: cxx_variadic_templates
-- Configuring done
-- Generating done
-- Build files have been written to: <dir>/build-CMakeTestbed-Desktop_Qt_6_0_0_MinGW_64_bit-Debug
[1/17 3.7/sec] Automatic MOC and UIC for target fmt
[2/15 5.6/sec] Automatic MOC and UIC for target Catch2WithMain
[3/14 6.8/sec] Automatic MOC and UIC for target CMakeTestbedLib
[4/12 3.6/sec] Automatic MOC and UIC for target CMakeTestbed
[5/10 4.5/sec] Automatic MOC and UIC for target Tests
[6/9 0.4/sec] Building CXX object _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj
FAILED: _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj
C:\Qt\Tools\mingw810_64\bin\g++.exe -I_deps/catch2-build -I_deps/catch2-src -I_deps/catch2-build/Catch2WithMain_autogen/include -I_deps/json-src/include -I_deps/fmt-src/include -I_deps/catch2-src/single_include -g -std=gnu++17 -MD -MT _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj -MF _deps\catch2-build\CMakeFiles\Catch2WithMain.dir\src\catch_with_main.cpp.obj.d -o _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj -c _deps/catch2-src/src/catch_with_main.cpp
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccE30B5r.s: Assembler messages:
<dir>\AppData\Local\Temp\ccE30B5r.s: Fatal error: can't write 233 bytes to section .text of _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: 'File too big'
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccE30B5r.s: Fatal error: can't close _deps/catch2-build/CMakeFiles/Catch2WithMain.dir/src/catch_with_main.cpp.obj: File too big
[7/9 0.4/sec] Building CXX object test/CMakeFiles/Tests.dir/main.cpp.obj
FAILED: test/CMakeFiles/Tests.dir/main.cpp.obj
C:\Qt\Tools\mingw810_64\bin\g++.exe -DFMT_LOCALE -DJSON_USE_IMPLICIT_CONVERSIONS=1 -Itest -I"<dir>/CMakeTestbed/test" -Itest/Tests_autogen/include -I_deps/json-src/include -I_deps/fmt-src/include -I_deps/catch2-src/single_include -I"<dir>/CMakeTestbed/test/../libs" -I"<dir>/CMakeTestbed/test/../src" -I_deps/json-src/single_include -g -std=gnu++17 -MD -MT test/CMakeFiles/Tests.dir/main.cpp.obj -MF test\CMakeFiles\Tests.dir\main.cpp.obj.d -o test/CMakeFiles/Tests.dir/main.cpp.obj -c "<dir>/CMakeTestbed/test/main.cpp"
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: test/CMakeFiles/Tests.dir/main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccARyb79.s: Assembler messages:
<dir>\AppData\Local\Temp\ccARyb79.s: Fatal error: can't write 233 bytes to section .text of test/CMakeFiles/Tests.dir/main.cpp.obj: 'File too big'
C:/Qt/Tools/mingw810_64/bin/../lib/gcc/x86_64-w64-mingw32/8.1.0/../../../../x86_64-w64-mingw32/bin/as.exe: test/CMakeFiles/Tests.dir/main.cpp.obj: too many sections (32805)
<dir>\AppData\Local\Temp\ccARyb79.s: Fatal error: can't close test/CMakeFiles/Tests.dir/main.cpp.obj: File too big
ninja: build stopped: subcommand failed.
14:57:49: The process "C:\Program Files\CMake\bin\cmake.exe" exited with code 1.
Error while building/deploying project CMakeTestbed (kit: Desktop Qt 6.0.0 MinGW 64-bit)
When executing step "Build"
These issues only occur in debug builds, so I believe the combination of heavy templating, along with the debug symbols is hitting some MinGW compiler threshold...
I am trying something like
target_compile_options(Tests
-Wa
-mbig-obj
)
(Doesn't seem to actually be valid CMake, tried without the '-' too, regardless, this doesn't fix the problem, just hides it...)
But it seems to me the best option would be to make the library precompile in release (I don't need to debug the Catch2 internals, if I am debugging, it is because I have a broken test that I am investigating, and I shouldn't need the dirty details of the lib internals to do that right?)
I have a minimal working example here https://github.com/SebastianTroy/CMakeTestbed.
The main files of interest here would be:
catch2.cmake (from https://github.com/catchorg/Catch2/blob/v2.x/docs/cmake-integration.md#cmake-target)
test/CMakeLists.txt (includes Catch2)
test/main.cpp (the unit that is failing to compile)
I would really appreciate some help with either forcing CMake to precompile the library with higher optimisation flags than the target (to prevent the issue altogether) I would settle for help telling the compiler to ignore the overly large file (to just sidestep the issue)

Emscripten library make from CMake cannot link

I am using Grassroot Dicom viewer version 2.8.9 to compile into Emscripten library bc on Windows 10 64-bit platform.
I followed the instruction from this website
emconfigure cmake .
emmake make VERBOSE=1
After that I got library file ending with .a instead of .bc similar to
libgdcmcharls.a libgdcmCommon.a libgdcmDICT.a ....
when I looked into the assembly code for each static library file it starts with
! arch./
instead of
BC
as mention in the tutorial website Note
Some build systems may not properly emit bitcode using the above
procedure, and you may see is not valid bitcode warnings. You can run
file to check if a file contains bitcode (also you can manually check
if the contents start with BC). It is also worth running emmake make
VERBOSE=1 which will print out the commands it runs - you should see
emcc being used, and not the native system compiler. If emcc is not
used, you may need to modify the configure or cmake scripts.
I think this is the problem that I cannot generate libraries with contents start with BC but instead I got ! arch ./ ?
and of course those files cannot be linked from emcc ( cannot find reference error )
error: undefined symbol: _ZN4gdcm11ImageReader8GetImageEv
error: undefined symbol: _ZN4gdcm11ImageReaderC1Ev
error: undefined symbol: _ZN4gdcm11ImageReaderD1Ev
error: undefined symbol: _ZN4gdcm5Trace14GetErrorStreamE
So I modified GDCM CMakefile.txt according to this
if (EMSCRIPTEN)
set(CMAKE_AR "emcc" CACHE)
set(CMAKE_STATIC_LIBRARY_SUFFIX ".bc")
set(CMAKE_C_CREATE_STATIC_LIBRARY "<CMAKE_AR> -o <TARGET> <LINK_FLAGS> <OBJECTS>")
set(CMAKE_CXX_CREATE_STATIC_LIBRARY "<CMAKE_AR> -o <TARGET> <LINK_FLAGS> <OBJECTS>")
endif()
However, it does not help anything. The "make" still generates .a library files
The command that I compiled my code as follows; I also copy all .a files to the same folder as main.cpp
emcc -std=c++17 -O3 -s WASM=1 -s USE_WEBGL2=1 -s FULL_ES3=1 -s ALLOW_MEMORY_GROWTH=1 -o hello.html -s "EXTRA_EXPORTED_RUNTIME_METHODS=['ccall', 'cwrap']" --no-heap-copy libgdcmcharls.a libgdcmCommon.a libgdcmDICT.a libgdcmDSED.a libgdcmexpat.a libgdcmIOD.a libgdcmjpeg12.a libgdcmjpeg16.a libgdcmjpeg8.a libgdcmMEXD.a libgdcmMSFF.a libgdcmopenjp2.a libgdcmzlib.a libgdcmuuid.a libsocketxx.a main.cpp ...........
The link is my GDCM 2.8.9 library files. Compiled in Windows10 using
emcmake cmake and finally emmake make ( based on mingw32-make ).
Here is my CMakeCXXCompiler.cmake after calling emconfigure cmake .
set(CMAKE_CXX_COMPILER "C:/workspace/emsdk/emscripten/1.38.14/em++.bat")
set(CMAKE_CXX_COMPILER_ARG1 "")
set(CMAKE_CXX_COMPILER_ID "Clang")
set(CMAKE_CXX_COMPILER_VERSION "6.0.1")
set(CMAKE_CXX_COMPILER_VERSION_INTERNAL "")
set(CMAKE_CXX_COMPILER_WRAPPER "")
set(CMAKE_CXX_STANDARD_COMPUTED_DEFAULT "98")
set(CMAKE_CXX_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters;cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates;cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates;cxx_std_17")
set(CMAKE_CXX98_COMPILE_FEATURES "cxx_std_98;cxx_template_template_parameters")
set(CMAKE_CXX11_COMPILE_FEATURES "cxx_std_11;cxx_alias_templates;cxx_alignas;cxx_alignof;cxx_attributes;cxx_auto_type;cxx_constexpr;cxx_decltype;cxx_decltype_incomplete_return_types;cxx_default_function_template_args;cxx_defaulted_functions;cxx_defaulted_move_initializers;cxx_delegating_constructors;cxx_deleted_functions;cxx_enum_forward_declarations;cxx_explicit_conversions;cxx_extended_friend_declarations;cxx_extern_templates;cxx_final;cxx_func_identifier;cxx_generalized_initializers;cxx_inheriting_constructors;cxx_inline_namespaces;cxx_lambdas;cxx_local_type_template_args;cxx_long_long_type;cxx_noexcept;cxx_nonstatic_member_init;cxx_nullptr;cxx_override;cxx_range_for;cxx_raw_string_literals;cxx_reference_qualified_functions;cxx_right_angle_brackets;cxx_rvalue_references;cxx_sizeof_member;cxx_static_assert;cxx_strong_enums;cxx_thread_local;cxx_trailing_return_types;cxx_unicode_literals;cxx_uniform_initialization;cxx_unrestricted_unions;cxx_user_literals;cxx_variadic_macros;cxx_variadic_templates")
set(CMAKE_CXX14_COMPILE_FEATURES "cxx_std_14;cxx_aggregate_default_initializers;cxx_attribute_deprecated;cxx_binary_literals;cxx_contextual_conversions;cxx_decltype_auto;cxx_digit_separators;cxx_generic_lambdas;cxx_lambda_init_captures;cxx_relaxed_constexpr;cxx_return_type_deduction;cxx_variable_templates")
set(CMAKE_CXX17_COMPILE_FEATURES "")
set(CMAKE_CXX20_COMPILE_FEATURES "")
set(CMAKE_CXX_PLATFORM_ID "emscripten")
set(CMAKE_CXX_SIMULATE_ID "")
set(CMAKE_CXX_SIMULATE_VERSION "")
set(CMAKE_AR "C:/workspace/emsdk/emscripten/1.38.14/emar.bat")
set(CMAKE_CXX_COMPILER_AR "C:/Program Files/LLVM/bin/llvm-ar.exe")
set(CMAKE_RANLIB "C:/workspace/emsdk/emscripten/1.38.14/emranlib.bat")
set(CMAKE_CXX_COMPILER_RANLIB "C:/Program Files/LLVM/bin/llvm-ranlib.exe")
set(CMAKE_LINKER "C:/Program Files/LLVM/bin/wasm-ld.exe")
set(CMAKE_COMPILER_IS_GNUCXX )
set(CMAKE_CXX_COMPILER_LOADED 1)
set(CMAKE_CXX_COMPILER_WORKS TRUE)
set(CMAKE_CXX_ABI_COMPILED )
set(CMAKE_COMPILER_IS_MINGW )
set(CMAKE_COMPILER_IS_CYGWIN )
if(CMAKE_COMPILER_IS_CYGWIN)
set(CYGWIN 1)
set(UNIX 1)
endif()
set(CMAKE_CXX_COMPILER_ENV_VAR "CXX")
if(CMAKE_COMPILER_IS_MINGW)
set(MINGW 1)
endif()
set(CMAKE_CXX_COMPILER_ID_RUN 1)
set(CMAKE_CXX_IGNORE_EXTENSIONS inl;h;hpp;HPP;H;o;O;obj;OBJ;def;DEF;rc;RC)
set(CMAKE_CXX_SOURCE_FILE_EXTENSIONS C;M;c++;cc;cpp;cxx;mm;CPP)
set(CMAKE_CXX_LINKER_PREFERENCE 30)
set(CMAKE_CXX_LINKER_PREFERENCE_PROPAGATES 1)
# Save compiler ABI information.
set(CMAKE_CXX_SIZEOF_DATA_PTR "4")
set(CMAKE_CXX_COMPILER_ABI "")
set(CMAKE_CXX_LIBRARY_ARCHITECTURE "")
if(CMAKE_CXX_SIZEOF_DATA_PTR)
set(CMAKE_SIZEOF_VOID_P "${CMAKE_CXX_SIZEOF_DATA_PTR}")
endif()
if(CMAKE_CXX_COMPILER_ABI)
set(CMAKE_INTERNAL_PLATFORM_ABI "${CMAKE_CXX_COMPILER_ABI}")
endif()
if(CMAKE_CXX_LIBRARY_ARCHITECTURE)
set(CMAKE_LIBRARY_ARCHITECTURE "")
endif()
set(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX "")
if(CMAKE_CXX_CL_SHOWINCLUDES_PREFIX)
set(CMAKE_CL_SHOWINCLUDES_PREFIX "${CMAKE_CXX_CL_SHOWINCLUDES_PREFIX}")
endif()
set(CMAKE_CXX_IMPLICIT_LINK_LIBRARIES "")
set(CMAKE_CXX_IMPLICIT_LINK_DIRECTORIES "")
set(CMAKE_CXX_IMPLICIT_LINK_FRAMEWORK_DIRECTORIES "")
Finally I found the problem. The library that generated from command;
emconfigure cmake .
is not the emcc compatible even when I see the compiler it used em++.bat as the compiler.
How to spot the problem.
I use a program called "Hex Editor Neo" to open the library files ( use only small library files to test, big library files the program crashes).
If it is not compatible to use with emcc. The first few character will be ! arch This will not work. see the picture below for the library file that is wrong generated from cmake;
Make sure that you have BC like in the picture below;
but how to do this
you cannot just use
emconfigure cmake . <---------- wrong
you need to put an option to generate bitcode (.bc) by following command
emconfigure cmake . -DEMSCRIPTEN_GENERATE_BITCODE_STATIC_LIBRARIES=1
Then it will generate all library files ending with .bc and these library files can be linked with emcc

using clang to generate call graph for a project

I have c and c++ project, and i would like to check for dead function (function that could not be called), for that i want to build a call graph and see which could not be accessed from the written code.
for that i want to use clang with the flag "-S -emit-llvm" so i could creat a dot file.
im using autoconf to compile the project and the autoconfig dont recognize the file that has been compiled as an executable.
tried using this line :
./configure --enable-debug --prefix=/opt/ibutils CC=clang CXX=clang++ CXXFLAGS="-S -emit-llvm"
and this
./configure --enable-debug --prefix=/opt/ibutils CC=clang CXX=clang++ CXXFLAGS="-S -emit-llvm"
LD="llvm-link"
does anyone know the reason? have a suggestions what could i do?
thanks

CMake preload script for cache

I fire cmake with the -C mysettings.cmake.
The content of myfile.cmake is
set(CMAKE_INSTALL_PREFIX "C:/install/mylib" STRING)
Everything is generated but it seems the -C mysettings.cmake variable is not set. It is still installed in the default directory.
Cmake prints the message "loading initial cache file ../../script/cmake/mysettings.cmake"
without any error.
The full call:
cmake -C ../../script/cmake/mysettings.cmake -G "Visual Studio 9 2008" ../../source/mylib
Is there something wrong with my syntax?
From the CMake manual:
The given file should be a CMake script containing SET commands that
use the CACHE option, not a cache-format file.
So your myfile.cmake needs to look something like this:
set(CMAKE_INSTALL_PREFIX "C:/install/mylib" CACHE PATH "")