C++ dhe CMake — vĂ«llezĂ«rit pĂ«rjetĂ«, pjesa II

C++ dhe CMake — vĂ«llezĂ«rit pĂ«rjetĂ«, pjesa II

Në pjesën e mëparshme të kësaj historije interesante u diskutua për organizimin e bibliotekës së titujve brenda gjeneratorit të sistemeve të ndërtimit CMake.

Tani do të shtojmë një bibliotekë që përfshin kompilimin, si dhe do të flasim për ndërlidhjen e moduleve me njëra-tjetrën.

Ashtu si më parë, ata që nuk mund të presin mund të kalojnë menjëherë në repozitorin e përditësuar dhe ta prekin gjithçka me duar.


Përmbajtja

  1. Ndaj
  2. Mbizotëro

Ndaj

Gjëja e parë që duhet bërë për të arritur qëllimin tonë të lartë është të ndajmë software-in e zhvilluar në blloqe universale të izoluara, të njëllojta nga këndvështrimi i përdoruesit.

NĂ« pjesĂ«n e parĂ« u pĂ«rshkrua njĂ« bllok standard i tillĂ« — njĂ« projekt me bibliotekĂ« titujsh. Tani do tĂ« shtojmĂ« nĂ« projektin tonĂ« njĂ« bibliotekĂ« qĂ« pĂ«rfshin kompilimin.

Për këtë do të nxjerrim implementimin e funksionit myfunc në një .cpp-skedë:

diff --git a/include/mylib/myfeature.hpp b/include/mylib/myfeature.hpp
index 43db388..ba62b4f 100644
--- a/include/mylib/myfeature.hpp
+++ b/include/mylib/myfeature.hpp
@@ -46,8 +46,5 @@ namespace mylib

         ~  shih mystruct
      * / 
-    inline bool myfunc (mystruct)
-    {
-        kthehu true;
-    }
+    bool myfunc (mystruct);
 }
diff --git a/src/mylib/myfeature.cpp b/src/mylib/myfeature.cpp
new file mode 100644
index 0000000..abb5004
--- /dev/null
+++ b/src/mylib/myfeature.cpp
@@ -0,0 +1,9 @@
+#include 
+
+namespace mylib
+{
+    bool myfunc (mystruct)
+    {
+        kthehu true;
+    }
+}

Më pas do të përcaktojmë bibliotekën që përfshin kompilimin (myfeature), e cila do të përbëhet nga skeda e marrë në hapin e mëparshëm. .cppBiblioteka e re, natyrisht, kërkon titujt ekzistues, dhe për ta siguruar këtë, mund dhe duhet ta lidhim atë me qëllimin ekzistues mylib. Në të vërtetë lidhja midis tyre është publike, dhe kjo do të thotë se çdo gjë që do të lidhet me qëllimin myfeature, automatikisht do të marrë gjithashtu qëllimin mylib (më shumë rreth mënyrave të lidhjes).

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 108045c..0de77b8 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -64,6 +64,17 @@ target_compile_features(mylib INTERFACE cxx_std_17)

 add_library(Mylib::mylib ALIAS mylib)

+###################################################################################################
+##
+##      Biblioteka që përfshin kompilimin
+##
+###################################################################################################
+
+add_library(myfeature src/mylib/myfeature.cpp)
+target_link_libraries(myfeature PUBLIC mylib)
+
+add_library(Mylib::myfeature ALIAS myfeature)
+

Më pas do ta bëjmë që biblioteka e re gjithashtu të instalohet në sistem:

@@ -72,7 +83,7 @@ add_library(Mylib::mylib ALIAS mylib)

 install(DIRECTORY include/mylib DESTINATION include)

-install(TARGETS mylib EXPORT MylibConfig)
+install(TARGETS mylib myfeature EXPORT MylibConfig)
 install(EXPORT MylibConfig NAMESPACE Mylib:: DESTINATION share/Mylib/cmake)

 include(CMakePackageConfigHelpers)

Duhet të vihet re se për qëllimin myfeature, as well as for mylib a pseudonym was created with the prefix Mylib::. The same is specified for both targets when exporting them for installation in the system. This allows for uniform work with targets under any linking scheme.

After that, we need to tie the module tests with the new library (the function myfunc was moved from the header, so now it needs to be linked):

diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index 5620be4..bc1266c 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -4,7 +4,7 @@ add_executable(mylib-unit-tests test_main.cpp)
 target_sources(mylib-unit-tests PRIVATE mylib/myfeature.cpp)
 target_link_libraries(mylib-unit-tests
     PRIVATE
-        Mylib::mylib
+        Mylib::myfeature
         doctest::doctest
 )

Headers (Mylib::mylib) no longer need to be included separately because, as previously mentioned, they are automatically included with the library (Mylib::myfeature).

And we will add a few nuances to ensure coverage measurements considering the newly arrived library:

@@ -15,11 +15,16 @@ if(MYLIB_COVERAGE AND GCOVR_EXECUTABLE)
     target_compile_options(mylib-unit-tests PRIVATE --coverage)
     target_link_libraries(mylib-unit-tests PRIVATE gcov)

+    target_compile_options(myfeature PRIVATE --coverage)
+    target_link_libraries(myfeature PRIVATE gcov)
+
     add_custom_target(coverage
         COMMAND
             ${GCOVR_EXECUTABLE}
-                --root=${PROJECT_SOURCE_DIR}/include/
-                --object-directory=${CMAKE_CURRENT_BINARY_DIR}
+                --root=${PROJECT_SOURCE_DIR}/
+                --filter=${PROJECT_SOURCE_DIR}/include
+                --filter=${PROJECT_SOURCE_DIR}/src
+                --object-directory=${PROJECT_BINARY_DIR}
         DEPENDS
             check
     )

You can add more libraries, executables, etc. In this case, it does not matter how they are linked together within the project. What matters is which targets are the interface of our module, meaning what is exposed externally.

Mbizotëro

Now we have standard module blocks, and we can control them: composing them into structures of any complexity, installing them in the system, or linking them within a unified build system.

Installation in the system

One option for using the module is to install our module into the system.

cmake --build path/to/build/directory --target install

After that, it can be included in any other project using the command find_package.

find_package(Mylib 1.0 REQUIRED)

Including as a submodule

Another option is to link the folder with our project to another project as a submodule using the command add_subdirectory.

Përdorimi

The ways of linking are different, but the result is the same. In both cases, the targets will be accessible in the project that uses our module. Mylib::myfeature dhe Mylib::mylib, të cilat mund të përdoren, për shembull, kështu:

add_executable(some_executable some.cpp sources.cpp)
target_link_libraries(some_executable PRIVATE Mylib::myfeature)

Konkrett në rastin tonë libri Mylib::myfeature duhet lidhur atëherë kur është e nevojshme të lidhet me bibliotekën libmyfeature. Nëse mjaftojnë skedarët e titujve, atëherë duhet të përdoret biblioteka Mylib::mylib.

Qëllimet CMake mund të jenë të zgjuara, për shembull, të destinuara vetëm për të kaluar ndonjë pronë, varësi etj. Në të njëjtën kohë, puna me to ndodh në një mënyrë të njëjtë.

ÇfarĂ« ishte e nevojshme pĂ«r t'u marrĂ«.

Burimi: habr.com

Blini hosting tĂ« besueshĂ«m pĂ«r faqe interneti me mbrojtje nga DDoS, serverĂ« VPS VDS đŸ”„ Blini hosting tĂ« besueshĂ«m pĂ«r faqe interneti me mbrojtje nga DDoS, serverĂ« VPS VDS | ProHoster