官术网_书友最值得收藏!

Creating a library

CMake allows you to create libraries, which are indeed used by the OpenCV build system. Factorizing the shared code among multiple applications is a common and useful practice in software development. In big applications or when the common code is shared in multiple applications, this practice is very useful.

In this case, we do not create a binary executable; instead, we create a compiled file that includes all the functions, classes, and so on, developed. We can then share this library file with the other applications without sharing our source code.

CMake includes the add_library function for this purpose:

# Create our hello library
add_library(Hello hello.cpp hello.h)

# Create our application that uses our new library
add_executable(executable main.cpp)

# Link our executable with the new library
target_link_libraries( executable Hello )

The lines starting with # add comments and are ignored by CMake.

The add_library(Hello hello.cpp hello.h) command defines our new library called, where Hello is the library name and hello.cpp, hello.h are the source files. We add the header file to allow IDEs such as Visual Studio to link to the header files.

This line will generate a shared file (So for OS X and Unix or .dll for Windows) or a static library (A for OS X and Unix or .dll for Windows), depending on our operating system or if it is a dynamic or static library.

target_link_libraries( executable Hello) is the function that links our executable to the desired library; in our case, it's the Hello library.

主站蜘蛛池模板: 淳化县| 乌鲁木齐县| 安吉县| 伊川县| 北宁市| 社会| 屏山县| 金门县| 延长县| 永嘉县| 甘德县| 东平县| 陇川县| 吉林市| 资阳市| 屯留县| 达州市| 徐水县| 栾川县| 平塘县| 古交市| 阳城县| 巴林右旗| 秭归县| 垫江县| 中宁县| 基隆市| 孟连| 宿迁市| 朝阳市| 台中县| 景洪市| 师宗县| 远安县| 徐汇区| 新竹市| 遂平县| 云浮市| 顺昌县| 辉南县| 镇康县|