在 Windows 上的 Visual Studio 中构建 C++"Zivid Hello World"应用程序
To build a C++ "Hello World" application with the Zivid API in Visual Studio 2022, create the project files, configure the solution with CMake, then build and run it. This tutorial shows how to create and develop a C++ application with Zivid in Visual Studio 2022.
要求
备注
测试过的 CMake 版本是 3.24.0。
说明
创建ZividHelloWorld.cpp和CMakeLists.txt
首先,我们必须为项目创建一个 CMakeLists.txt 文件和一个主 cpp 文件。打开 Visual Studio 2022,然后单击 Open a folder 。我们将在这里创建项目。在本教程中,我们将使用以下文件名和源路径:
源路径 |
|
Cpp 文件名 |
|
CMake 文件名 |
|
其次,将以下代码复制并粘贴到 ZividHelloWorld.cpp 和 CMakeLists.txt ;
#include <Zivid/Zivid.h> #include <iostream> int main() { try { Zivid::Application zivid; std::cout << "Hello Zivid World!" << std::endl; } catch(const std::exception &e) { std::cerr << "Error: " << Zivid::toString(e) << std::endl; return EXIT_FAILURE; } }
cmake_minimum_required(VERSION 3.24.0 FATAL_ERROR)
project(ZividHelloWorld)
set(CMAKE_CXX_STANDARD 17)
find_package(Zivid COMPONENTS Core REQUIRED)
add_executable(${PROJECT_NAME} ${PROJECT_NAME}.cpp)
target_link_libraries(
${PROJECT_NAME}
Zivid::Core
Zivid::Visualization
)
小技巧
Visual Studio 2019 及更新版本可以自动配置 CMakeLists 并构建解决方案。您不必通过 CMake 进行配置,因此可以跳过下一节并直接转到 运行程序 。
使用CMake配置解决方案
通过命令提示符或 CMake GUI 运行 CMake。
Command Prompt
在 ZividHelloWorld 目录中创建一个构建目录,并通过运行以下命令配置解决方案:
cmake -B build
小技巧
Make sure cmake is in your system PATH to use the cmake -B build command.
See the "Add CMake to the system PATH" dropdown below for how to add CMake to your system PATH.
将 CMake 添加到系统 PATH
在文件资源管理器中或通过提示打开生成的 Visual Studio 解决方案 ZividCppSamples.sln 以进一步构建示例。
cmake --open build
CMake GUI
构建解决方案
In Visual Studio, change the build configuration from Debug to Release. Debug builds disable compiler optimizations and significantly degrade performance, which is not suitable for benchmarking or production use.
Build the solution by pressing Ctrl + Shift + B.
运行程序
在右侧,右键单击 ZividHelloWorld ,然后单击 Set as StartUp project 。按 F5 键运行该程序。
示例程序位于 ZividHelloWorld/build/Release 下的 build 目录中。您也可以通过双击文件资源管理器中的 exe 或使用命令提示符来运行示例,例如,
.\ZividHelloWorld.exe
要继续使用 Zivid 和 C++ 进行开发,请查看 Zivid C++ API reference 和 使用 CMake 配置 C++ 示例并在 Windows 的 Visual Studio 中构建它们 。
版本历史
SDK |
变更 |
|---|---|
2.12.0 |
移除了对 Visual Studio 2017 的支持。 |