Building FLTK Project with CMAKE/CONAN - c++

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)

Related

Compiling opencv program cause gcc -I/usr/local/lib test.cpp test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory

There is a error when compiling opencv program program simple includes #include <opencv2/core.hpp>
I compiled it with this command
g++ test.cpp -o app `pkg-config --cflags --libs opencv`
compiling opencv in c++
This is full error
gcc -I/usr/local/lib test.cpp
test.cpp:1:10: fatal error: opencv2/core.hpp: No such file or directory
1 | #include <opencv2/core.hpp>
I compiled and install the opencv by looking at this page https://docs.opencv.org/2.4/doc/tutorials/introduction/linux_install/linux_install.html#building-opencv-from-source-using-cmake-using-the-command-line
The code is from https://docs.opencv.org/4.x/d3/d50/group__imgproc__colormap.html
Also my opencv so files are located at /usr/local/lib
Error also says
Perhaps you should add the directory containing `opencv.pc'
to the PKG_CONFIG_PATH environment variable
but I searched /usr directory there is no opencv.pc file
Update
Compiling program after updating my compile command This error throws
$g++ -I/usr/local/include/opencv4/ test.cpp
/usr/bin/ld: /tmp/ccuJvWgF.o: in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit status
$
Update 2
when now compiling
g++ -L/usr/local/lib/libopencv_core.so -I/usr/local/include/opencv4/ test.cpp
it throws this error. There are many opencv so files in /usr/local/lib do I need to include specific opencv so files to compile the code in the link
in function `main':
test.cpp:(.text+0xb4): undefined reference to `cv::imread(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, int)'
/usr/bin/ld: test.cpp:(.text+0xde): undefined reference to `cv::Mat::empty() const'
/usr/bin/ld: test.cpp:(.text+0x154): undefined reference to `cv::Mat::Mat()'
/usr/bin/ld: test.cpp:(.text+0x1a1): undefined reference to `cv::applyColorMap(cv::_InputArray const&, cv::_OutputArray const&, int)'
/usr/bin/ld: test.cpp:(.text+0x21d): undefined reference to `cv::imshow(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, cv::_InputArray const&)'
/usr/bin/ld: test.cpp:(.text+0x254): undefined reference to `cv::waitKey(int)'
/usr/bin/ld: test.cpp:(.text+0x265): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x274): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x33d): undefined reference to `cv::Mat::~Mat()'
/usr/bin/ld: test.cpp:(.text+0x355): undefined reference to `cv::Mat::~Mat()'
collect2: error: ld returned 1 exit sta
With the -L option you should specify the library search path.
Then with -l options you specify the library names you'd like to link against.
So in your case I'd expect to see -L/usr/local/lib -lopencv_core. Note that the -l name has the lib prefix and file extension omitted. (You may need more OpenCV libraries.)
Seeing your struggles, I think it would be good to read a general tutorial about compiling and linking C/C++ programs (on your platform).
I had a similar problem when I moved some code from OpenCV2 to OpenCV4, it appears you are using some OpenCV4 as well. My fix was to not include opencv2/ but to include opencv4/opencv2/. Not exactly sure what about that made it work, I did that way too long ago, but it worked since.

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 write a simple CMAKELISTS for OpenViNO to my C++ programme in Linux

I have been trying very hard to set up OpenVINO for my C++ programme. But the official guide was very unclear to me (partially because I am a very beginner). I was struggling to understand how it finds "InferenceEngine_LIBRARIES" (or "OpenCV_LIBS") without even defining it.
I have tried to understand some examples in GitHub but sadly many of them are for older versions. I was wondering if I could have a minimum demo of the CMakeLists.txt to use the OpenVINO. Thank you very much.
--- Updates ---
Thanks for the comments. I understand some things were handled by CMake behind the scene. Going to the point, here is my CMakeLists file
cmake_minimum_required(VERSION 3.21)
project(openvino)
set(CMAKE_CXX_STANDARD 14)
# OpenVINO
set(ngraph_DIR "/opt/intel/openvino_2021/deployment_tools/ngraph/cmake")
find_package(ngraph REQUIRED)
set(InferenceEngine_DIR "/opt/intel/openvino_2021/deployment_tools/inference_engine/share")
find_package(InferenceEngine REQUIRED)
set(OpenCV_DIR "/opt/intel/openvino_2021/opencv/cmake")
find_package(OpenCV REQUIRED)
add_executable(openvino main.cpp)
target_link_libraries(
${PROJECT_NAME}
PRIVATE ${InferenceEngine_LIBRARIES}
${OpenCV_LIBS}
${NGRAPH_LIBRARIES})
And my "main.cpp" is just
#include <inference_engine.hpp>
int main() {
InferenceEngine::Core core;
InferenceEngine::ExecutableNetwork executable_network;
executable_network = core.LoadNetwork("./models/decoder.xml", "CPU");
auto infer_request = executable_network.CreateInferRequest();
return 0;
}
But When I build it, I got the following error, and I would like to ask for any help. Thank you very much!
====================[ Build | openvino | Debug ]================================
/home/kent/.local/share/JetBrains/Toolbox/apps/CLion/ch-0/213.6777.58/bin/cmake/linux/bin/cmake --build /media/kent/DISK2/CLionProjects/openvino/cmake-build-debug --target openvino
[2/2] Linking CXX executable openvino
FAILED: openvino
: && /usr/bin/c++ -g CMakeFiles/openvino.dir/main.cpp.o -o openvino -Wl,-rpath,/opt/intel/openvino_2021/opencv/lib:/opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64:/opt/intel/openvino_2021/deployment_tools/ngraph/lib /opt/intel/openvino_2021/opencv/lib/libopencv_gapi.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_highgui.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_ml.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_objdetect.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_photo.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_stitching.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_video.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_videoio.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_dnn.so.4.5.3 /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine_c_api.so /opt/intel/openvino_2021/deployment_tools/ngraph/lib/libngraph.so /opt/intel/openvino_2021/opencv/lib/libopencv_imgcodecs.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_calib3d.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_features2d.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_flann.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_imgproc.so.4.5.3 /opt/intel/openvino_2021/opencv/lib/libopencv_core.so.4.5.3 && :
/usr/bin/ld: warning: libtbb.so.2, needed by /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so, not found (try using -rpath or -rpath-link)
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::interface7::internal::task_arena_base::internal_max_concurrency(tbb::interface7::task_arena const*)'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::interface7::internal::task_arena_base::internal_initialize()'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::task_scheduler_observer_v3::observe(bool)'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::concurrent_vector_base_v3::internal_grow_by(unsigned long, unsigned long, void (*)(void*, void const*, unsigned long), void const*)'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::concurrent_vector_base_v3::~concurrent_vector_base_v3()'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::NFS_Allocate(unsigned long, unsigned long, void*)'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::concurrent_vector_base_v3::internal_clear(void (*)(void*, unsigned long))'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::interface7::internal::task_arena_base::internal_current_slot()'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::allocate_via_handler_v3(unsigned long)'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::interface7::internal::task_arena_base::internal_execute(tbb::interface7::internal::delegate_base&) const'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::interface7::internal::task_arena_base::internal_terminate()'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::NFS_Free(void*)'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::thread_get_id_v3()'
/usr/bin/ld: /opt/intel/openvino_2021/deployment_tools/inference_engine/lib/intel64/libinference_engine.so: undefined reference to `tbb::internal::deallocate_via_handler_v3(void*)'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
The linker error shows that it cannot find the TBB symbols. The TBB library should be pointed to by the TBB_DIR variable. You don't have to set those variables manually using cmake's set() function. Instead - in the shell where you compile your own app - you can source OpenVINO's setupvars.sh script. Just run something like: source /opt/intel/openvino_2021/bin/setupvars.sh and re-run the compiler.
I can see you're using CLion, not the terminal directly. In that case you can try adding the variable manually. The TBB location might be slightly different between the OV versions but in general it should point to a subdirectory of /opt/intel/openvino_2021 - just browse the installation directory and try to find it or source setupvars.sh in the terminal and copy the TBB_DIR env var value to your IDE.

CMake C++ Project librealsense: undefined reference to OpenGL with CLion

I am trying to integrate librealsense C++ CLion Project on Ubuntu 20.04.
Compiling the Librealsense separately in the terminal works just as expected.
the Project looks like this:
RS_Pipeline
.
├── build
├── main.cpp
├── CMakeLists.txt
└── librealsense // the integrated lib
├── CMakeLists.txt
├── third-party // libs like GLFW and GLAD which are use in the examples
├── examples //
| ├── example.hpp // with class to generate OpenGL Window
└── ...
The CMakeLists.txt in the base Folder looks rather simple:
cmake_minimum_required(VERSION 3.2)
project(RS_Pipeline LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 11)
set(OpenGL_GL_PREFERENCE LEGACY)
add_executable(RS_Pipeline main.cpp librealsense/examples/example.hpp)
add_subdirectory(librealsense)
target_link_libraries(${PROJECT_NAME} realsense2)
As soon as i try to include the submodul into my CLion Project i get a ton of: main.cpp:(.text+0x23c): undefined reference to glViewport error messages and building my main.cpp in the base dir fails.
somehow the #include <librealsense2/rs.hpp> #include "librealsense/examples/example.hpp"
works fine and even the intellisense finds all the functions.
PLUS: the included examples within the librealsense Library also compile without any problem. (They use exactly the same #include "librealsense/examples/example.hpp".
It looks like, that CLion just dose not link find the right openGL in my own main.cpp resp. if i include it from example.hpp.
====================[ Build | RS_Pipeline | Release ]===========================
/snap/clion/175/bin/cmake/linux/bin/cmake --build /home/lukas/Development/Cpp/RS_Pipeline/cmake-build-release --target RS_Pipeline
[0/1] Re-running CMake...
-- Checking internet connection...
-- Internet connection identified
-- Info: REALSENSE_VERSION_STRING=2.50.0
-- Setting Unix configurations
-- Building libcurl enabled
-- using RS2_USE_V4L2_BACKEND
-- Could NOT find apriltag (missing: APRILTAG_INC APRILTAG_LIB)
-- Unable to find apriltag library, skipping pose-apriltag example
-- Check for updates capability added to realsense-viewer
-- Check for updates capability added to realsense-depth-quality
-- Building with TM2
-- Fetching recommended firmwares:
-- D4XX_FW_VERSION: 5.13.0.50
-- SR3XX_FW_VERSION: 3.26.1.0
-- T26X_FW_VERSION: 0.2.0.951
-- L51X_FW_VERSION: 1.5.8.1
-- L53X_FW_VERSION: 3.5.5.1
-- https://librealsense.intel.com/Releases/RS4xx/FW/D4XX_FW_Image-5.13.0.50.bin
-- Download firmware 0;"returning early; file already exists with expected SHA1 hash" for D4XX_FW_Image-5.13.0.50.bin
-- https://librealsense.intel.com/Releases/SR300/FW/SR3XX_FW_Image-3.26.1.0.bin
-- Download firmware 0;"returning early; file already exists with expected SHA1 hash" for SR3XX_FW_Image-3.26.1.0.bin
-- https://librealsense.intel.com/Releases/TM2/FW/target/0.2.0.951/target-0.2.0.951.mvcmd
-- Download firmware 0;"returning early; file already exists with expected SHA1 hash" for target-0.2.0.951.mvcmd
-- https://librealsense.intel.com/Releases/L5xx/FW/L51X_FW_Image-1.5.8.1.bin
-- Download firmware 0;"returning early; file already exists with expected SHA1 hash" for L51X_FW_Image-1.5.8.1.bin
-- https://librealsense.intel.com/Releases/L5xx/FW/L53X_FW_Image-3.5.5.1.bin
-- Download firmware 0;"returning early; file already exists with expected SHA1 hash" for L53X_FW_Image-3.5.5.1.bin
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lukas/Development/Cpp/RS_Pipeline/cmake-build-release
[1/1] Linking CXX executable RS_Pipeline
FAILED: RS_Pipeline
: && /usr/bin/c++ -O3 -DNDEBUG -rdynamic CMakeFiles/RS_Pipeline.dir/main.cpp.o -o RS_Pipeline -Wl,-rpath,/home/lukas/Development/Cpp/RS_Pipeline/cmake-build-release/librealsense librealsense/librealsense2.so.2.50.0 -lglfw && :
/usr/bin/ld: CMakeFiles/RS_Pipeline.dir/main.cpp.o: in function `set_viewport(rect const&)':
main.cpp:(.text+0x23c): undefined reference to `glViewport'
/usr/bin/ld: main.cpp:(.text+0x241): undefined reference to `glLoadIdentity'
/usr/bin/ld: main.cpp:(.text+0x24b): undefined reference to `glMatrixMode'
/usr/bin/ld: CMakeFiles/RS_Pipeline.dir/main.cpp.o: in function `draw_pointcloud(float, float, glfw_state&, rs2::points&)':
main.cpp:(.text+0x2bb): undefined reference to `glLoadIdentity'
/usr/bin/ld: main.cpp:(.text+0x2c5): undefined reference to `glPushAttrib'
/usr/bin/ld: main.cpp:(.text+0x2e0): undefined reference to `glClearColor'
/usr/bin/ld: main.cpp:(.text+0x2ea): undefined reference to `glClear'
/usr/bin/ld: main.cpp:(.text+0x2f4): undefined reference to `glMatrixMode'
/usr/bin/ld: main.cpp:(.text+0x2f9): undefined reference to `glPushMatrix'
/usr/bin/ld: main.cpp:(.text+0x32a): undefined reference to `gluPerspective'
/usr/bin/ld: main.cpp:(.text+0x334): undefined reference to `glMatrixMode'
/usr/bin/ld: main.cpp:(.text+0x339): undefined reference to `glPushMatrix'
/usr/bin/ld: main.cpp:(.text+0x36c): undefined reference to `gluLookAt'
/usr/bin/ld: main.cpp:(.text+0x394): undefined reference to `glTranslatef'
/usr/bin/ld: main.cpp:(.text+0x3b2): undefined reference to `glRotated'
/usr/bin/ld: main.cpp:(.text+0x3cf): undefined reference to `glRotated'
/usr/bin/ld: main.cpp:(.text+0x3e3): undefined reference to `glTranslatef'
/usr/bin/ld: main.cpp:(.text+0x3f6): undefined reference to `glPointSize'
/usr/bin/ld: main.cpp:(.text+0x400): undefined reference to `glEnable'
/usr/bin/ld: main.cpp:(.text+0x40a): undefined reference to `glEnable'
/usr/bin/ld: main.cpp:(.text+0x417): undefined reference to `glBindTexture'
/usr/bin/ld: main.cpp:(.text+0x437): undefined reference to `glTexParameterfv'
/usr/bin/ld: main.cpp:(.text+0x44b): undefined reference to `glTexParameteri'
/usr/bin/ld: main.cpp:(.text+0x45f): undefined reference to `glTexParameteri'
/usr/bin/ld: main.cpp:(.text+0x466): undefined reference to `glBegin'
/usr/bin/ld: main.cpp:(.text+0x516): undefined reference to `glEnd'
/usr/bin/ld: main.cpp:(.text+0x51b): undefined reference to `glPopMatrix'
/usr/bin/ld: main.cpp:(.text+0x525): undefined reference to `glMatrixMode'
/usr/bin/ld: main.cpp:(.text+0x52a): undefined reference to `glPopMatrix'
/usr/bin/ld: main.cpp:(.text+0x52f): undefined reference to `glPopAttrib'
/usr/bin/ld: main.cpp:(.text+0x558): undefined reference to `glVertex3fv'
/usr/bin/ld: main.cpp:(.text+0x566): undefined reference to `glTexCoord2fv'
/usr/bin/ld: CMakeFiles/RS_Pipeline.dir/main.cpp.o: in function `draw_pointcloud_wrt_world(float, float, glfw_state&, rs2::points&, rs2_pose&, float*, std::vector >&)':
main.cpp:(.text+0x98b): undefined reference to `glLoadIdentity'
/usr/bin/ld: main.cpp:(.text+0x995): undefined reference to `glPushAttrib'
/usr/bin/ld: main.cpp:(.text+0x9b0): undefined reference to `glClearColor'
/usr/bin/ld: main.cpp:(.text+0x9ba): undefined reference to `glClear'
/usr/bin/ld: main.cpp:(.text+0x9c4): undefined reference to `glMatrixMode'
/usr/bin/ld: main.cpp:(.text+0x9c9): undefined reference to `glPushMatrix'
/usr/bin/ld: main.cpp:(.text+0x9fa): undefined reference to `gluPerspective'
/usr/bin/ld: main.cpp:(.text+0xa04): undefined reference to `glMatrixMode'
/usr/bin/ld: main.cpp:(.text+0xa09): undefined reference to `glPushMatrix'
/usr/bin/ld: main.cpp:(.text+0xa2f): undefined reference to `glTranslatef'
/usr/bin/ld: main.cpp:(.text+0xa4a): undefined reference to `glRotated'
/usr/bin/ld: main.cpp:(.text+0xa65): undefined reference to `glRotated'
/usr/bin/ld: main.cpp:(.text+0xa79): undefined reference to `glTranslatef'
/usr/bin/ld: main.cpp:(.text+0xa83): undefined reference to `glEnable'
/usr/bin/ld: main.cpp:(.text+0xa90): undefined reference to `glLineWidth'
/usr/bin/ld: main.cpp:(.text+0xa9a): undefined reference to `glBegin'
/usr/bin/ld: main.cpp:(.text+0xac4): undefined reference to `glColor3f'
/usr/bin/ld: main.cpp:(.text+0xad8): undefined reference to `glVertex3f'
/usr/bin/ld: main.cpp:(.text+0xae2): undefined reference to `glEnd'
/usr/bin/ld: main.cpp:(.text+0xaef): undefined reference to `glLineWidth'
/usr/bin/ld: main.cpp:(.text+0xb02): undefined reference to `glColor3f'
/usr/bin/ld: main.cpp:(.text+0xb2f): undefined reference to `glMultMatrixf'
/usr/bin/ld: main.cpp:(.text+0xb37): undefined reference to `glMultMatrixf'
/usr/bin/ld: main.cpp:(.text+0xb4a): undefined reference to `glPointSize'
/usr/bin/ld: main.cpp:(.text+0xb54): undefined reference to `glEnable'
/usr/bin/ld: main.cpp:(.text+0xb5e): undefined reference to `glEnable'
/usr/bin/ld: main.cpp:(.text+0xb71): undefined reference to `glBindTexture'
/usr/bin/ld: main.cpp:(.text+0xb91): undefined reference to `glTexParameterfv'
/usr/bin/ld: main.cpp:(.text+0xba5): undefined reference to `glTexParameteri'
/usr/bin/ld: main.cpp:(.text+0xbb9): undefined reference to `glTexParameteri'
/usr/bin/ld: main.cpp:(.text+0xbc0): undefined reference to `glBegin'
/usr/bin/ld: main.cpp:(.text+0xc76): undefined reference to `glEnd'
/usr/bin/ld: main.cpp:(.text+0xc7b): undefined reference to `glPopMatrix'
/usr/bin/ld: main.cpp:(.text+0xc85): undefined reference to `glMatrixMode'
/usr/bin/ld: main.cpp:(.text+0xc8a): undefined reference to `glPopMatrix'
/usr/bin/ld: main.cpp:(.text+0xc8f): undefined reference to `glPopAttrib'
/usr/bin/ld: main.cpp:(.text+0xcc0): undefined reference to `glVertex3fv'
/usr/bin/ld: main.cpp:(.text+0xcce): undefined reference to `glTexCoord2fv'
/usr/bin/ld: CMakeFiles/RS_Pipeline.dir/main.cpp.o: in function `set_viewport(rect const&)':
main.cpp:(.text+0x27b): undefined reference to `glOrtho'
collect2: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.
The examples all include this file, while you don't:
find_package(OpenGL REQUIRED)
set(DEPENDENCIES realsense2 glfw ${OPENGL_LIBRARIES})
which makes the realsense2 library depend on GLFW and your OpenGL stack.
Add the following to your CMakeLists.txt:
find_package(OpenGL REQUIRED)
target_link_libraries(${PROJECT_NAME} glfw OpenGL::GL )
#Botje Thanks a lot, that was fast! now it is working with the following CMakeLists:
cmake_minimum_required(VERSION 3.2)
project(RS_Pipeline LANGUAGES CXX C)
set(CMAKE_CXX_STANDARD 11)
set(OpenGL_GL_PREFERENCE LEGACY)
find_package(OpenGL REQUIRED)
add_executable(RS_Pipeline main.cpp librealsense/examples/example.hpp)
add_subdirectory(librealsense)
target_link_libraries(${PROJECT_NAME}
realsense2
glfw OpenGL::GL
glfw OpenGL::GLU
)
looks like this topic can be closed already.

Problems setting external headers and linker libraries in CLion and Cmake [duplicate]

This question already has answers here:
CMake link to external library
(6 answers)
Closed 2 years ago.
I've just started to use CLion as my IDE after mainly using code::blocks on Linux for the last year or so. However I'm finding it really difficult to set up a project that worked and compiled fine in code::blocks because I just don't understand cmake yet.
Here's a quick description of my project - it consists of a main file that is calling a few header and source files, which all in the same directory. In the main file I'm also calling an external header file ("coupling.h", which is located in "/opt/package/API_SRC_Files/Coupling") which describes some classes relating to coupling to that software. The "coupling.h" file in turn references some files in "/opt/package/API_SRC_Files/Coupling". I also need to link to a shared library file "libcoupling.so" that is located in "/opt/package/lib".
Setting this up in code::blocks was pretty easy - just go to build options, and the respective paths to the search directories, and the path to the linked file and then build. Compiles in a few seconds.
I've tried to setup the project in CLion and CMake but I'm truly lost and I don't really understand why CMake is not finding the "coupling.h" file and throws countless "undefined reference to " errors. I'm sure I'll also have a problem setting up the shared library and I'm scared to even think about the difference in debug and release versions at the moment!
Here's my current CMake.txt file, hopefully someone can help. I'm using CLion 2020.1 on Fedora.
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
# add extra include directories
include_directories(.)
include_directories(/opt/package/API_SRC_Files)
include_directories(/opt/package/API_SRC_Files/Core)
include_directories(/opt/package/API_SRC_Files/Coupling)
set(PROJECT_HEADERS
coupling_utilities.h
geometry.h
io.h
shapelib.h
pid.h
spline.h
)
set(PROJECT_SOURCES
main.cpp
io.cpp
coupling_utilities.cpp
shapelib.cpp
pid.cpp
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(MBD ${PROJECT_SOURCES} ${PROJECT_HEADERS})
Here's the error log from the output related to the include_directories():
/snap/clion/114/bin/cmake/linux/bin/cmake --build /home/user/CLionProjects/mbd/cmake-build-debug --target mbd -- -j 24
-- Configuring done
-- Generating done
-- Build files have been written to: /home/user/CLionProjects/mbd/cmake-build-debug
[ 16%] Linking CXX executable mbd
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `main':
/home/user/CLionProjects/mbd/main.cpp:224: undefined reference to `NApi::Coupling::getGeometryId(char const*, int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:260: undefined reference to `NApi::Coupling::getTimeStep(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:268: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:273: undefined reference to `NApi::Coupling::setTime(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:276: undefined reference to `NApi::Coupling::getTime(double&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:285: undefined reference to `NApi::Coupling::setGridCellSize(double const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:288: undefined reference to `NApi::Coupling::setNumberOfCores(int const&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:343: undefined reference to `NApi::Coupling::getGeometryPosition(int, NApi::C3dValue&, NApi::C3x3Matrix&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:345: undefined reference to `NApi::Coupling::getGeometryTranslation(int, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:348: undefined reference to `NApi::Coupling::getGeometryVelocity(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:442: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:444: undefined reference to `NApi::Coupling::getGeometryForces(int, NApi::C3dValue&, NApi::C3dValue&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:531: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:534: undefined reference to `NApi::Coupling::setGeometryMotion(int, NApi::C3dValue const&, NApi::C3x3Matrix const&, NApi::C3dValue const&, NApi::C3dValue const&, double, bool)'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:554: undefined reference to `NApi::Coupling::isConnected() const'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:556: undefined reference to `NApi::Coupling::simulate(double const&, double)'
/usr/bin/ld: CMakeFiles/mbd.dir/main.cpp.o: in function `__static_initialization_and_destruction_0(int, int)':
/home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::ICoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/main.cpp:74: undefined reference to `NApi::Coupling::~ICoupling()'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `connect(NApi::Coupling&)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:54: undefined reference to `NApi::Coupling::initialiseCoupling()'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:67: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:90: undefined reference to `NApi::Coupling::connectCoupling(bool, char const*)'
/usr/bin/ld: CMakeFiles/mbd.dir/coupling_utilities.cpp.o: in function `getResumeTimestep(NApi::Coupling&, double, double)':
/home/user/CLionProjects/mbd/coupling_utilities.cpp:109: undefined reference to `NApi::Coupling::getNumberOfTimesteps(unsigned int&)'
/usr/bin/ld: /home/user/CLionProjects/mbd/coupling_utilities.cpp:136: undefined reference to `NApi::Coupling::getTimesteps(double*, unsigned int&, unsigned int)'
collect2: error: ld returned 1 exit status
gmake[3]: *** [CMakeFiles/mbd.dir/build.make:144: mbd] Error 1
gmake[2]: *** [CMakeFiles/Makefile2:76: CMakeFiles/mbd.dir/all] Error 2
gmake[1]: *** [CMakeFiles/Makefile2:83: CMakeFiles/mbd.dir/rule] Error 2
gmake: *** [Makefile:118: mbd] Error 2
Here is how I would write the cmakelists.txt file:
cmake_minimum_required(VERSION 3.16)
project(MBD VERSION 0.6.1)
set(CMAKE_CXX_STANDARD 14)
set(API_SRC_Files /opt/package/API_SRC_Files) # ${API_SRC_Files}
set(SRC_FILES
main.cpp
coupling_utilities.h coupling_utilities.cpp
geometry.h
io.h io.cpp
pid.h pid.cpp
shapelib.h shapelib.cpp
spline.h
)
# add extra include directories
include_directories(
includes
.
${API_SRC_Files}
${API_SRC_Files}/Core
${API_SRC_Files}/Coupling
)
# add extra lib directories
link_directories(/opt/package/lib)
add_executable(${PROJECT_NAME} ${SRC_FILES})
Use something like the API_SRC_Files variable for two reasons: reduces errors from having to repeat the same path information, makes it easier if the library is ever moved to a new location. The same thing with using the PROJECT_NAME variable. The comment is to remind myself how to utilize the variable later on in the file.
Combine your SRC_FILES and SRC_HEADERS into one array with source and matching header on the same line. This helps to make sure both are listed. Please note when you tell Clion you want to add a new C++ Class, it will be in the form class.cpp class.h, and not alphabetized. I always list the main.cpp, first.
The dot, ., in the include_directories may or may not be necessary.
Hope this helps.