kl3m3n's blog

Community Browser
cancel
Showing results for 
Search instead for 
Did you mean: 

Create a Visual Studio project from source files using Cmake (OpenCV and PCL)

Klemen
Active Participant

Dear readers,

there have been some questions on how to create a Visual Studio project from the source files (.cpp) that are attached to some of the posts on this blog. This makes it possible to modify the code to your preferences.

The post will hopefully act as a tutorial in order to successfully create a project using either OpenCV or PCL functionalities. You can setup the project manually of course, but I prefer using Cmake (open-source). This helps you configure all dependencies and libraries with a couple of mouse clicks. You can get it from here:

http://www.cmake.org/

After installing Cmake, follow the instructions below:

1. create a new folder (I will call it "project folder") and put the source code (.cpp) inside it. Create another folder inside the previously created folder and name it "build".


2. create a file named "CMakeLists.txt", put it in the "project folder" and setup the Cmake variables in the .txt as follows (replacing the bold text with your values):

    

     2a. using OpenCV

         

               cmake_minimum_required(VERSION 2.6)

               project(project name)

               find_package(OpenCV REQUIRED)

               add_executable(name source_file.cpp)

               target_link_libraries(name ${OpenCV_LIBS} )

     2b. using PCL

         

               cmake_minimum_required(VERSION 2.6 FATAL_ERROR)

               project(project name)

               find_package(PCL 1.3 REQUIRED)

               include_directories(${PCL_INCLUDE_DIRS})

               link_directories(${PCL_LIBRARY_DIRS})

               add_definitions(${PCL_DEFINITIONS})

               add_executable(name source_file.cpp)

               target_link_libraries (name ${PCL_LIBRARIES})

3. open Cmake and add path to source code (...\"project folder"). Add also the path to the "build" folder.

4. select "Configure" and specify the generator for the project (I use Visual Studio 2010). Use default native compilers. Click finish.

     4a. if OpenCV configuration is giving errors, complaining that OpenCV directory is not found, manually add it. Select "ungrouped entries" and specify the OpenCV dir "...\opencv\build". Click "Configure" again.

5. click on "Generate".

6. the "build" is now populated with the Cmake output. Open the Visual Studio solution.

7. in "Solution Explorer", right click on your project and select "Properties". Under "General" change "Target Extension" to .dll and change "Configuration Type" to "Dynamic Library (.dll)".

8. Apply changes and build solution. I suggest using "Release" configuration for optimized performance. Remember you need to do step 7 for the "Release" configuration also.

Hope this will be useful to people having troubles with building from source code.

Best regards,

K


https://decibel.ni.com/content/blogs/kl3m3n



"Kudos: Users may give one another Kudos on the forums for posts that they found particularly helpful or insightful."
Comments