IntelRealSense / librealsense

Intel® RealSense™ SDK
https://www.intelrealsense.com/
Apache License 2.0
7.53k stars 4.81k forks source link

How to use librealsense in CLion? #3782

Closed offchan42 closed 5 years ago

offchan42 commented 5 years ago

I am new to C++ ecosystem so maybe this is a question that might be quite general (but I'm not sure) I have installed CLion, Cygwin, g++, gcc, make, CMake and it's working and I can do hello world C++ project.

I've also installed Intel.RealSense.SDK.exe already. But when I tried to #include <librealsense2/rs.hpp>, it doesn't work because it could not find that library. So my question is how do I include librealsense2? What do I need to do to use librealsense on my Windows 10? Please provide concrete instructions if possible. Or point me to the link that teaches how to do this.

offchan42 commented 5 years ago

I've used include_directories() command in the CLion and it seems the project now sees those header files. The problem is that now the IDE cannot locate the functions specified in those header files. How do I include the library in CMake or g++?

Let me simplify the problem to not be involving CMake or CLion at all. Suppose I use g++ to compile this, I can use -I option to specify the "C:\Program Files (x86)\Intel RealSense SDK 2.0\include" directory of realsense. But what do I do with -L option? Where should I point it to? C:\Program Files (x86)\Intel RealSense SDK 2.0\bin\x64 or "C:\Program Files (x86)\Intel RealSense SDK 2.0\lib\x64\realsense2.lib" don't seem to work

offchan42 commented 5 years ago

I think this should be mentioned somewhere in the document about how to include the SDK in the code: @dorodnic Do you know the solution? I'm not building from source.

RealSenseCustomerSupport commented 5 years ago

Hi @of99555, Which toolchain do you use in CLion?

I use VS2017 toolchain in CLion. And CMakeLists.txt like this,

include_directories("C:\Program Files (x86)\Intel RealSense SDK 2.0\include") link_directories("C:\Program Files (x86)\Intel RealSense SDK 2.0\lib\x64") add_executable(rs-example rs-example.cpp) target_link_libraries(rs-example realsense2.lib)

offchan42 commented 5 years ago

I have Cygwin toolchain and VS2017 toolchain. I used Cygwin before. So now I'm using VS2017 toolchain like you. I still get an error (the program interrupted without returning 0). @RealSenseCustomerSupport

Here is the toolchain setting: image Is the architecture correct? I tried with x86 but it did not work also.

This is CMakeLists.txt file: image

The project name is TestC__ (new project in CLion). Actually, it was TestC++ but the ++ symbol is not allowed in the name.

Here is the main file: image

Here's the output after Run (Shift+F10): image You will see that the hello world is not even printing.

This is the project explorer: image

How do I fix this?

RealSenseCustomerSupport commented 5 years ago

There is no this issue in my test project. I guess, there are different version realsense2.dll in your PC. You can try copy C:\Program Files (x86)\Intel RealSense SDK 2.0\bin\x64\realsense2.dll to your "cmake-build-debug" folder.

offchan42 commented 5 years ago

Thank you for the suggestion.

At first, there is no realsense2.dll file in this folder. Then I copied as you requested: image

The build passes successfully (the previous version was successful as well): image

It fails when I run (with a different message): image

You will see that the hello world message is now printed. But the program doesn't continue. When I press Retry, the program quits without returning zero: image

What could be the next possible issue?

Some rants: I know how to code in C++ a lot. I even use it to do competitive programming in the past. But when it comes to the compiling/building other people' source, it's always problematic. I spent most of my time trying to install the system. Is there a resource you would suggest about understanding stuff required to grasp C++ ecosystem so that I can build/compile/fix other people's projects better? It should be about CMake, CLion, Visual Studio, library files, Windows/Linux compatibility, etc.

RealSenseCustomerSupport commented 5 years ago

Throw an except in your code (Line 9), I think.

There is an article about Windows how to search the .dll files. https://docs.microsoft.com/en-us/windows/desktop/dlls/dynamic-link-library-search-order

offchan42 commented 5 years ago

Hey, it's working now! image

So It seems like whenever there is a runtime error, the program quits without informing me anything useful. (I have also changed runtime_error to exception as you suggested. I'm not sure if that's what you meant though.) image

And I also cannot use the debug functionality of CLion when I use VS2017 toolchain. image

How do you usually debug the software when there is a dialog without an informative message? How do you show the exception message?

offchan42 commented 5 years ago

This is how I'm dealing with the error for now: image

Suggest me if there is a better way.

RealSenseCustomerSupport commented 5 years ago

Why don't you use Visual Studio to replace CLion on Windows? if you want to use the debugger on Windows.

The exception is not an issue, according to your code. more information about exception handling. https://docs.microsoft.com/en-us/windows/desktop/debug/exception-dispatching

offchan42 commented 5 years ago

Sure. I will use Visual Studio when I want to debug. I read what you sent and from what I understand it simply means that if I throw something in the main() function, the program will ExitProcess without telling me any exception message? How do I show the message then? Is the way I did using cerr already good enough?

And the reason that I don't want to use VS2017 is because I'm familiar with JetBrains IDE. All the keyboard shortcuts and refactoring commands are very helpful to me.

RealSenseCustomerSupport commented 5 years ago

How to process an exception, that's your decision.

offchan42 commented 5 years ago

Yep. It is my decision. But I need to see a practical example. That's why I asked in your case what you do. Then I can decide what to do with it. I just wanted to show the message when the exception is not caught with try-catch.

RealSenseCustomerSupport commented 5 years ago

@off99555 For exception handling, https://docs.microsoft.com/en-us/windows/desktop/debug/exception-dispatching should have provided the guidance about this. Any other questions?

offchan42 commented 5 years ago

OK. No other questions. Thank you!