I have a library project mk_lib
which creates a shared library mk_lib.so
when built using make
command. I then install this library to /usr/local/lib/
using the command sudo make install
. The library header file lib.h
is also installed to /usr/local/include/
using install
command. The main executable makes use of the mk_lib
library. This library is made available using sudo ldconfig
command which makes path /usr/local/lib/
as library search path during executable mk_demo
creation. I want to know is it possible to perform the three library related actions (creation using make
and installation using sudo make install
to copy library to /usr/local/lib/
and install header file lib.h
to usr/local/include
) as one command instead of executing them separately. Basically if library build is successful then automatically call install commands else don’t.
mk_demo/main.cpp
#include <iostream>
#include "lib.h"
int main(int, char**){
std::cout << "Hello, from mk_demo!n";
std::cout << add(2, 3) << std::endl;
std::cout << subtract(2, 3) << std::endl;
std::cout << multiply(2, 3) << std::endl;
std::cout << divide(2, 3) << std::endl;
}
mk_demo/CMakeLists.txt
cmake_minimum_required(VERSION 3.5.0)
project(mk_demo VERSION 0.1.0 LANGUAGES C CXX)
add_subdirectory(${PROJECT_SOURCE_DIR}/mk_lib)
add_executable(${PROJECT_NAME} main.cpp)
target_link_libraries(${PROJECT_NAME} mk_lib)
mk_demo/build/
mk_demo/mk_lib/lib.h
#ifndef LIB_H
#define LIB_H
int add(const int a, const int b);
int subtract(const int a, const int b);
int multiply(const int a, const int b);
int divide(const int a, const int b);
#endif
mk_demo/mk_lib/lib.cpp
#include "lib.h"
int add(const int a, const int b) {
return a + b;
}
int subtract(const int a, const int b) {
return a - b;
}
int multiply(const int a, const int b) {
return a * b;
}
int divide(const int a, const int b) {
return a / b;
}
mk_demo/mk_lib/CMakeLists.txt
cmake_minimum_required(VERSION 3.5.0)
project(mk_lib VERSION 0.1.0 LANGUAGES C CXX)
add_library(${PROJECT_NAME} SHARED lib.h lib.cpp)
install(TARGETS ${PROJECT_NAME} DESTINATION /usr/local/lib)
mk_demo/mk_lib/build/
5