Can't compile MagickWand using CMake - c++

I couldn't compile my program which uses MagickWand with CMake. I'm getting this error:
[ 33%] Linking CXX executable bin/ScreenRecorder
/usr/bin/ld: CMakeFiles/ScreenRecorder.dir/source/main.cpp.o: in function `std::thread::thread<void (&)(), , void>(void (&)())':
main.cpp:(.text._ZNSt6threadC2IRFvvEJEvEEOT_DpOT0_[_ZNSt6threadC5IRFvvEJEvEEOT_DpOT0_]+0x20): undefined reference to `pthread_create'
/usr/bin/ld: CMakeFiles/ScreenRecorder.dir/source/screenshot.cpp.o: in function `printScrn(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
screenshot.cpp:(.text+0xd): undefined reference to `MagickWandGenesis'
/usr/bin/ld: screenshot.cpp:(.text+0x1a): undefined reference to `NewMagickWand'
/usr/bin/ld: screenshot.cpp:(.text+0x31): undefined reference to `MagickReadImage'
/usr/bin/ld: screenshot.cpp:(.text+0x4f): undefined reference to `MagickWriteImage'
/usr/bin/ld: screenshot.cpp:(.text+0x62): undefined reference to `DestroyMagickWand'
/usr/bin/ld: screenshot.cpp:(.text+0x6b): undefined reference to `MagickWandTerminus'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/ScreenRecorder.dir/build.make:99: bin/ScreenRecorder] Error 1
make[1]: *** [CMakeFiles/Makefile2:73: CMakeFiles/ScreenRecorder.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.0)
project(ScreenRecorder)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14 -Wall")
set(source_dir "${PROJECT_SOURCE_DIR}/source/")
set(EXECUTABLE_OUTPUT_PATH "${PROJECT_SOURCE_DIR}/bin/")
find_package(ImageMagick REQUIRED)
include_directories(
"/usr/include/ImageMagick-6/"
)
link_libraries(
"/usr/lib/x86_64-linux-gnu/"
)
file (GLOB src_files "${source_dir}*.cpp")
add_executable(ScreenRecorder ${src_files})
I tried using variables such as ${ImageMagick_MagickWand_INCLUDE_DIR} and ${ImageMagick_MagickWand_LIBS_DIR} but it says that "wand/MagickWand.h" not found.
Thanks for your answers.

I was able to resolve a similar issue by enclosing the auto-generated variables in quotes:
INCLUDE_DIRECTORIES(
"${ImageMagick_INCLUDE_DIRS}"
)
TARGET_LINK_LIBRARIES(MyProject
"${ImageMagick_LIBRARIES}"
)
The reason that this resolved the issue is that the variables are CMake lists.
Without quotes, they are concatenated to a long string:
MESSAGE(${ImageMagick_INCLUDE_DIRS})
MESSAGE(${ImageMagick_LIBRARIES})
->
1> [CMake] /usr/include/ImageMagick-6/usr/include/x86_64-linux-gnu/ImageMagick-6
1> [CMake] /usr/lib/x86_64-linux-gnu/libMagickWand-6.Q16.so
However, with quotes, the different elements of the list are separated using a semicolon:
MESSAGE("${ImageMagick_INCLUDE_DIRS}")
MESSAGE("${ImageMagick_LIBRARIES}")
->
1> [CMake] /usr/include/ImageMagick-6;/usr/include/x86_64-linux-gnu/ImageMagick-6
1> [CMake] /usr/lib/x86_64-linux-gnu/libMagickWand-6.Q16.so

Related

Undefined reference when using OpenCV and CMake in VSCode

Im trying to use OpenCV in VSCode and followed this videos instructions: https://www.youtube.com/watch?v=m9HBM1m_EMU&t=3s&ab_channel=NicolaiNielsen-ComputerVision%26AI
Difference from the video is im using mingw compiler and im unable to get the msvc compiler (Not the issue though).
main.cpp:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
int main()
{
std::cout << "aa" << std::endl;
std::string img = "lenna.jpg";
Mat srcImage = imread(img);
if (!srcImage.data) {
return 1;
}
imshow("srcImage", srcImage);
waitKey(0);
return 0;
}
CMakeLists.txt:
cmake_minimum_required(VERSION 3.0.0)
project(OpenCV_Test VERSION 0.1.0)
include(CTest)
enable_testing()
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(OpenCV_Test main.cpp)
target_link_libraries(OpenCV_Test ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Im able to get it to compile and output "aa" if i remove everything with OpenCV, but adding it gives me this compile error:
[main] Building folder: OpenCVTest
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/Chris/Desktop/OpenCVTest/build --config Debug --target all -j 10 --
[build] Consolidate compiler generated dependencies of target OpenCV_Test
[build] [ 50%] Building CXX object CMakeFiles/OpenCV_Test.dir/main.cpp.obj
[build] [100%] Linking CXX executable OpenCV_Test.exe
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/OpenCV_Test.dir/objects.a(main.cpp.obj):C:/Users/Chris/Desktop/OpenCVTest/main.cpp:12: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: CMakeFiles/OpenCV_Test.dir/objects.a(main.cpp.obj): in function `main':
[build] C:/Users/Chris/Desktop/OpenCVTest/main.cpp:16: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Chris/Desktop/OpenCVTest/main.cpp:17: undefined reference to `cv::waitKey(int)'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Chris/Desktop/OpenCVTest/main.cpp:19: undefined reference to `cv::Mat::~Mat()'
[build] C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/Chris/Desktop/OpenCVTest/main.cpp:19: undefined reference to `cv::Mat::~Mat()'
[build] collect2.exe: error: ld returned 1 exit status
[build] make[2]: *** [CMakeFiles/OpenCV_Test.dir/build.make:117: OpenCV_Test.exe] Error 1
[build] make[1]: *** [CMakeFiles/Makefile2:839: CMakeFiles/OpenCV_Test.dir/all] Error 2
[build] make: *** [Makefile:121: all] Error 2
[build] Build finished with exit code 2

linked library is valid in CMakeLists, but doesn't link at compile time

I'm just starting around with messing around with vulkan, and GLFW, but when I try to compile a test program, it gives me a bunch of linker errors:
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Init()':
loops.cpp:(.text+0xd): undefined reference to `glfwInit'
/usr/bin/ld: loops.cpp:(.text+0x1c): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x2b): undefined reference to `glfwWindowHint'
/usr/bin/ld: loops.cpp:(.text+0x4f): undefined reference to `glfwCreateWindow'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::Update()':
loops.cpp:(.text+0xa3): undefined reference to `glfwPollEvents'
/usr/bin/ld: CMakeFiles/vulkan_test.dir/loops.cpp.o: in function `Loops::DeInit()':
loops.cpp:(.text+0xcd): undefined reference to `glfwDestroyWindow'
/usr/bin/ld: loops.cpp:(.text+0xd2): undefined reference to `glfwTerminate'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/vulkan_test.dir/build.make:113: vulkan_test] Error 1
make[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/vulkan_test.dir/all] Error 2
make: *** [Makefile:91: all] Error 2
This is my CMakeLists.txt:
cmake_minimum_required(VERSION 3.22)
project(vulkan_test)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_MODULE_PATH /home/headass/CMake_Modules/)
find_package(GLFW REQUIRED)
find_package(Vulkan REQUIRED)
include_directories(${GLFW_INCLUDE_DIRS} ${VULKAN_INCLUDE_DIRS})
add_executable(vulkan_test main.cpp loops.cpp)
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
Any idea why this is happening? I have both vulkan and GLFW installed, libglfw.so is in my /usr/lib/ directory, and clangd doesn't see anything wrong with it, but it still fails to link properly.
And yes, I HAVE tried googling this, to no avail.
Changing:
target_link_libraries(vulkan_test ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
to:
target_link_libraries(vulkan_test glfw ${GLFW_LIBRARIES} ${VULKAN_LIBRARIES})
worked flawlessly

Cmake undefined reference to 'cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'

I created a small program to test OpenCV C++ with Cmake and visual studio code but the build failed because of undefined functions and I don't know why Here is my code and my CmakeList.txt
I have already set the variable paths like this :
C:\opencv\build\x64\vc15\bin
C:\opencv\build\x64\vc15\lib
#include <stdio.h>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
int main()
{
Mat img = imread("lena.jpg");
imshow("IMAGE", img);
waitKey(0);
return 0;
}
cmake_minimum_required(VERSION 3.0.0)
project(TP1 VERSION 0.1.0)
include(CTest)
enable_testing()
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(TP1 main.cpp)
target_link_libraries(TP1 ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
Build output using cmake tools in vscode
[main] Building folder: TP1
[build] Starting build
[proc] Executing command: "C:\Program Files\CMake\bin\cmake.EXE" --build c:/Users/amine/Desktop/My_Studies/OpenCV/TP1/build --config Debug --target all -- -j 10
[build] Consolidate compiler generated dependencies of target TP1
[build] [ 50%] Building CXX object CMakeFiles/TP1.dir/main.cpp.obj
[build] [100%] Linking CXX executable TP1.exe
[build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `main':
[build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:9: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:10: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/amine/Desktop/My_Studies/OpenCV/TP1/main.cpp:11: undefined reference to `cv::waitKey(int)'
[build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `cv::Mat::~Mat()':
[build] C:/opencv/build/include/opencv2/core/mat.inl.hpp:751: undefined reference to `cv::fastFree(void*)'
[build] CMakeFiles\TP1.dir/objects.a(main.cpp.obj): In function `cv::Mat::release()':
[build] C:/opencv/build/include/opencv2/core/mat.inl.hpp:863: undefined reference to `cv::Mat::deallocate()'
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\TP1.dir\build.make:115: TP1.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:370: CMakeFiles/TP1.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:120: all] Error 2
[build] Build finished with exit code 2
Location of opencv
C://opencv
Location of my code sample
Desktop//My_studies//OpenCV//TP1

Can't link Boost 1.63.0 through CMake

what I'm trying to do is as you can guess from the title to link Boost libraries through CMake (I'm working with CLion to write cross platform code, so I have no other chance). I am sure I built everything correctly cause when I use it inside Visual Studio it works with no problem at all.
Here's my CMake code:
cmake_minimum_required(VERSION 3.7)
project(BoostHello)
set(BOOST_ROOT C:/boost_1.63.0)
find_package(BOOST 1.6.0 REQUIRED)
include_directories( ${Boost_INCLUDE_DIR} )
set(CMAKE_CXX_STANDARD 14)
set(SOURCE_FILES main.cpp)
add_executable(BoostHello ${SOURCE_FILES})
target_link_libraries( BoostHello ${Boost_LIBRARIES} )
And here are my compilation errors:
"C:\Program Files (x86)\JetBrains\CLion 2017.1\bin\cmake\bin\cmake.exe" --build C:\Users\Admin\CLionProjects\BoostHello\cmake-build-debug --target all -- -j 8
Scanning dependencies of target BoostHello
[ 50%] Building CXX object CMakeFiles/BoostHello.dir/main.cpp.obj
[100%] Linking CXX executable BoostHello.exe
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:6: undefined reference to `boost::filesystem::path::root_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:7: undefined reference to `boost::filesystem::path::relative_path() const'
C:/Users/Admin/CLionProjects/BoostHello/main.cpp:8: undefined reference to `boost::filesystem::path::filename() const'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `_static_initialization_and_destruction_0':
C:/boost_1.63.0/boost/system/error_code.hpp:221: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:222: undefined reference to `boost::system::generic_category()'
C:/boost_1.63.0/boost/system/error_code.hpp:223: undefined reference to `boost::system::system_category()'
CMakeFiles\BoostHello.dir/objects.a(main.cpp.obj): In function `ZN5boost10filesystem11path_traits7convertEPKwS3_RNSt7__cxx1112basic_stringIcSt1 1char_traitsIcESaIcEEE':
C:/boost_1.63.0/boost/filesystem/path.hpp:989: undefined reference to `boost::filesystem::path::codecvt()'
C:/boost_1.63.0/boost/filesystem/path.hpp:989: undefined reference to `boost::filesystem::path_traits::convert(wchar_t const*, wchar_t const*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&, std::codecvt<wchar_t, char, int> const&)'
collect2.exe: error: ld returned 1 exit status
CMakeFiles\BoostHello.dir\build.make:96: recipe for target 'BoostHello.exe' failed
mingw32-make.exe[2]: *** [BoostHello.exe] Error 1
mingw32-make.exe[1]: *** [CMakeFiles/BoostHello.dir/all] Error 2
CMakeFiles\Makefile2:66: recipe for target 'CMakeFiles/BoostHello.dir/all' failed
mingw32-make.exe: *** [all] Error 2
Makefile:82: recipe for target 'all' failed
And eventually here's the code I'm trying to compile:
#include <iostream>
#include <boost/filesystem.hpp>
int main(int argc, char** argv) {
boost::filesystem::path myPath = {L"C:/Users/Admin/ClionProjects/BoostHello"};
std::cout << "Root:\t" << myPath.root_path() << std::endl;
std::cout << "Relative:\t" << myPath.relative_path() << std::endl;
std::cout << "Filename:\t" << myPath.filename() << std::endl;
return 0;
}
What am I doing wrong?
I am compiling with MinGW.
Thanks in advance!
Replace
find_package(BOOST 1.6.0 REQUIRED)
by
find_package(Boost 1.63.0 REQUIRED filesystem system)
otherwise CMake doesn't know which boost library to link against.

MinGW/CMake Undefined Reference to ZLib

I have a project I want to build with CMake and compile with MinGW. The project uses Zlib. When I build with CMake I get no errors but then when I run MinGW Make it gives the following output:
C:\Projects\MultiMCBuild>C:\Qt\Tools\mingw492_32\bin\mingw32-make.exe
.
.
.
[ 50%] Linking CXX shared library ..\libMultiMC_logic.dll
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x1f6c): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x29e2): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2a6d): undefined reference to 'z_get_crc_table'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2ca7): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2f52): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x2f77): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3239): undefined reference to 'z_inflateInit2_'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3317): undefined reference to 'z_inflateEnd'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3626): undefined reference to 'z_crc32'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x371f): undefined reference to 'z_inflate'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x376a): undefined reference to 'z_crc32'
C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj):unzip.c:(.text+0x3a57): undefined reference to 'z_inflateEnd'
C:/Qt/Tools/mingw492_32/bin/../lib/gcc/i686-w64- mingw32/4.9.2/../../../../i686-w64-mingw32/bin/ld.exe: C:/Projects/MultiMCBuild/External/Install/QuaZIP/lib/libquazip.a(unzip.c.obj): bad reloc address 0x20 in section `.eh_frame'
collect2.exe: error: ld returned 1 exit status
logic\CMakeFiles\MultiMC_logic.dir\build.make:3186: recipe for target 'libMultiMC_logic.dll' failed
mingw32-make[2]: * * * [libMultiMC_logic.dll] Error 1
CMakeFiles\Makefile2:1806: recipe for target 'logic/CMakeFiles/MultiMC_logic.dir/all' failed
mingw32-make[1]: * * * [logic/CMakeFiles/MultiMC_logic.dir/all] Error 2
makefile:159: recipe for target 'all' failed
mingw32-make: * * * [all] Error 2
Anyone have a clue what I can do to fix this? I read that the code can't find the ZLib library, how do I link it?
EDIT here is my CMakeLists.txt. I got this from the Git project.
I just tried to generate a small example to reproduce your errors. I took my test file from the home page of zlib http://zlib.net/zpipe.c
My initial CMakeLists.txt was
cmake_minimum_required(VERSION 3.4)
project(zlib_test)
set(ZLIB_TEST_SOURCES zpipe.c)
add_executable(${PROJECT_NAME} ${ZLIB_TEST_SOURCES})
And I got the same errors
[ 50%] Building C object CMakeFiles/zlib_test.dir/zpipe.c.o
[100%] Linking C executable zlib_test
CMakeFiles/zlib_test.dir/zpipe.c.o: In function `def':
zpipe.c:(.text+0x65): undefined reference to `deflateInit_'
zpipe.c:(.text+0xcd): undefined reference to `deflateEnd'
zpipe.c:(.text+0x135): undefined reference to `deflate'
zpipe.c:(.text+0x1cf): undefined reference to `deflateEnd'
zpipe.c:(.text+0x25d): undefined reference to `deflateEnd'
CMakeFiles/zlib_test.dir/zpipe.c.o: In function `inf':
zpipe.c:(.text+0x2eb): undefined reference to `inflateInit_'
zpipe.c:(.text+0x353): undefined reference to `inflateEnd'
zpipe.c:(.text+0x3a4): undefined reference to `inflate'
zpipe.c:(.text+0x404): undefined reference to `inflateEnd'
zpipe.c:(.text+0x476): undefined reference to `inflateEnd'
zpipe.c:(.text+0x4a6): undefined reference to `inflateEnd'
collect2: error: ld returned 1 exit status
make[2]: *** [zlib_test] Error 1
make[1]: *** [CMakeFiles/zlib_test.dir/all] Error 2
make: *** [all] Error 2
After changing CMakeLists.txt to this form
cmake_minimum_required(VERSION 3.4)
project(zlib_test)
find_package(ZLIB REQUIRED)
if (ZLIB_FOUND)
include_directories(${ZLIB_INCLUDE_DIRS})
endif()
set(ZLIB_TEST_SOURCES zpipe.c)
add_executable(${PROJECT_NAME} ${ZLIB_TEST_SOURCES})
target_link_libraries(${PROJECT_NAME} ${ZLIB_LIBRARIES})
I could compile the program.
So your problem is: Where is ZLIB added in your CMakeLists.txt? At least, you need the line find_package(ZLIB REQUIRED).