CMake build gives undefined reference to functions in OpenCV - c++

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?

Related

Building FLTK Project with CMAKE/CONAN

I'm fairly new to C++ / CMake, but I'd like to create a project with FLTK using CMAKE and CONAN as package manager. I'm using Windows 11, but trying to get it to run under WSL (Ubuntu 20.04). My WSL-version supports GUI applications.
When I install everything without Conan and compile the official "fltk-hello-world" from the command line using fltk-config, everything works okay. However, when I try to set it up using Conan und Cmake I get following errors:
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Font_Descriptor::Fl_Font_Descriptor(char const*, int, int)':
fl_font.cxx:(.text+0x232): undefined reference to `XftFontOpenXlfd'
/usr/bin/ld: fl_font.cxx:(.text+0x47e): undefined reference to `XftFontMatch'
/usr/bin/ld: fl_font.cxx:(.text+0x48f): undefined reference to `XftFontOpenPattern'
/usr/bin/ld: fl_font.cxx:(.text+0x4e1): undefined reference to `XftFontOpen'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::width(char const*, int)':
fl_font.cxx:(.text+0x954): undefined reference to `XftTextExtents32'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::width(unsigned int)':
fl_font.cxx:(.text+0xa5e): undefined reference to `XftTextExtents32'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::text_extents(char const*, int, int&, int&, int&, int&)':
fl_font.cxx:(.text+0xb07): undefined reference to `XftTextExtents32'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x1140): undefined reference to `XftDrawChange'
/usr/bin/ld: fl_font.cxx:(.text+0x1176): undefined reference to `XftDrawSetClip'
/usr/bin/ld: fl_font.cxx:(.text+0x1209): undefined reference to `XftDrawString32'
/usr/bin/ld: fl_font.cxx:(.text+0x12d9): undefined reference to `XftDrawCreate'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `Fl_Xlib_Graphics_Driver::rtl_draw(char const*, int, int, int)':
fl_font.cxx:(.text+0x149b): undefined reference to `XftTextExtents32'
/usr/bin/ld: fl_font.cxx:(.text+0x14cc): undefined reference to `XftDrawChange'
/usr/bin/ld: fl_font.cxx:(.text+0x1502): undefined reference to `XftDrawSetClip'
/usr/bin/ld: fl_font.cxx:(.text+0x1595): undefined reference to `XftDrawString32'
/usr/bin/ld: fl_font.cxx:(.text+0x15f9): undefined reference to `XftDrawCreate'
/usr/bin/ld: /home/bfl/.conan/data/fltk/1.3.8/_/_/package/b6898709771003e31b8ea824ee836cf119580bd8/lib/libfltk.a(fl_font.cxx.o): in function `fl_destroy_xft_draw(unsigned long)':
fl_font.cxx:(.text+0x10d9): undefined reference to `XftDrawChange'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/hello.dir/build.make:84: bin/hello] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/hello.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
So I assume that i am probably not linking XFT? But I am not sure how to do that, do I need to include the correct name in target-link-libraries? My Cmake-File looks like this:
cmake_minimum_required(VERSION 3.2.3)
project(Hello VERSION 0.1.0)
set(EXECUTABLE_NAME hello)
set(EXE_SOURCES hello.cc)
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_executable(${EXECUTABLE_NAME} ${EXE_SOURCES})
target_include_directories(hello PUBLIC ${FLTK_INCLUDE_DIRS})
target_link_libraries(hello
${CONAN_LIBS})
I'm grateful for any suggestion.
Thanks,
GB
The conan recipe for fltk does not correctly deal with the cmake system of fltk.
Fltk auto-detects if the system it compiles on has Xft. If it does, then it enables the HAS_XFT variable and compiles some code into the library that uses it.
Conan does not pick up on this and doesn't add Xft to the dependent libraries.
The easiest way around this bug is to manually add the xft dependency.
If you have libXft installed on your system you can simply add an entry of Xft to the target_link_libraries of the target that uses fltk.
Like this:
target_link_libraries(hello ${CONAN_LIBS} Xft)

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

How to use Boost/Json on linux

I have used boost/json inside my C++ project which I have created under windows. There the dependency was installed with vcpkg (vcpkg.exe install boost-json). Now I want to port this project to Ubuntu. But I have no clue how to install the library under Linux. Probably this is obvious to a C++ veteran but I couldn't get it to work. I can't find any hints in the git project or on the official website.
I have already tried:
using CMake to build and install the library
including the code via add_library in my CMakeLists.txt
Using it as header-only by copying only the include folder
What is the best practice to include such a library in a project and what are the steps to achieve it? Is there a tutorial for such tasks? My biggest problem is, I don`t know what to google for.
I hope someone can help me, thank you in advance.
EDIT:
As proposed by #vre, I built boost 1.78.0 from source. CMake finds now the boost version with version 1.78.0 and the include error is gone. Nevertheless it is still not working as the linking under Linux is failing. The following output I get:
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `parse_server_config_json(std::filesystem::__cxx11::path)':
main.cpp:(.text+0x511): undefined reference to `boost::json::parse(boost::basic_string_view<char, std::char_traits<char> >, boost::json::storage_ptr, boost::json::parse_options const&)'
/usr/bin/ld: main.cpp:(.text+0x544): undefined reference to `boost::json::value::~value()'
/usr/bin/ld: main.cpp:(.text+0x589): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x5d0): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x615): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x662): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: main.cpp:(.text+0x6af): undefined reference to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o:main.cpp:(.text+0x758): more undefined references to `boost::json::object::operator[](boost::basic_string_view<char, std::char_traits<char> >)' follow
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `parse_server_config_json(std::filesystem::__cxx11::path)':
main.cpp:(.text+0xb46): undefined reference to `boost::json::object::~object()'
/usr/bin/ld: main.cpp:(.text+0xbb4): undefined reference to `boost::json::value::~value()'
/usr/bin/ld: main.cpp:(.text+0xd4f): undefined reference to `boost::json::object::~object()'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::object::object(boost::json::object const&)':
main.cpp:(.text._ZN5boost4json6objectC2ERKS1_[_ZN5boost4json6objectC5ERKS1_]+0x4a): undefined reference to `boost::json::object::object(boost::json::object const&, boost::json::storage_ptr)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_object()':
main.cpp:(.text._ZN5boost4json5value9as_objectEv[_ZN5boost4json5value9as_objectEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_array()':
main.cpp:(.text._ZN5boost4json5value8as_arrayEv[_ZN5boost4json5value8as_arrayEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_string() const':
main.cpp:(.text._ZNK5boost4json5value9as_stringEv[_ZNK5boost4json5value9as_stringEv]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
/usr/bin/ld: CMakeFiles/Server.dir/main.cpp.o: in function `boost::json::value::as_int64()':
main.cpp:(.text._ZN5boost4json5value8as_int64Ev[_ZN5boost4json5value8as_int64Ev]+0x66): undefined reference to `boost::json::detail::throw_invalid_argument(char const*, boost::source_location const&)'
collect2: error: ld returned 1 exit status
make[2]: *** [CMakeFiles/Server.dir/build.make:102: Server] Error 1
make[1]: *** [CMakeFiles/Makefile2:140: CMakeFiles/Server.dir/all] Error 2
make: *** [Makefile:84: all] Error 2
I also added as mentioned by #GP8:
find_package(
Boost 1.78 REQUIRED
COMPONENTS json
)
Edit2:
I forgot to link boost-json. After adding the following to my CMakeLists.txt the build was successfull under linux:
target_link_libraries(${PROJECT_NAME}
Boost::boost
Boost::json
)
You should first install boost using the following command : sudo apt-get install libboost-all-dev
To get Boost libraries included in your project you must find the package this way :
find_package(
Boost 1.65 REQUIRED
COMPONENTS json
)
You can then, tell CMake to with which file to create your executable and against which libraries to link :
add_execublable( anyExecutable main.cpp )
target_link_libraries( exeLINK_PUBLIC ${Boost_LIBRARIES})
#GPB outlines the general procedure.
If your CMake/FindBoost doesn't support Boost Json yet, the simplest thing to do is to
#include <boost/json/src.hpp>
in exactly 1 (one) translation unit that participates in your linked binary.
See Header Only

How to include a static library using cmake?

I want to add the library ixwebsocket my project. Following the instructions of the library I get the libixwebsocket.a (the compiled static library, correct me if I am wrong).
How I can use this .a file in my project?
For testing I have created the following program: main.cpp
#CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(cpptest VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(cpptest main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
target_link_libraries(cpptest "${CMAKE_SOURCE_DIR}/libixwebsocket.a")
//main.cpp
#include <iostream>
#include <ixwebsocket/IXNetSystem.h>
#include <ixwebsocket/IXWebSocket.h>
int main(int, char**) {
std::cout << "Hello, world!\n";
ix::WebSocket webSocket;
}
In the directory I have CMakeLists.txt, libixwebsocket.a and main.cpp.
I get a long list of undefined reference errors, the firsts ones:
[build] [ 50%] Linking CXX executable cpptest
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocket.cpp.o): in function `ix::WebSocket::start()':
[build] IXWebSocket.cpp:(.text+0x2240): undefined reference to `pthread_create'
ld] /usr/bin/ld: ../libixwebsocket.a(IXWebSocket.cpp.o): in function `ix::WebSocket::checkConnection(bool)':
[build] IXWebSocket.cpp:(.text+0x5b8d): undefined reference to `pthread_cond_clockwait'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `bool ix::WebSocketPerMessageDeflateCompressor::compressData<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&) [clone .part.0]':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x67): undefined reference to `deflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateCompressor::~WebSocketPerMessageDeflateCompressor()':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x35c): undefined reference to `deflateEnd'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateCompressor::init(unsigned char, bool)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x3a6): undefined reference to `deflateInit2_'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::~WebSocketPerMessageDeflateDecompressor()':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x64c): undefined reference to `inflateEnd'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::init(unsigned char, bool)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x685): undefined reference to `inflateInit2_'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `ix::WebSocketPerMessageDeflateDecompressor::decompress(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x7d6): undefined reference to `inflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXWebSocketPerMessageDeflateCodec.cpp.o): in function `bool ix::WebSocketPerMessageDeflateCompressor::compressData<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::vector<unsigned char, std::allocator<unsigned char> > >(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::vector<unsigned char, std::allocator<unsigned char> >&) [clone .part.0]':
[build] IXWebSocketPerMessageDeflateCodec.cpp:(.text+0x90d): undefined reference to `deflate'
[build] /usr/bin/ld: ../libixwebsocket.a(IXSocketOpenSSL.cpp.o): in function `ix::SocketOpenSSL::openSSLInitialize()':
[build] IXSocketOpenSSL.cpp:(.text+0x1c): undefined reference to `OPENSSL_init_ssl'
[build] /usr/bin/ld: IXSocketOpenSSL.cpp:(.text+0x29): undefined reference to `OPENSSL_init_ssl'
How may these undefined refrence errors be resolved?
Thanks in advance!
It would be always best to use find_package() calls. This way you ensure that the library is indeed installed on your system, or is exported in the build tree.
If you have the library installed on your system you can use this call, like this:
find_package(ixwebsocket REQUIRED)
target_link_libraries(cpptest ixwebsocket::ixwebsocket)
In your case, since the library is compiled independently (and it is not a target in your tree), you need to use find_library(IXWEBSOCKET_LIB ixwebsocket) and specify additional hints/names if you have the library in your own location, following the docs.
I ran into a similar issue today, the library was linking just fine but a static lib is just a collection of .o files and has no understanding of it's own dependencies...
This command worked for me:
g++ -o output/myapp -std=c++17 myapp.cpp -Llib -framework CoreFoundation -framework Security -lmylib
I'm no expert in CMake but this answer seems to indicate that the below would achieve the same effect
target_link_libraries(program "-framework CoreFoundation" "-framework Security")
So if we apply it to your case:
#CMakeLists.txt
cmake_minimum_required(VERSION 3.0.0)
project(cpptest VERSION 0.1.0)
include(CTest)
enable_testing()
add_executable(cpptest main.cpp)
set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)
target_link_libraries(cpptest "${CMAKE_SOURCE_DIR}/libixwebsocket.a" "-framework CoreFoundation")
I've got a feeling pthread_create is likely to live in CoreFoundation but I'm just guessing... You may need to try a few frameworks to find the right one.
Hope this helps a little :)

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