[Ceph] Use cmake to compile Ceph simple tutorial|Ceph Project

The optimization level of ceph compilation and the target of loading libraries at runtime are specified in link.txt. (Record library https://blog.csdn.net/bandaoyu/article/details/113181179 )

Makefile


Detailed Makefile rule tutorial: https://blog.csdn.net/liang13664759/article/details/1771246

Here is a very simple cmake entry blog: How to write CMakeList.txt  https://www.cnblogs.com/cv-pr/p/6206921.html
 

cmake

Cross-platform compilation management tool. The main function is actually to automatically generate Makefile according to the rules, and then use the make command to compile and link.

Using cmake requires the following steps:
1. Write CMakeList.txt file
2. Generate Makefile, mkdir build && cd build && cmake ../CMakeList.txt. Because cmake generates a lot of directory files, a build directory is usually created for placement.
3. Execute the make command to compile and link the project.

 

The directory result generated by cmake is roughly as follows:

├── build
│   ├── CMakeCache.txt
│   ├── CMakeFiles
│   │   ├── 3.2.2
│   │   │   ├── CMakeCCompiler.cmake
│   │   │   ├── CMakeCXXCompiler.cmake
│   │   │   ├── CMakeDetermineCompilerABI_C.bin
│   │   │   ├── CMakeDetermineCompilerABI_CXX.bin
│   │   │   ├── CMakeSystem.cmake
│   │   │   ├── CompilerIdC
│   │   │   │   ├── a.out
│   │   │   │   └── CMakeCCompilerId.c
│   │   │   └── CompilerIdCXX
│   │   │       ├── a.out
│   │   │       └── CMakeCXXCompilerId.cpp
│   │   ├── cmake.check_cache
│   │   ├── CMakeDirectoryInformation.cmake
│   │   ├── CMakeOutput.log
│   │   ├── CMakeTmp
│   │   ├── feature_tests.bin
│   │   ├── feature_tests.c
│   │   ├── feature_tests.cxx
│   │   ├── Makefile2
│   │   ├── Makefile.cmake
│   │   ├── progress.marks
│   │   ├── TargetDirectories.txt
│   │   └── test_sqrt.dir
│   │       ├── build.make
│   │       ├── C.includecache
│   │       ├── cmake_clean.cmake
│   │       ├── DependInfo.cmake
│   │       ├── depend.internal
│   │       ├── depend.make
│   │       ├── flags.make
│   │       ├── link.txt
│   │       ├── progress.make
│   │       └── src
│   │           ├── b.c.o
│   │           └── main.c.o
│   ├── cmake_install.cmake
│   ├── Makefile
│   └── test_sqrt
├── CMakeLists.txt
├── include
│   └── b.h
└── src
    ├── b.c
    └── main.c
 

 

Use cmake to compile ceph


When I was learning ceph before, I wanted to modify the code, such as adding some print statements, etc. This is okay, just re-make the corresponding module directly. For example, if you modify the relevant files of the rbd module, just execute make rbd. But if you want to add a file, module or tool, it is not easy to handle. Because there is no related file information in the Makefile, that is to say, even if make is executed, the newly added module will not be compiled.


But it's much easier to do since cmake. You can directly add the newly written module file in CMakeList.txt, and then make it. Because it is based on the entire ceph project, the header file include path can be imported as the project root directory according to ceph/src. At the same time, you don't need to care about the problem of global link dependency, just imitate the ceph's writing directly.

First explain the structure of ceph CMakeList.txt, as shown below. There is a total CMakeList.txt file in the project root directory, and then use the add_subdirectory statement to include the specified folder level by level, so as to find all the CMakeList configurations. Therefore, when we change the ceph source code, we only need to modify the CMakeList.txt under this module.

├── ceph-root
│ ├── build
│ │ ├── ...
│ ├── CMakeLists.txt
│ ├── src
│ │ ├── CMakeLists.txt
│ │ ├── tools
│ │ │ ├─ ─ CMakeLists.txt
│ │ ├── osd
│ │ │ ├── CMakeLists.txt
│ │ ├── mon
│ │ │ ├── CMakeLists.txt
│ │ │... For

example, we want to add a command line tool, In the src/tools/ directory, a new directory is created, um, called demo. This project does nothing, and uses boost-pragram_option to parse the command line, and then print the parameter value. The procedure is as follows:

#include <boost/program_options.hpp>
#include <iostream>

using namespace std;
namespace po = boost::program_options;

int main(int args, char** argv){

  try {

    po::options_description generic("Generic Options: ");
    generic.add_options()
      ("help", "help message")
      ("version,v", "print procedure version");

    int opt = 5;
    po::options_description ha("haha Options: ");
    ha.add_options()
      ("opt", po::value<int>(&opt)->default_value(10), "optimize value");

    po::options_description cmdline_options;
    cmdline_options.add(generic).add(ha);

    po::variables_map vm;
    po::store(po::parse_command_line(args, argv, cmdline_options), vm);
    po::notify(vm);

    if (vm.count("help")){
      cout << cmdline_options << endl;
      return 1;
    }
    cout << "vm opt = " << vm["opt"].as<int>() << endl;
    cout << "no opt = " << opt << endl;
  }
  catch(exception& e) {
  }
  catch(...) {
  }
  cout << "haha!" << endl;
  return 0;



Then under the demo/ directory, create a new CMakeList.txt file, just write a few words on the file:

set(demo test.cc)
add_executable(demo ${demo})
target_link_libraries(demo global
  ${BLKID_LIBRARIES} ${CMAKE_DL_LIBS})

Then mark the demo file in the CMakeList.txt in the upper directory.

...
add_subdirectory(demo)
...

Then, return to the root/build directory, execute cmake .., a Makefile will be regenerated, and then execute make demo, you can find the executable file build/bin/demo, and the execution will be Output:

@ ./bin/demo --help
Generic Options: :
  --help                 help message
  -v [ --version ]       print procedure version

haha Options: :
  --opt arg (=10)        optimize value


According to this example, you can reference libraries or functions such as librados, librbd, etc., and then you can draw a gourd and compile your own tool.

Original link: https://blog.csdn.net/hedongho/article/details/79993098

 

Add dynamic library dependencies

vi ceph-L/src/osd/CMakeList.txt

target_link_libraries(osd so_libname)
osd:可执行程序名
so_libname:共享库名

 

Guess you like

Origin blog.csdn.net/bandaoyu/article/details/114739976