- CMake (install via
brew install cmakeon macOS) - A C++ compiler (clang++ comes with Xcode Command Line Tools)
- Visual Studio Code with C++ and CMake extensions
Create a basic project structure:
mkdir my_project
cd my_project
mkdir src include buildCreate the main CMake configuration file in the root directory:
cmake_minimum_required(VERSION 3.11)
project("159731_Project" CXX C)
# Enable IDE Project Folders
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
#########################################################
# Find OpenCV
#########################################################
find_package(OpenCV REQUIRED)
#########################################################
# Include directories
#########################################################
include_directories(${OpenCV_INCLUDE_DIRS})
include_directories("${PROJECT_SOURCE_DIR}/src")
include_directories("${PROJECT_SOURCE_DIR}/include")
# ... rest of CMake configuration ...Create a new file src/main.cpp:
#include <iostream>
int main() {
std::cout << "Hello, CMake!" << std::endl;
return 0;
}Create src/CMakeLists.txt:
#########################################################
# Source Files
#########################################################
SET(sources "main.cpp")
add_executable(main ${sources})
# Link with OpenCV Library
target_link_libraries(main PRIVATE ${OpenCV_LIBS})- Configure the project:
mkdir build && cd build
cmake ..- Build the project:
make- Run the executable:
./src/main-
Install these VS Code extensions:
- C/C++
- CMake
- CMake Tools
-
Create
.vscode/settings.json:
{
"cmake.configureOnOpen": true,
"cmake.buildDirectory": "${workspaceFolder}/build"
}cmake ..- Configure projectcmake --build .- Build projectcmake --build . --clean-first- Clean and buildcmake --build . --target clean- Clean build files
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Debug",
"type": "cppdbg",
"request": "launch",
"program": "${workspaceFolder}/build/main",
"args": [],
"stopAtEntry": false,
"cwd": "${workspaceFolder}",
"environment": [],
"externalConsole": false,
"MIMode": "lldb"
}
]
}Now you can press F5 to debug your program in VS Code.
g++ -o executable_name program_name.cpp -lopencv_highgui -lopencv_core -lopencv_imgcodecs
or, if the path for the include directory and library directory are needed:
g++ -o executable_name program_name.c -I /usr/local/include/opencv/
-L /usr/local/lib/ -lopencv_highgui -lopencv_core -lopencv_imgcodecs
or yet another form:
g++ -o executable name program name.c `pkg-config --libs --cflags opencv`