OpenCV cannot find libraries [duplicate] - c++

This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
I am trying to read an image in OpenCV, like this:
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main (int argv, char **argc)
{
Mat image = imread("Foam_Image.jg", CV_LOAD_IMAGE_GRAYSCALE);
return 0;
}
But I get the following error:
undefined reference to cv::imread(cv::String const&, int)
It seems that OpenCV cannot find the libraries I included, maybe because I didn't link them correctly, or maybe there are some libraries missing. Does anyone know how to look for missing libraries or how to link the libraries in OpenCV?

If your operating system is any UNIX which has CMake, then it would be better for you to write a CMakelists.txt file as follows
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
And just use
cmake .
make
./DisplayImage
to execute the program.
you can install CMake from the official repositories using your package manager
In case your operating system is Windows, install CMake and set compiler options as Visual Studio (your version). Also add the OpenCV path to your system path, if not already done.
More instructions here :
http://docs.opencv.org/doc/tutorials/introduction/windows_install/windows_install.html

Related

CMake: DLL's Missing

I'm new to CMake and after a week of endless errors I'm at my wit's end.
main.cpp
#include <tox/tox.h>
int main(int argc, char **argv) {
Tox* tox = tox_new(NULL, NULL);
return 0;
}
CMakeLists.txt
cmake_minimum_required(VERSION 3.18)
Project(toxtest)
add_library(toxcore SHARED IMPORTED)
set_target_properties(toxcore PROPERTIES
INTERFACE_INCLUDE_DIRECTORIES "${CMAKE_SOURCE_DIR}/c-toxcore/include"
IMPORTED_IMPLIB "${CMAKE_SOURCE_DIR}/c-toxcore/_build/Debug/toxcore.lib"
IMPORTED_LOCATION "${CMAKE_SOURCE_DIR}/c-toxcore/_build/Debug/toxcore.dll"
)
set(BINARY_NAME "toxtest-bin")
add_executable(${BINARY_NAME} main.cpp)
target_link_libraries(${BINARY_NAME} PUBLIC toxcore)
Introduction:
My C++ executable depends on a C-library called toxcore, toxcore depends on two other C-libraries called pthread-win32 and libsodium.
I have compiled the toxcore library with the following flags, to solve a unresolved external symbol error (Not sure if this is the right way):
cmake .. -DCMAKE_WINDOWS_EXPORT_ALL_SYMBOLS=TRUE -DBUILD_SHARED_LIBS=TRUE
I build my executable successfully but now it complains about missing DLL's:
toxcore.dll, libsodium.dll, pthreadVC2.dll
With my patience running out:
I dumped the DLL's into the directory with the executable, and now I got Application Error:
The application was unable to start correctly (0xc000007b)
How do I this properly?
I want to solve the Application Error, and let the executable find the DLL's in their respective directories.
PS: Before anyone marks my question as duplicate.
I have looked thru and tried stackoverflow answers which were not sufficient in detail/clarity for a beginner like me. I need someone to give me a more tailored answer and tell me if my steps are in line with the current best practices.

Files/directories to include in Visual Studio C++ to use CUDA?

I want to use CUDA/GPU in OpenCV in Visual Studio. For example, cuda::GpuMat. I successfully build OpenCV with the extra modules with CUDA enabled
I tried the following code
#include <string>
#include <opencv2/core/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/photo/cuda.hpp>
#include <opencv2/photo.hpp>
using namespace std;
using namespace cv;
int main(){
string imageName("input.bmp");
//CPU version
Mat image = imread(imageName.c_str(), IMREAD_GRAYSCALE);
//CUDA version
cuda::GpuMat imageGPU;
cuda::GpuMat downloadGPU;
Mat buff;
imageGPU.upload(image);
downloadGPU.download(buff);
imwrite("gpu.bmp", buff);
return 0;
}
But I get an unhandled exception error.
I originally downloaded OpenCV in C:\Users\me\Downloads\opencv
I then downloaded and installed the latest OpenCV extra modules with CUDA on in
In Property Pages->C/C++->General->Additional Include Directories, I have:
C:\Users\me\Downloads\opencv\build\include\opencv
C:\Users\me\Downloads\opencv\build\include\opencv2
C:\Users\me\Downloads\opencv\build\include\
In Property Pages->Linker->General->Additional Library Directories, I have:
C:\Users\me\Downloads\opencv\build\x64\vc15\lib
and in Property Pages->Linker->Input->Additional Dependencies, I have:
opencv_world343d.lib
opencv_world343.lib
what else am I supposed to include so I can get GpuMat to work properly?
Most of the cases, yes, but you need to know which library you need to add, it may be cufft.lib, cublas.lib, cudnn.lib, etc. It depends of the function you use inside your code.
Opencv includes a cmake include file that would set all of this up for you if you use cmake to build you VS test project. This file will be in the root of the opencv install directory, i.e. after building opencv running cmake --install or the equivalent in VS. The file is OpenCVConfig.cmake, and it would be included in the CMakeLists.txt file for your project. Then you would call FindPackage(OpenCV), which would locate the OpenCV install, setup a few variables, which you would then use to link against your app.
I can post a sample CMakeList.txt file if you feel that would help.

Making allegro5 project with CLion

Few days ago I downloaded clion to make a school project in C++ and Allegro5. First I used allegro installed with homebrew but it wasn't working so I compiled it by myself. Since I've never worked with CMake before it took me some time to include the libs and compile the project but I managed to do it. The problem is that when I try to run it it throws an error:
dyld: Symbol not found: __al_mangled_main
Referenced from: /usr/local/lib/liballegro_main.5.0.dylib
Expected in: flat namespace
in /usr/local/lib/liballegro_main.5.0.dylib
My CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(arkanoid)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES classes/main.cpp classes/ball.cpp classes/ball.h classes/block.cpp classes/block.h)
add_executable(arkanoid ${SOURCE_FILES})
INCLUDE_DIRECTORIES( allegro/5.0.11/include )
LINK_DIRECTORIES( allegro/5.0.11/lib )
TARGET_LINK_LIBRARIES(arkanoid allegro_acodec
allegro_audio
allegro_color
allegro_dialog
allegro_image
allegro_main
allegro_memfile
allegro_physfs
allegro_primitives
allegro_ttf
allegro_font
allegro)
And for now my main.cpp looks like this:
#include <iostream>
#include <allegro5/allegro.h>
using namespace std;
int main() {
al_init();
return 0;
}
I'm trying to build this project on OSX. I've searched for 2 days for the solution to my problem but with no results. Not many people are using CLion and even less use it with allegro5. Could anyone have a clue what this error even mean?
Ok this one blew my mind. I don't know why I found solution only after asking on stackoverflow but I'm posting it for someone who might encounter simillar problem to mine. Change your main declaration from
int main()
to
int main(int argc, char **argv)
and that's it. Really.

making boost work with cmake ( fatal error: boost/smart_ptr.hpp: No such file or directory )

After digging for hours on this, I finally need some expert help.
I am new to cmake and trying to port my Visual Studio 2008 project to cmake.
My project uses opencv, qt and boost libraries.
Now I have managed to run simple cmake examples with opencv and qt but I am stuck with boost.
The bottleneck is this: CMake finds the correct boost version, finds the libraries and gives no error while configuration.
But when I try to build the project using make, it gives me:
" fatal error: boost/smart_ptr.hpp: No such file or directory"
Assuming the configuration is done right, cmake should be able to find the boost include directories.
I have set BOOST_ROOT to the correct boost root directory and that's why configuration does not give errors. But what happens while running make? Am I missing something simple here? Need help desperately...
I am sorry if this is a stupid question.
Here's my CMakeLists.txt file:
cmake_minimum_required( VERSION 2.6 )
project (CMakeBoostTutorial)
find_package( Boost 1.46.1 COMPONENTS date_time REQUIRED)
link_directories ( ${Boost_LIBRARY_DIRS} )
include_directories ( ${Boost_INCLUDE_DIRS} )
add_executable ( helloworld helloworld.cc )
target_link_libraries (
helloworld
${Boost_LIBRARIES}
)
And here's my helloworld.cc:
#include <iostream>
#include <stdlib.h>
#include <boost/smart_ptr.hpp>
using namespace std;<br/>
int main(int argc, char *argv[]){
cout<<"Hello World";
return 0;
}
PS: This compiles and runs fine if I remove the #include <boost/smart_ptr.hpp> line

Error on building OpenCV (C++)

Building my code (below) returns error 'imread' is not a member of 'cv'.
I am using:
Ubuntu 11.04.
libcv is at 2.1.0-3ubuntu1
CMake as a build system (with only project(foo) and add_executable(foo main.cpp) in it.)
main.cpp:
#include <opencv/cv.h>
int main(int argc, char **argv) {
cv::Mat src = cv::imread("frame_original.png", 0);
return 0;
}`
What do I need to include to get cv::imread to work?
imread is part of OpenCV 2.1:
http://opencv.willowgarage.com/documentation/cpp/highgui_reading_and_writing_images_and_video.html?highlight=imread#imread
But where is it on my system? What do I need to include? Where can I find the documentation that tells me which header file I need from OpenCV to use a specific function?
You should include opencv/highgui.h.
You might have to actually include the OpenCV library and the headers in your CMake configuration file.
Especially looking at include_directories for the header files and target_link_libraries for the library itself