Undefined reference when using OpenCV and CMake in VSCode - c++

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

Related

CMake build gives undefined reference to functions in OpenCV

I'm making a jump from python to C++ in vscode. My project utilizes OpenCV and I quickly ran into a lot of problems with trying to get my compiler (mingw-w64) and CMake to find and reference the OpenCV functions.
My test code looks like this. I know I have to change my file path, but when I do I get the same problem.
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
Mat image1,image2;
// Read the file
image1 = imread("C:\\Users\\arjun\\Desktop\\opencv-logo.jpg");
// Check for invalid input
if(! image1.data )
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
//Write the File
imwrite( "C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg",image1);
// Read the Writen File
image2 =imread("C:\\Users\\arjun\\Desktop\\opencv-logo-new.jpg");
namedWindow("Image1");
imshow("Image1",image1);
namedWindow("Image2");
imshow("Image2",image2);
waitKey(0);
}
My CMakeLists.txt looks like this
cmake_minimum_required(VERSION 3.0.0)
project(chessbot VERSION 0.1.0)
include(CTest)
enable_testing()
find_package( OpenCV REQUIRED)
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable(chessbot main.cpp)
target_link_libraries(chessbot ${OpenCV_LIBS})
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
When I use the CMake Build tool (which I believe is supposed to make my executable for me) I get a bunch of undefined references to opencv functions
uild] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:25: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:28: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:29: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:31: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:32: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::waitKey(int)'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:33: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:13: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:25: undefined reference to `cv::Mat::~Mat()'
[build] C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:10: undefined reference to `cv::Mat::~Mat()'
[build] CMakeFiles\chessbot.dir/objects.a(main.cpp.obj):C:/Users/tomma/Desktop/code stuff/c_files/testing/main.cpp:10: more undefined references to `cv::Mat::~Mat()' follow
[build] collect2.exe: error: ld returned 1 exit status
[build] mingw32-make.exe[2]: *** [CMakeFiles\chessbot.dir\build.make:115: chessbot.exe] Error 1
[build] mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:838: CMakeFiles/chessbot.dir/all] Error 2
[build] mingw32-make.exe: *** [Makefile:120: all] Error 2
[build] Build finished with exit code 2
There is another compile tool that doesn't return undefined references and seems to compile just fine, which leads me to believe that CMake cant find the target library for some reason. The learning curve is steep for me. Can someone explain to me the issue? Why cant I reference the OpenCV functions?

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 compile MagickWand using CMake

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

OpenCV undefined reference to

I'm using Ubuntu 16.04 and OpenCV 2.4.9 and my quite simple example programs are constantly failing to build. I'm building on CLion.
As I'm kinda new to OpenCV I have some issues to narrow down the problem. I tried two different programs.
Number 1 is this one:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main(int argc, char **argv) {
IplImage *img = cvLoadImage(argv[1]);
cvNamedWindow("Example1", CV_WINDOW_AUTOSIZE );
cvShowImage("Example1", img);
cvWaitKey(0);
cvReleaseImage(&img);
cvDestroyWindow("Example1" );
}
Which fails with:
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
CMakeFiles/untitled.dir/main.cpp.o: In function `main':
/home/computervision/CLionProjects/untitled/main.cpp:7: undefined reference to `cvLoadImage'
/home/computervision/CLionProjects/untitled/main.cpp:8: undefined reference to `cvNamedWindow'
/home/computervision/CLionProjects/untitled/main.cpp:9: undefined reference to `cvShowImage'
/home/computervision/CLionProjects/untitled/main.cpp:10: undefined reference to `cvWaitKey'
/home/computervision/CLionProjects/untitled/main.cpp:11: undefined reference to `cvReleaseImage'
/home/computervision/CLionProjects/untitled/main.cpp:12: undefined reference to `cvDestroyWindow'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled.dir/build.make:94: recipe for target 'untitled' failed
make[3]: *** [untitled] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/untitled.dir/all' failed
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/untitled.dir/rule' failed
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
Makefile:118: recipe for target 'untitled' failed
make: *** [untitled] Error 2
My 2nd one is this:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main( int argc, char** argv ) {
cv::Mat image;
image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR);
if(! image.data ) {
std::cout << "Could not open or find the image" << std::endl ;
return -1;
}
cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE );
cv::imshow( "Display window", image );
cv::waitKey(0);
return 0;
}
Which fails with:
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
CMakeFiles/untitled.dir/main.cpp.o: In function `main':
/home/computervision/CLionProjects/untitled/main.cpp:9: undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/computervision/CLionProjects/untitled/main.cpp:16: undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/home/computervision/CLionProjects/untitled/main.cpp:17: undefined reference to `cv::_InputArray::_InputArray(cv::Mat const&)'
/home/computervision/CLionProjects/untitled/main.cpp:17: undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/home/computervision/CLionProjects/untitled/main.cpp:19: undefined reference to `cv::waitKey(int)'
CMakeFiles/untitled.dir/main.cpp.o: In function `cv::Mat::~Mat()':
/usr/local/include/opencv2/core/mat.hpp:278: undefined reference to `cv::fastFree(void*)'
CMakeFiles/untitled.dir/main.cpp.o: In function `cv::Mat::operator=(cv::Mat const&)':
/usr/local/include/opencv2/core/mat.hpp:298: undefined reference to `cv::Mat::copySize(cv::Mat const&)'
CMakeFiles/untitled.dir/main.cpp.o: In function `cv::Mat::release()':
/usr/local/include/opencv2/core/mat.hpp:367: undefined reference to `cv::Mat::deallocate()'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled.dir/build.make:94: recipe for target 'untitled' failed
make[3]: *** [untitled] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/untitled.dir/all' failed
make[2]: *** [CMakeFiles/untitled.dir/all] Error 2
CMakeFiles/Makefile2:79: recipe for target 'CMakeFiles/untitled.dir/rule' failed
make[1]: *** [CMakeFiles/untitled.dir/rule] Error 2
Makefile:118: recipe for target 'untitled' failed
make: *** [untitled] Error 2
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
add_executable(untitled main.cpp)
I wonder what I could have done so wrong.
Most searches lead to wrong settings of the g++ command, but I assume CLion knows what it's doing (also I tried with the command too, same errors).
For instance this here: openCV error : undefined reference to `cvLoadImage' Ubuntu
I assume highgui is not linked properly. Which might be the case, my output of:
computervision#computervision-VirtualBox:~/ex$ pkg-config opencv --libs
/usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_flann.so /usr/local/lib/libopencv_ml.so
looks kinda poor, I assume I made a mistake during installation (just which?).
Another error might be the CMakeLists.txt. Here it was recommended to change it. But the line
LIBRARIES += glog gflags protobuf leveldb snappy
causes an error.
Executing
export CMAKE_CXX_FLAGS=`pkg-config opencv --cflags --libs`
before starting CLion doesn't make any difference either.
EDIT:
Using cmake:
computervision#computervision-VirtualBox:~/CLionProjects/untitled$ cmake .
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
OpenCV_LIBS: opencv_mlopencv_flannopencv_core
-- Configuring done
-- Generating done
-- Build files have been written to: /home/computervision/CLionProjects/untitled
computervision#computervision-VirtualBox:~/CLionProjects/untitled$ g++ -o untitled main.cpp `pkg-config opencv --cflags --libs`
/tmp/ccdAr2hr.o: In function `main':
main.cpp:(.text+0x78): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x128): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x1a2): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
main.cpp:(.text+0x1ca): undefined reference to `cv::waitKey(int)'
collect2: error: ld returned 1 exit status
EDIT (2):
Using make after cmake:
computervision#computervision-VirtualBox:~/CLionProjects/untitled$ make
Scanning dependencies of target untitled
[ 50%] Building CXX object CMakeFiles/untitled.dir/main.cpp.o
[100%] Linking CXX executable untitled
CMakeFiles/untitled.dir/main.cpp.o: In function `main':
main.cpp:(.text+0x78): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x128): undefined reference to `cv::namedWindow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
main.cpp:(.text+0x1a2): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
main.cpp:(.text+0x1ca): undefined reference to `cv::waitKey(int)'
collect2: error: ld returned 1 exit status
CMakeFiles/untitled.dir/build.make:97: recipe for target 'untitled' failed
make[2]: *** [untitled] Error 1
CMakeFiles/Makefile2:67: recipe for target 'CMakeFiles/untitled.dir/all' failed
make[1]: *** [CMakeFiles/untitled.dir/all] Error 2
Makefile:83: recipe for target 'all' failed
make: *** [all] Error 2
Linking OpenCV in CMakeLists.txt is missing.
cmake_minimum_required(VERSION 3.9)
project(untitled)
set(CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED ) # locate OpenCV in system
include_directories( ${OpenCV_INCLUDE_DIRS} ) # provide library headers
add_executable(untitled main.cpp)
target_link_libraries( untitled ${OpenCV_LIBS} /usr/lib/x86_64-linux-gnu/libopencv_highgui.so) # link OpenCV libraries , hightgui.so not found by cmake so this hack
MESSAGE("OpenCV_LIBS: " ${OpenCV_LIBS} ) #display opencv libs found
if your compiler finds OpenCV and after executing cmake it should show OpenCV libraries found.

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.