Making C++ Project with OpenCV and librealsense - c++

I want to start a computer vision project using an r200 and OpenCV. I'm using the legacy librealsense library and i've ran the examples on it successfully.
Unfortunately i'm new to C++ and Cmake so i'm struggling to import things appropriately.
My project structure looks like:
-Project
-build
-librealsense //this is the directory that contains the code to run the r200
-main.cpp //my main working file
-CMakeLists.txt //what i'm working on
my CMakeLists.txt runs successfully when i execute ../ cmake from the build directory and it looks like this:
cmake_minimum_required(VERSION 3.10)
project(Project)
set(CMAKE_CXX_STANDARD 11)
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_subdirectory(librealsense)
# add the executable
add_executable(Project main.cpp librealsense/examples/example.hpp)
target_link_libraries( Project ${OpenCV_LIBS})
target_link_libraries(Project realsense2)
Note that at the moment i'm just trialing code from the examples and that librealsense/examples/example.hpp is the relative path to one of the example.hpp headers used in the example that i'm copying.
The imports that i'm using in main.cpp are as follows:
#include <librealsense2/rs.hpp>
#include "librealsense/examples/example.hpp"
#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;
It responds with a very verbose error message that ends with the following:
...
"_rs2_start_processing", referenced from:
void rs2::processing_block::start<rs2::frame_queue>(rs2::frame_queue) in main.cpp.o
"_rs2_stream_to_string", referenced from:
texture::show(rect const&) const in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [Project] Error 1
make[1]: *** [CMakeFiles/Project.dir/all] Error 2
make: *** [all] Error 2
My question is how do I actually import OpenCV and the librealsense libraries!!!
Any direction to good guides for this and CMake would make me forever grateful!

Related

Unable to run SFML project with CMake on arm64

I am using MacBook with M1 and I try to create a project with SFML.
I downloaded a snapshot of SFML with files built for arm64
I verified it using file command
Example:
libsfml-graphics.dylib: Mach-O 64-bit dynamically linked shared library arm64
I placed these files in ~/Library/Frameworks along with extlibs directory content.
I set a CMake flag: -DCMAKE_OSX_ARCHITECTURES=arm64
But I still am unable to run this project.
Error:
[ 50%] Linking CXX executable myProject
Undefined symbols for architecture arm64:
"__ZN2sf6StringC1EPKcRKSt6locale", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
collect2: error: ld returned 1 exit status
make[3]: *** [myProject] Error 1
make[2]: *** [CMakeFiles/myProject.dir/all] Error 2
make[1]: *** [CMakeFiles/myProject.dir/rule] Error 2
make: *** [myProject] Error 2
This is my whole CMakeLists.txt file:
cmake_minimum_required(VERSION 3.19)
project(myProject)
add_executable(myProject main.cpp)
set(CMAKE_CXX_COMPILER "/opt/homebrew/bin/g++-11" CACHE STRING "C++ compiler" FORCE)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DCMAKE_OSX_ARCHITECTURES=arm64")
include_directories(/usr/local/include)
find_package(SFML 2.5 COMPONENTS system window graphics network audio REQUIRED)
include_directories(${SFML_INCLUDE_DIRS})
target_link_libraries(myProject sfml-system sfml-window sfml-graphics sfml-audio sfml-network)
I also tried to use dylib instead of framework, but it was unsuccessful also. I'd be grateful for any help, I can't figure out what I did wrong.

OpenCV C++ give architecture arm64 error in Macbook M1 chip

I built OpenCV-4.5.2 in Macbook M1 followed this tutorial: https://sayak.dev/install-opencv-m1. It works fine in Python but when I use in C++
#include <iostream>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
int main()
{
cv::Mat img = cv::imread("avatar.jpeg");
return 0;
}
It give an error in cv::Mat
Undefined symbols for architecture arm64:
"cv::Mat::~Mat()", referenced from:
_main in main.cpp.o
"cv::imread(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > const&, int)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [imgproc] Error 1
make[2]: *** [CMakeFiles/imgproc.dir/all] Error 2
make[1]: *** [CMakeFiles/imgproc.dir/rule] Error 2
make: *** [imgproc] Error 2
After hours, I can't find what's wrong with it. Can anybody help me? Thank you!
P/S: as additional, this is my CMakeLists.txt
cmake_minimum_required(VERSION 3.19)
project(imgproc)
set(CMAKE_CXX_STANDARD 14)
# Set the location of the OpenCV directory
set(OpenCV_DIR "/usr/local/include/opencv4")
# Find OpenCV library
find_package( OpenCV 4 REQUIRED )
# Add header file
include_directories(include ${OpenCV_INCLUDE_DIRS} )
add_executable(imgproc main.cpp)
I found that replace these include:
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
with:
#include <opencv2/opencv.hpp>
Then everything worked!
After some exploration I finally solved the problem. To user the openCV library on M1 Mac, you need to include -I/opt/homebrew/Cellar/opencv/4.5.5/include/opencv4/ -lopencv_core -lopencv_imgcodecs -lopencv_highgui -L/opt/homebrew/Cellar/opencv/4.5.5/lib/ as your g++ compile options.
I have tested OpenCV in macOS successfully, refer to:
https://medium.com/#mfkhao2009/set-up-opencv-development-enrioment-875aa69bd403
You should link the library to the target imgproc by adding this code to CMakeLists.txt
add_executable(imgproc main.cpp)
target_link_libraries(imgproc ${OpenCV_LIBS} )
I've been dealing with the same issue. I kept getting the linker error (Undefined symbols for architecture arm64...). FYI I installed via homebrew on my M1 mac, and developing with CLion.
What solved it was adding this to specifiy X86_64 in cmake:
set(CMAKE_OSX_ARCHITECTURES x86_64)
My full CMakeLists.txt:
cmake_minimum_required(VERSION 3.9)
set(CMAKE_OSX_ARCHITECTURES x86_64)
project(opencvtest)
set(CMAKE_CXX_STANDARD 23)
find_package(OpenCV REQUIRED)
include_directories(${OpenCV_INCLUDE_DIRS})
add_executable(opencvtest main.cpp)
target_link_libraries(opencvtest ${OpenCV_LIBS})

CMAKE Boost Unit Test Framework not working on Mac

The boost test framework will not seem to work on my machine. I have done a lot of googling but all of the answers seem to lead round in circles but don't resolve the issue. I have tried switching to #define BOOST_TEST_DYN_LINK as well but this just brings another batch of errors and I can't see any modern answers suggesting to use this approach.
The below line works fine and I can run tests without issue:
#include <boost/test/include/unit_test.hpp>
The problems all start when I try to move to:
#define BOOST_TEST_MODULE test
#include <boost/test/unit_test.hpp>
#include <iostream>
BOOST_AUTO_TEST_CASE( something_test)
{
BOOST_TEST(true);
BOOST_TEST(2+2 == 4);
BOOST_CHECK(2+3 == 6);
}
my CMAKE file looks like:
cmake_minimum_required(VERSION 3.13)
project(BoostTestsWork)
set(CMAKE_CXX_STANDARD 17)
find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
if(NOT Boost_FOUND)
message(FATAL_ERROR "Could not find boost!")
endif()
message("File System: " ${Boost_FILESYSTEM_LIBRARY})
message("System Library: " ${Boost_SYSTEM_LIBRARY})
message("Unit test framework: " ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY})
message("Boost Library Dir: " ${Boost_LIBRARY_DIRS})
include_directories(${Boost_INCLUDE_DIR})
link_directories(${Boost_LIBRARY_DIRS})
add_executable(BoostTestsWork library.cpp)
target_link_libraries(BoostTestsWork ${Boost_FILESYSTEM_LIBRARY}
${Boost_SYSTEM_LIBRARY}
${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
${Boost_LIBRARIES})
The output I get is:
CMAKE:
-- Boost version: 1.67.0
-- Found the following Boost libraries:
-- system
-- filesystem
-- unit_test_framework
File System: /usr/local/boost_1_67_0/stage/lib/libboost_filesystem.dylib
System Library: /usr/local/boost_1_67_0/stage/lib/libboost_system.dylib
Unit test framework: /usr/local/boost_1_67_0/stage/lib/libboost_unit_test_framework.dylib
Boost Library Dir: /usr/local/boost_1_67_0/stage/lib
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/davidespley/CLionProjects/BoostTestsWork/cmake-build-debug
And compile:
====================[ Build | all | Debug ]=====================================
/Applications/CLion.app/Contents/bin/cmake/mac/bin/cmake --build /Users/davidespley/CLionProjects/BoostTestsWork/cmake-build-debug --target all -- -j 6
[ 50%] Building CXX object CMakeFiles/BoostTestsWork.dir/library.cpp.o
[100%] Linking CXX executable BoostTestsWork
Undefined symbols for architecture x86_64:
"_main", referenced from:
implicit entry/start for main executable
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [BoostTestsWork] Error 1
make[1]: *** [CMakeFiles/BoostTestsWork.dir/all] Error 2
make: *** [all] Error 2
use #include <boost/test/included/unit_test.hpp>
instead of #include <boost/test/unit_test.hpp>
and it won't require linking

allegro5 project in CLion, ld: library not found error

I'm making a C++ and allegro5 project for university. I compiled allegro library and it's working well in Xcode for example. But I wanted to do my project in CLion and as soon as try to build project including allegro it throws an error:
ld: library not found for -lallegro_acodec
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[2]: *** [TEST1] Error 1
make[1]: *** [CMakeFiles/TEST1.dir/all] Error 2
make: *** [all] Error 2
CMakeLists.txt:
cmake_minimum_required(VERSION 3.3)
project(TEST1)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_executable(TEST1 ${SOURCE_FILES})
INCLUDE_DIRECTORIES( /usr/local/Cellar/allegro/5.0.11/include )
LINK_DIRECTORIES( /usr/local/Cellar/allegro/5.0.11/lib )
TARGET_LINK_LIBRARIES(TEST1
allegro_acodec
allegro_audio
allegro_color
allegro_dialog
allegro_image
allegro_main
allegro_memfile
allegro_physfs
allegro_primitives
allegro_ttf
allegro_font
allegro)
main.cpp:
#include <iostream>
#include <allegro5/allegro.h>
using namespace std;
int main(int argc, char **argv) {
al_init();
return 0;
}
I'm working on OSX 10.11. I could not find solution for my problem. I konw that allegro and CLion are not that popular. Can anyone help me what that error means?
You should issue link_directories before add_executable.
From the documentation about link_directories:
The command will apply only to targets created after it is called.

cmake simple sub-directory not including files

I have read and tried just about every tutorial/wiki/SO post, page, snippet I could find to get this CMAKE working....
I have a super simple directory structure:
ROOT/
|- CMakeLists.txt
|- main.cpp
|- sub/
|-CMakeLists.txt
|-subx/
|-CMakeLists.txt
|-subx.h
|-subx.cpp
|-suby/
|-CMakeLists.txt
|-suby.h
|-suby.cpp
The main.cpp is a super simple cpp program:
//omitting all unnecessary code
int main() {
subx s;
s.defined_method();
s.another_defined_method(1);
return 0;
}
You can assume, for everyone's sake that the subx and suby definitions are correct and work just fine, because they do when I compile by hand.
When I compile by CMake I get the following error:
"/path/to/cmake" --build /path/to/Debug --target CS220_Project -- -j 4
Linking CXX executable simple_project
Undefined symbols for architecture x86_64:
"subx::defined_method()", referenced from:
_main in main.cpp.o
"subx::another_defined_method(int)", referenced from:
_main in main.cpp.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make[3]: *** [simple_project] Error 1
make[2]: *** [CMakeFiles/simple_project.dir/all] Error 2
make[1]: *** [CMakeFiles/simple_project.dir/rule] Error 2
make: *** [simple_project] Error 2
The root CMakeLists.txt file looks:
cmake_minimum_required(VERSION 2.8.4)
project(simple_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_subdirectory(sub)
add_executable(simple_project ${SOURCE_FILES})
Sub CMakeLists.txt file looks:
add_subdirectory(subx)
add_subdirectory(suby)
subx & suby CMakeLists.txt file looks: (they include their respective distinction)
set(SUBX_SOURCES subx.cpp)
#Add any files in this directory
add_executable(SUBX ${SUBX_SOURCES})
I've tried things like add_library, file (glob), etc. I cannot, for the life of me get files that are in any sub-directory to compile with the main.cpp program.
Depends on what exactly you want the subprojects to be. The way I understand it, subx and suby are libraries, which should be linked to the main executable:
ROOT/CMakeLists.txt
cmake_minimum_required(VERSION 2.8.4)
project(simple_project)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")
set(SOURCE_FILES main.cpp)
add_subdirectory(sub)
add_executable(simple_project ${SOURCE_FILES})
target_link_libraries(simple_project SUBX SUBY)
ROOT/subx/CMakeLists.txt
set(SUBX_SOURCES subx.cpp)
#Add any files in this directory
add_library(SUBX ${SUBX_SOURCES})
(dtto for suby)