Include tensorflow lite libraries in CMakeLists.txt of C++ project giving errors "undefined reference to `ruy::ScopedSuppressDenormals" - cmake

I'm trying to include TensorFlow lite libraries in CMakeLists.txt of C++ project. I followed the instructure in https://www.tensorflow.org/lite/guide/build_cmake
git clone https://github.com/tensorflow/tensorflow.git tensorflow_src
mkdir tflite_build && cd tflite_build
cmake ../tensorflow_src/tensorflow/lite -DTFLITE_ENABLE_XNNPACK=OFF
-DTFLITE_ENABLE_EXTERNAL_DELEGATE=OFF TFLITE_ENABLE_RUY=ON/OFF (tried both option)
cmake --build . --config release -j24
and added all the libs to the CmakeLists.txt file as follow
cmake_minimum_required(VERSION 3.0.0)
project(main)
set(TENSORFLOW_SRC_DIR "test_cpp/tensorflow_src")
set(TFLITE_DIR "test_cpp/tflite_build")
find_package(OpenCV 4 REQUIRED)
file(GLOB SOURCE_FILES src/*.cpp src/*.h )
message(${SOURCE_FILES})
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
include_directories(
${TENSORFLOW_SRC_DIR}
${TFLITE_DIR}/eigen
${TFLITE_DIR}/neon2sse
${TFLITE_DIR}/abseil-cpp
${TFLITE_DIR}/farmhash/src
${TFLITE_DIR}/flatbuffers/include
${TFLITE_DIR}/gemmlowp/public
${TFLITE_DIR}/gemmlowp
${TFLITE_DIR}/ruy
${TFLITE_DIR}/cpuinfo/include
)
target_link_libraries(
${CMAKE_PROJECT_NAME} PUBLIC
${OpenCV_LIBS}
${TFLITE_DIR}/_deps/farmhash-build/libfarmhash.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_marshalling.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_program_name.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_reflection.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_private_handle_accessor.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_config.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_commandlineflag_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags_commandlineflag.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/flags/libabsl_flags.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cordz_functions.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_strings_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cordz_handle.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_str_format_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cordz_info.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cord.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_cord_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/strings/libabsl_strings.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/container/libabsl_raw_hash_set.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/container/libabsl_hashtablez_sampler.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/synchronization/libabsl_synchronization.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/synchronization/libabsl_graphcycles_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/hash/libabsl_city.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/hash/libabsl_hash.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/hash/libabsl_low_level_hash.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/time/libabsl_civil_time.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/time/libabsl_time_zone.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/time/libabsl_time.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/profiling/libabsl_exponential_biased.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/types/libabsl_bad_optional_access.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/types/libabsl_bad_variant_access.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/numeric/libabsl_int128.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_raw_logging_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_throw_delegate.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_base.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_log_severity.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_spinlock_wait.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/base/libabsl_malloc_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/status/libabsl_status.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_stacktrace.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_demangle_internal.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_symbolize.a
${TFLITE_DIR}/_deps/abseil-cpp-build/absl/debugging/libabsl_debugging_internal.a
${TFLITE_DIR}/_deps/flatbuffers-build/libflatbuffers.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_ctx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_avx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_have_built_path_for_avx512.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_cpuinfo.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_frontend.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_blocking_counter.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_avx512.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_apply_multiplier.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_block_map.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_trmul.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_avx2_fma.a
${TFLITE_DIR}/_deps/ruy-build/ruy/profiler/libruy_profiler_instrumentation.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_denormal.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_prepacked_cache.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_system_aligned_alloc.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_context_get_ctx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_allocator.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_have_built_path_for_avx2_fma.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_avx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_wait.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_context.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_avx2_fma.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_thread_pool.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_arm.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_tune.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_kernel_arm.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_have_built_path_for_avx.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_prepare_packed_matrices.a
${TFLITE_DIR}/_deps/ruy-build/ruy/libruy_pack_avx512.a
${TFLITE_DIR}/_deps/fft2d-build/libfft2d_fftsg.a
${TFLITE_DIR}/_deps/fft2d-build/libfft2d_fftsg2d.a
${TFLITE_DIR}/_deps/clog-build/libclog.a
${TFLITE_DIR}/_deps/cpuinfo-build/libcpuinfo.a
${TFLITE_DIR}/libtensorflow-lite.a
dl
)
It worked on a windows machine but on Linux I'm getting error about
undefined reference to `ruy::ScopedSuppressDenormals::ScopedSuppressDenormals()'
[ 33%] Building CXX object CMakeFiles/main.dir/src/main.cpp.o
[ 66%] Building CXX object CMakeFiles/main.dir/src/ssd_mobilenet_tflite.cpp.o
[100%] Linking CXX executable main
/usr/bin/ld: ../tflite_build/libtensorflow-lite.a(interpreter.cc.o): in function `tflite::Interpreter::Invoke()':
interpreter.cc:(.text+0x3ff): undefined reference to `ruy::ScopedSuppressDenormals::ScopedSuppressDenormals()'
/usr/bin/ld: interpreter.cc:(.text+0x523): undefined reference to `ruy::ScopedSuppressDenormals::~ScopedSuppressDenormals()'
/usr/bin/ld: ../tflite_build/libtensorflow-lite.a(interpreter.cc.o): in function `tflite::Interpreter::Invoke() [clone .cold]':
interpreter.cc:(.text.unlikely+0x5c): undefined reference to `ruy::ScopedSuppressDenormals::~ScopedSuppressDenormals()'
/usr/bin/ld: CMakeFiles/main.dir/src/ssd_mobilenet_tflite.cpp.o: in function `SSD_MOBILENET::loadModel(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
ssd_mobilenet_tflite.cpp:(.text+0x2b2): undefined reference to `tflite::ops::builtin::BuiltinOpResolver::BuiltinOpResolver()'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/main.dir/build.make:231: main] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I don't want to add the libs as "add_subdirectory" as it was given in the guide because it will compile every time I create a new project
So how can I solve the undefined reference to `ruy::ScopedSuppressDenormals::ScopedSuppressDenormals()'
UPDATE:
if I change the CMakeLists.txt to :
cmake_minimum_required(VERSION 3.0.0)
project(main)
set(TENSORFLOW_SRC_DIR "test_cpp/tensorflow_src")
set(TFLITE_DIR "test_cpp/tflite_build")
find_package(OpenCV 4 REQUIRED)
find_package(TFLITE)
file(GLOB SOURCE_FILES src/*.cpp src/*.h )
message(${SOURCE_FILES})
add_executable(${CMAKE_PROJECT_NAME} ${SOURCE_FILES})
include_directories(
${TENSORFLOW_SRC_DIR}
)
target_link_libraries(
${CMAKE_PROJECT_NAME} PUBLIC
${OpenCV_LIBS}
${TFLITE_LIBS}
)
I take this error
from test_cpp/src/main.cpp:1:
test_cpp/tensorflow_src/tensorflow/lite/interpreter_builder.h:26:10: fatal error: flatbuffers/flatbuffers.h: No such file or directory
26 | #include "flatbuffers/flatbuffers.h" // from #flatbuffers
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~
compilation terminated.
make[2]: *** [CMakeFiles/main.dir/build.make:63: CMakeFiles/main.dir/src/main.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/main.dir/all] Error 2
make: *** [Makefile:84: all] Error 2

I found this repo that contains all needed libs for the project https://github.com/muhammedakyuzlu/tflite-cpp-package

I believe that the problem you have is the ordering of the libs.
Try putting the libtensorflow-lite.a before the ruy-*.a in your setup.

There is another trick that could help you getting started.
Just add this to your CMake File:
include(FetchContent)
FetchContent_Declare(
tensorflow
GIT_REPOSITORY https://github.com/tensorflow/tensorflow.git
GIT_PROGRESS TRUE
GIT_SHALLOW TRUE
GIT_TAG v2.10.0
SOURCE_SUBDIR tensorflow/lite
)
FetchContent_MakeAvailable(tensorflow)
and link to the library:
target_link_libraries(${PROJECT_NAME} PRIVATE
tensorflow-lite
)
This is downloading all dependencies automatically and should make you ready to go.

Related

Linking problems with yaml-cpp

I am trying to incorporate yaml-cpp into my project.
I have a "Demes" class where I need to parse a YAML file.
This is the relevant method in Demes.cpp:
#include "Demes.hpp"
void Demes::parse(const std::string& fileName)
{
YAML::Node test = YAML::LoadFile(fileName);
}
where Demes.hpp includes the yaml-cpp headers and declares the 'parse' method.
Building with make -VERBOSE=1 (as suggested by #Tsyvarev) gives:
[100%] Linking CXX executable momentspp
cd /home/gvbarroso/Devel/momentspp/build/src && /usr/bin/cmake -E cmake_link_script CMakeFiles/momentspp.dir/link.txt --verbose=1
/usr/bin/c++ -std=c++20 -Weffc++ -Wshadow -Wall -Wextra -ffast-math -O3 -march=native CMakeFiles/momentspp.dir/Log.cpp.o CMakeFiles/momentspp.dir/PolymorphismData.cpp.o CMakeFiles/momentspp.dir/SumStatsLibrary.cpp.o CMakeFiles/momentspp.dir/Drift.cpp.o CMakeFiles/momentspp.dir/Migration.cpp.o CMakeFiles/momentspp.dir/Mutation.cpp.o CMakeFiles/momentspp.dir/Recombination.cpp.o CMakeFiles/momentspp.dir/Epoch.cpp.o CMakeFiles/momentspp.dir/Model.cpp.o CMakeFiles/momentspp.dir/OptimizationWrapper.cpp.o CMakeFiles/momentspp.dir/Demes.cpp.o CMakeFiles/momentspp.dir/main.cpp.o -o momentspp -Wl,-rpath,/home/gvbarroso/.local/lib: /home/gvbarroso/.local/lib/libbpp-phyl3.so.1.0.0 /usr/lib/x86_64-linux-gnu/libboost_iostreams.so.1.74.0 /home/gvbarroso/.local/lib/libbpp-seq3.so.1.0.0 /home/gvbarroso/.local/lib/libbpp-core3.so.1.0.0
/usr/bin/ld: CMakeFiles/momentspp.dir/Demes.cpp.o: in function Demes::parse(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)': Demes.cpp:(.text+0x4c): undefined reference to YAML::LoadFile(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)'
/usr/bin/ld: Demes.cpp:(.text+0x12c): undefined reference to `YAML::operator<<(std::ostream&, YAML::Node const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [src/CMakeFiles/momentspp.dir/build.make:277: src/momentspp] Error 1
make[2]: Leaving directory '/home/gvbarroso/Devel/momentspp/build'
make[1]: *** [CMakeFiles/Makefile2:125: src/CMakeFiles/momentspp.dir/all] Error 2
make[1]: Leaving directory '/home/gvbarroso/Devel/momentspp/build'
make: *** [Makefile:156: all] Error 2
I am using CMake to build my project, but I am still fairly unfamiliar with it.
EDIT: I forgot to mention that I have two CMakeLists.txt files, one inside src and the other inside the external build.
The start of my CMakeLists.txt file in the external build is:
cmake_minimum_required (VERSION 3.5.0)
project (momentspp CXX)
SET(CMAKE_CXX_FLAGS "-std=c++20 -Weffc++ -Wshadow -Wall -Wextra -ffast-math -O3 -march=native")
And the part of it where I look for yaml-cpp is:
FIND_PACKAGE(yaml-cpp REQUIRED)
IF(yaml-cpp_FOUND)
INCLUDE_DIRECTORIES(${yaml-cpp_INCLUDE_DIRS})
SET(LIBS {yaml-cpp_LIBRARIES})
MESSAGE("-- yaml-cpp libraries found here:")
MESSAGE(" includes: ${yaml-cpp_INCLUDE_DIRS}")
ENDIF()
My full CMakeLists.txt file inside src is:
SET(momentspp_CPP
Log.cpp
PolymorphismData.cpp
SumStatsLibrary.cpp
Drift.cpp
Migration.cpp
Mutation.cpp
Recombination.cpp
Epoch.cpp
Model.cpp
OptimizationWrapper.cpp
Demes.cpp
main.cpp
)
ADD_EXECUTABLE (momentspp ${momentspp_CPP})
SET(momentspp-targets momentspp)
FOREACH (target ${momentspp-targets})
TARGET_LINK_LIBRARIES(${target} ${BPP_LIBS_SHARED} ${BOOST_LIBS_SHARED} ${EIGEN3_LIBS_SHARED} ${yaml-cpp_LIBS_SHARED})
TARGET_LINK_LIBRARIES (${target} ${LIBS})
ENDFOREACH (target)
INSTALL(TARGETS ${momentspp-targets} DESTINATION ${CMAKE_INSTALL_BINDIR})
and this was working prior to the inclusion of yaml-cpp.
This feels like a rather complicated CMake set-up, but I am copying and editing it from a previous project where someone else helped me with it.
How can I fix the linking issue?
I tried looking similar questions around here, but couldn't get their solutions to work for me (apparently those people where not using CMake to build their projects).
Thank you,
Gustavo

up dating linker ld on Mac

I am trying to compile an example pytroch c++ project using instructions at:
https://pytorch.org/cppdocs/installing.html
When i reach the point of calling:
cmake -DCMAKE_PREFIX_PATH=/absolute/path/to/libtorch ..
cmake --build . --config Release
I get an error:
ld: unknown option: --no-as-needed
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [BLOCK_READER] Error 1
make[1]: *** [CMakeFiles/BLOCK_READER.dir/all] Error 2
make: *** [all] Error 2
It looks like the issue is that the linker doesnt understand the option --no-as-needed.
After some research it looks like newer version of the linker support this option, but not the version i have.
So my question is how do i update my linker to a version that support this option?
I am on a Apple M1 (BigSur)..
It is my understanding that ld is part of gcc so can i just update gcc?
Btw here is my cmake CMakeLists.txt
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
project(block_reader)
# Setup Torch
set(CMAKE_CXX_COMPILER "/usr/bin/g++" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_LINKER "/usr/bin/ld" CACHE STRING "" FORCE)
set(CMAKE_PREFIX_PATH /Users/username/Downloads/libtorch)
find_package(Torch REQUIRED)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${TORCH_CXX_FLAGS}")
# Setup Torch
add_executable(BLOCK_READER main.cpp)
target_include_directories(BLOCK_READER PRIVATE /src)
set_property(TARGET BLOCK_READER PROPERTY CXX_STANDARD 14)
target_link_libraries(BLOCK_READER "${TORCH_LIBRARIES}")

SDL2 Cmake undefined reference to `WinMain#16' on Win10 MinGW

I am trying to set up SDL in CLion with MinGW, but I get errors after errors, and I just can't imagine where do I go wrong. Now I have this Cmake code, and I get the error undefined reference to 'WinMain#16' . I suppose I forget to link something, probably SDLmain, but I think I wrote it there in the target_link_libraries(). I would greatly appreciate some help to make it work.
cmake_minimum_required(VERSION 3.15)
project(SDL_project)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_project main.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY} -lmingw32 -lSDL2main -lSDL2 -mwindows)
The error message:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
Scanning dependencies of target SDL_project
[ 50%] Building CXX object CMakeFiles/SDL_project.dir/main.cpp.obj
[100%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/lib/../lib/libmingw32.a(lib32_libmingw32_a-crt0_c.o): in function `main':
D:/mingwbuild/mingw-w64-crt-git/src/mingw-w64/mingw-w64-crt/crt/crt0_c.c:18: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
Makefile:117: recipe for target 'SDL_project' failed
And I also made a cmake folder in the project folder, for the SDL find, but I am not sure if it is necessary.
set(FIND_SDL2_PATHS D:/SDL2/SDL2-devel-2.0.12-mingw/SDL2-2.0.12/x86_64-w64-mingw32)
find_path(SDL2_INCLUDE_DIR SDL2
PATH_SUFFIXES include
PATHS ${FIND_SDL2_PATHS})
find_library(SDL2_LIBRARY
NAMES SDL2
PATH_SUFFIXES lib
PATHS ${FIND_SDL2_PATHS})
The solution was to add a new line to the end: add_definitions(-DSDL_MAIN_HANDLED).
Now the CMakeLists.txt code looks like this:
cmake_minimum_required(VERSION 3.15)
project(SDL_project)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(SDL2 REQUIRED)
include_directories(${SDL2_INCLUDE_DIR})
add_executable(SDL_project main.cpp)
target_link_libraries(${PROJECT_NAME} ${SDL2_LIBRARY} -lmingw32 -mwindows)
add_definitions(-DSDL_MAIN_HANDLED)
And in the project folder in a separete folder called cmake there is the FindSDL2.cmake file, to locate SDL2:
set(FIND_SDL2_PATHS D:/SDL2/SDL2-devel-2.0.12-mingw/SDL2-2.0.12/x86_64-w64-mingw32)
find_path(SDL2_INCLUDE_DIR SDL2
PATH_SUFFIXES include
PATHS ${FIND_SDL2_PATHS})
find_library(SDL2_LIBRARY
NAMES SDL2 SDL2main
PATH_SUFFIXES lib
PATHS ${FIND_SDL2_PATHS})
From the CMakeLists.txt I removed -lSDL2 and -lSDL2main flags, because they resulted in errors:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2
A:/MSYS2/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: cannot find -lSDL2main
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
Also, it is important to use 64-bit mingw-w64, because mingw-w32 also gives errors, like:
====================[ Build | SDL_project | Debug ]=============================
"A:\CLion 2019.2.5\bin\cmake\win\bin\cmake.exe" --build D:\SDL_project\cmake-build-debug --target SDL_project -- -j 4
[ 50%] Linking CXX executable SDL_project.exe
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj):D:/SDL_project/main.cpp:17: undefined reference to `SDL_Init'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: CMakeFiles\SDL_project.dir/objects.a(main.cpp.obj): in function `main':
D:/SDL_project/main.cpp:19: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:24: undefined reference to `SDL_CreateWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:27: undefined reference to `SDL_GetError'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:32: undefined reference to `SDL_GetWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_MapRGB'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:35: undefined reference to `SDL_FillRect'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:38: undefined reference to `SDL_UpdateWindowSurface'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:41: undefined reference to `SDL_Delay'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:45: undefined reference to `SDL_DestroyWindow'
A:/MSYS2/mingw32/bin/../lib/gcc/i686-w64-mingw32/9.3.0/../../../../i686-w64-mingw32/bin/ld.exe: D:/SDL_project/main.cpp:48: undefined reference to `SDL_Quit'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\SDL_project.dir\build.make:86: recipe for target 'SDL_project.exe' failed
CMakeFiles\Makefile2:74: recipe for target 'CMakeFiles/SDL_project.dir/all' failed
CMakeFiles\Makefile2:81: recipe for target 'CMakeFiles/SDL_project.dir/rule' failed
Makefile:117: recipe for target 'SDL_project' failed
mingw32-make.exe[3]: *** [SDL_project.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles/SDL_project.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles/SDL_project.dir/rule] Error 2
mingw32-make.exe: *** [SDL_project] Error 2
And, the SDL.dll file from the binary folder has to be added to the compiler, to avoid exit code -1073741515 (0xC0000135). It can be done by setting a path to the folder, or by copying the SDL.dll from the SDL\bin folder to the mingw64\bin folder.

How to link Qt 5.9 in ROS?

The following CMakeList produces errors which have been listed below:
cmake_minimum_required(VERSION 2.8.12)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
set(CMAKE_AUTORCC ON)
project(draw_on_images)
find_package(catkin REQUIRED COMPONENTS
roscpp
rospy
std_msgs
)
find_package(OpenCV 2.4.8 REQUIRED)
find_package(Qt5 COMPONENTS Core Qml Quick REQUIRED)
qt5_add_resources(RESOURCES qml.qrc)
catkin_package(
CATKIN_DEPENDS roscpp rospy std_msgs
)
include_directories(
${catkin_INCLUDE_DIRS}
)
add_executable(${PROJECT_NAME} ${RESOURCES} src/main.cpp src/draw_on_qimage.cpp src/provide_qpixmap_to_qml.cpp )
qt5_use_modules(${PROJECT_NAME} Core Qml Quick)
target_link_libraries(${PROJECT_NAME} Qt5::Core Qt5::Qml Qt5::Quick ${OpenCV_LIBS})
The way I run it:
catkin_make -DCMAKE_PREFIX_PATH="/opt/Qt5.9.1/5.9.1/gcc_64/lib/" -DWITH_QT:BOOL=ON
Yes I have sourced it.
Errors:
Base path: /home/.../ros_workspace_test
Source space: /home/.../ros_workspace_test/src
Build space: /home/.../ros_workspace_test/build
Devel space: /home/.../ros_workspace_test/devel
Install space: /home/.../ros_workspace_test/install
####
#### Running command: "cmake /home/.../ros_workspace_test/src -DCMAKE_PREFIX_PATH=/opt/Qt5.9.1/5.9.1/gcc_64/lib/ -DWITH_QT:BOOL=ON -DCATKIN_DEVEL_PREFIX=/home/.../ros_workspace_test/devel -DCMAKE_INSTALL_PREFIX=/home/.../ros_workspace_test/install -G Unix Makefiles" in "/home/.../ros_workspace_test/build"
####
-- Using CATKIN_DEVEL_PREFIX: /home/.../ros_workspace_test/devel
-- Using CMAKE_PREFIX_PATH: /opt/Qt5.9.1/5.9.1/gcc_64/lib/
-- Using PYTHON_EXECUTABLE: /usr/bin/python
-- Using Debian Python package layout
-- Using empy: /usr/bin/empy
-- Using CATKIN_ENABLE_TESTING: ON
-- Call enable_testing()
-- Using CATKIN_TEST_RESULTS_DIR: /home/.../ros_workspace_test/build/test_results
-- Found gtest sources under '/usr/src/gtest': gtests will be built
-- Using Python nosetests: /usr/bin/nosetests-2.7
-- catkin 0.6.19
-- BUILD_SHARED_LIBS is on
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- ~~ traversing 1 packages in topological order:
-- ~~ - draw_on_images
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-- +++ processing catkin package: 'draw_on_images'
-- ==> add_subdirectory(draw_on_images)
-- Configuring done
-- Generating done
CMake Warning:
Manually-specified variables were not used by the project:
WITH_QT
-- Build files have been written to: /home/.../ros_workspace_test/build
####
#### Running command: "make -j4 -l4" in "/home/.../ros_workspace_test/build"
####
[ 14%] Automoc for target draw_on_images
[ 14%] Built target draw_on_images_automoc
Linking CXX executable /home/.../ros_workspace_test/devel/lib/draw_on_images/draw_on_images
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `int qmlRegisterType<Draw_on_qimage>(char const*, int, int, char const*)':
main.cpp:(.text._Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_[_Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_]+0x38): undefined reference to `Draw_on_qimage::staticMetaObject'
main.cpp:(.text._Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_[_Z15qmlRegisterTypeI14Draw_on_qimageEiPKciiS2_]+0x1b6): undefined reference to `Draw_on_qimage::staticMetaObject'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `Draw_on_qimage::~Draw_on_qimage()':
main.cpp:(.text._ZN14Draw_on_qimageD2Ev[_ZN14Draw_on_qimageD5Ev]+0x13): undefined reference to `vtable for Draw_on_qimage'
main.cpp:(.text._ZN14Draw_on_qimageD2Ev[_ZN14Draw_on_qimageD5Ev]+0x25): undefined reference to `vtable for Draw_on_qimage'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `QMetaTypeIdQObject<Draw_on_qimage*, true>::qt_metatype_id()':
main.cpp:(.text._ZN18QMetaTypeIdQObjectIP14Draw_on_qimageLb1EE14qt_metatype_idEv[_ZN18QMetaTypeIdQObjectIP14Draw_on_qimageLb1EE14qt_metatype_idEv]+0x2c): undefined reference to `Draw_on_qimage::staticMetaObject'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `QtPrivate::MetaObjectForType<Draw_on_qimage*, true>::value()':
main.cpp:(.text._ZN9QtPrivate17MetaObjectForTypeIP14Draw_on_qimageLb1EE5valueEv[_ZN9QtPrivate17MetaObjectForTypeIP14Draw_on_qimageLb1EE5valueEv]+0x7): undefined reference to `Draw_on_qimage::staticMetaObject'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x10): undefined reference to `Draw_on_qimage::metaObject() const'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x18): undefined reference to `Draw_on_qimage::qt_metacast(char const*)'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTVN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x20): undefined reference to `Draw_on_qimage::qt_metacall(QMetaObject::Call, int, void**)'
CMakeFiles/draw_on_images.dir/src/main.cpp.o:(.data.rel.ro._ZTIN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE[_ZTIN11QQmlPrivate11QQmlElementI14Draw_on_qimageEE]+0x10): undefined reference to `typeinfo for Draw_on_qimage'
CMakeFiles/draw_on_images.dir/src/main.cpp.o: In function `Provide_QPixmap_to_QML::~Provide_QPixmap_to_QML()':
main.cpp:(.text._ZN22Provide_QPixmap_to_QMLD2Ev[_ZN22Provide_QPixmap_to_QMLD5Ev]+0x13): undefined reference to `vtable for Provide_QPixmap_to_QML'
main.cpp:(.text._ZN22Provide_QPixmap_to_QMLD2Ev[_ZN22Provide_QPixmap_to_QMLD5Ev]+0x25): undefined reference to `vtable for Provide_QPixmap_to_QML'
CMakeFiles/draw_on_images.dir/src/draw_on_qimage.cpp.o: In function `Draw_on_qimage::Draw_on_qimage(QQuickPaintedItem*)':
draw_on_qimage.cpp:(.text+0x4b): undefined reference to `vtable for Draw_on_qimage'
draw_on_qimage.cpp:(.text+0x5d): undefined reference to `vtable for Draw_on_qimage'
CMakeFiles/draw_on_images.dir/src/provide_qpixmap_to_qml.cpp.o: In function `Provide_QPixmap_to_QML::Provide_QPixmap_to_QML()':
provide_qpixmap_to_qml.cpp:(.text+0x52): undefined reference to `vtable for Provide_QPixmap_to_QML'
provide_qpixmap_to_qml.cpp:(.text+0x64): undefined reference to `vtable for Provide_QPixmap_to_QML'
collect2: error: ld returned 1 exit status
make[2]: *** [/home/.../ros_workspace_test/devel/lib/draw_on_images/draw_on_images] Error 1
make[1]: *** [draw_on_images/CMakeFiles/draw_on_images.dir/all] Error 2
make: *** [all] Error 2
Invoking "make -j4 -l4" failed
How to link Qt 5.9 in ROS?
I am using Ubuntu 14.
Solution was to add the header files to add_executable in CMakelists.txt:
add_executable(${PROJECT_NAME} ${RESOURCES}
src/main.cpp
include/draw_on_images/draw_on_qimage.h src/draw_on_qimage.cpp
include/draw_on_images/provide_qpixmap_to_qml.h src/provide_qpixmap_to_qml.cpp)
My guess is that CMake's AUTOMOC fails in this case. See the documentation here https://cmake.org/cmake/help/v3.0/prop_tgt/AUTOMOC.html
It asserts, that the header file of your class Draw_on_qimage is similarly named as the source file and is locatable by CMake.
You can avoid AUTOMOC by manually running moc on your header for your QObject-derived class like this
QT5_WRAP_CPP(MOC_SOURCES <path of header file of your class Draw_on_qimage>)
add_executable(${PROJECT_NAME} ${RESOURCES} src/main.cpp src/draw_on_qimage.cpp ${MOC_SOURCES} src/provide_qpixmap_to_qml.cpp

cmake on windows. Link libraries in folder

Disclaimer: I'm new to cmake so i have no idea what I'm doing.
All guides and tutorials i find seem to think I'm running a 20 man team that needs to work together.
All I'm trying to do is put all my libraries in an include folder and lib folder.
In visual studio and code::blocks i just set up the linker in the IDE, but cmake is a little hard for me to wrap my head around.
I've set include and link directories with
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
Then i try to
target_link_libraries(${PROJECT_NAME} jsoncpp)
This however produces undefined references. Is there a simple way to add and link libraries without me having to google and add a cmake module for each one?
Edit:
attempts:
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} libjsoncpp)
error: cannot find -llibjsoncpp
target_link_libraries(${PROJECT_NAME} jsoncpp)
error: undefined reference
target_link_libraries(${PROJECT_NAME} C:/Repos/prosjekt-bad-racoon/lib/libjsoncpp.a)
error: undefined reference
target_link_libraries(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/lib/libjsoncpp.a)
error: undefined reference
Edit:
Full cmake file
cmake_minimum_required(VERSION 3.8)
project(prosjekt_bad_racoon)
set(CMAKE_CXX_STANDARD 11)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
set(SOURCE_FILES main.cpp Core.cpp Core.h Collider.cpp Collider.h Player.cpp Player.h Texture.cpp Texture.h AudioController.cpp AudioController.h)
add_executable(prosjekt_bad_racoon ${SOURCE_FILES})
set(EXECUTABLE_NAME ${PROJECT_NAME})
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake_modules" ${CMAKE_MODULE_PATH})
target_link_libraries(${PROJECT_NAME} libjsoncpp.a)
find_package(SFML 2 REQUIRED system window graphics network audio)
if(SFML_FOUND)
include_directories(${SFML_INCLUDE_DIR})
target_link_libraries(${EXECUTABLE_NAME} ${SFML_LIBRARIES})
link_directories("${PROJECT_SOURCE_DIR}")
endif()
"C:\Program Files\JetBrains\CLion 2017.2.2\bin\cmake\bin\cmake.exe" --build C:\Repos\prosjekt-bad-racoon\cmake-build-debug --target prosjekt_bad_racoon -- -j 2
[ 14%] Linking CXX executable prosjekt_bad_racoon.exe
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreC2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:3: undefined reference to `Json::Value::Value(char const*)'
CMakeFiles\prosjekt_bad_racoon.dir/objects.a(Core.cpp.obj): In function `ZN4CoreD2Ev':
C:/Repos/prosjekt-bad-racoon/Core.cpp:8: undefined reference to `Json::Value::~Value()'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\prosjekt_bad_racoon.dir\build.make:236: recipe for target 'prosjekt_bad_racoon.exe' failed
mingw32-make.exe[3]: *** [prosjekt_bad_racoon.exe] Error 1
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/all' failed
mingw32-make.exe[2]: *** [CMakeFiles/prosjekt_bad_racoon.dir/all] Error 2
CMakeFiles\Makefile2:78: recipe for target 'CMakeFiles/prosjekt_bad_racoon.dir/rule' failed
mingw32-make.exe[1]: *** [CMakeFiles/prosjekt_bad_racoon.dir/rule] Error 2
Makefile:117: recipe for target 'prosjekt_bad_racoon' failed
mingw32-make.exe: *** [prosjekt_bad_racoon] Error 2