0

I've created a C++ class where I calculate the CPU usage using getrusage getrusage(RUSAGE_SELF, &usage).

I can successfully create and link a shared library .so that I created, however, I want to use that same shared.so library in some other external project. For instance, If I want to link this .so to a binary that is generated in a different project.

Here is my CMakeLists.txt to create a shared library libcpp_cpu_usage_library.so from the source file ${PROJECT_SOURCE_DIR}/src/cpu_usage.cpp.

CMakeLists.txt to generate a shared library

############################################################
# Find CMake, Compiler Version, and OS
############################################################
cmake_minimum_required(VERSION 3.15)
project(cpp_cpu_usage VERSION 1.0.1 DESCRIPTION "cpp_cpu_usage description")
set(CMAKE_CXX_STANDARD 20)

message(STATUS "CMake version   : " ${CMAKE_SYSTEM_VERSION})
message(STATUS "Compiler        : " ${CMAKE_CXX_COMPILER})
message(STATUS "Operating System: " ${CMAKE_SYSTEM})
message(STATUS "SYSTEM PROCESSOR: " ${CMAKE_SYSTEM_PROCESSOR})

############################################################
# Create a library cpp_cpu_usage_library  [.so]
############################################################
#Generate the shared library from the library sources
add_library(cpp_cpu_usage_library SHARED
        ${PROJECT_SOURCE_DIR}/src/cpu_usage.cpp
        )
add_library(cpp_cpu_usage::library ALIAS cpp_cpu_usage_library)


target_include_directories(cpp_cpu_usage_library
        PUBLIC
        ${PROJECT_SOURCE_DIR}/include
        )

############################################################
#  Create a executable for cpp-cpu-usage
############################################################
# Add an executable with the above sources
add_executable(cpp_cpu_usage_binary
        ${PROJECT_SOURCE_DIR}/src/main.cpp
        )

############################################################
# Target Link Libaries
############################################################
# link the cpp_cpu_usage_library target with the cpp-cpu-usage target
target_link_libraries(cpp_cpu_usage_binary
        PRIVATE
        cpp_cpu_usage::library
        )

Issue

It worked with binary cpp_cpu_usage_binary which is generated to the same CMake project, however, now I want to use that libcpp_cpu_usage_library.so is some external CMake project.

For instance, I want to use that shared library in the external C++ and CMake project.

#include <iostream>
using namespace cpu_usage;

int main(int argc, char** argv)
{
  double utime;
  double stime;
  double cputime;

  CpuUsage::cpu_start();
  delay();
  CpuUsage::cpu_end();

  
  printf("utime [getrusage]: %f\n", utime);
  printf("stime [getrusage]: %f\n", stime);
  printf("cputime [getrusage]: %f\n", cputime);

  return 0;
}

I know that I had to link the binary for the above to libcpp_cpu_usage_library.so but how can I install libcpp_cpu_usage_library.so to usr/local/lib or root and then include and link in my external CMake project.

Thanks & looking forward.

Arslan

Arslan Ali
  • 63
  • 1
  • 12
  • 1
    The other project could use the library via `find_package` mechanism. CMake documentation has [a nice description and an example](https://cmake.org/cmake/help/latest/manual/cmake-packages.7.html) for make a library project to support that usage. I would suggest to read that description. – Tsyvarev Jan 17 '21 at 09:13
  • @Tsyvarev Thanks for your brisk response and I highly appreciate your help. – Arslan Ali Jan 17 '21 at 12:33

0 Answers0