Cmake 01

Posted blogofer

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Cmake 01相关的知识,希望对你有一定的参考价值。

1. sdsf(single direction single file)

  1.1  The directory tree

/*
./template
    |
    +--- build
    |
    +---main.cpp
    |
    +---CMakeLists.txt
 */

  1.2 Sources code

 //  main.cpp
 
 #include <iostream>
 using namespace std;
 
 int main(int argc, char **argv) { 
     std::cout << "Hello, world!" << std::endl;
     return 0;
 }
 
 //CMakeLists.txt
 
 cmake_minimum_required(VERSION 2.6) //------The minimum version required---// 
 project(template) //------The project information-----//
 
 add_executable(template main.cpp) //-----Build Target------//
 
 install(TARGETS template RUNTIME DESTINATION bin)

  1.3 Compile All

1 cd build
2 cmake .. 
3 make

2. Single direct Multi- files

    2.1 The directory tree

/*
 ./template
    |
    +--- build/
    |
    +---main.cpp
    |
    +---a.cpp
    |
    +---CMakeLists.txt
*/

  2.2 Sources Code

  //  main.cpp
  
#include <iostream>
#include "a.h"
using namespace std;

int main(int argc, char **argv) {
   
    display();    
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

// a.cpp

#include "a.h"
using namespace std;

void display(void)
{
  cout << "I‘m in a.cpp"<< endl;
  
}

// a.h

#ifndef A_H_
#define A_H_

#include <iostream>
void display(void);
#endif


 //CMakeLists.txt
 
 cmake_minimum_required(VERSION 2.6) //------The minimum version required---// 
 project(template) //------The project information-----//
 
 add_executable(template main.cpp a.cpp) //-----Build Target------//
 
 install(TARGETS template RUNTIME DESTINATION bin)

 only add a.cpp in add_executable ,and there is a accepted way----used command  aux_source_directory 

// CMakeLists

cmake_minimum_required(VERSION 2.6)
project(template)
aux_source_directory(./  allfiles)   // The current dir
add_executable(template ${allfiles})

install(TARGETS template RUNTIME DESTINATION bin)

3. Multi dir Multi files

  3.1 dir tree

/*
 ./template
    |
    +--- build/
    |
    +---src/
          |
          +---b.cpp
          |
          +---CMakeLists
    |
    +---main.cpp
    |
    +---a.cpp a.h
    |
    +----b.h
    |
    +---CMakeLists.txt
*/

  3.2 Sources Code

// main.cpp

#include <iostream>
#include "a.h"
#include "b.h"

using namespace std;

int main(int argc, char **argv) {
   
    display(); 
    DisplayInFileb();
    std::cout << "Hello, world!" << std::endl;
    return 0;
}

//a.cpp

#include "a.h"
using namespace std;

void display(void)
{
  cout << "I‘m in a.cpp"<< endl;
  
}

// b.cpp

#include "b.h"
using namespace std;
void DisplayInFileb(void)
{
  cout << "I‘m in file b" << endl;
}

// CMakeLists  in src
aux_source_directory(./  AllfilesInSrc)
add_library(SrcFile ${AllfilesInSrc})

//CMakeLists in template
cmake_minimum_required(VERSION 2.6)
project(template)
aux_source_directory(./  allfiles)
add_subdirectory(src)
add_executable(template ${allfiles})
target_link_libraries(template SrcFile)

install(TARGETS template RUNTIME DESTINATION bin)

  3.3 compile all

1 cd build
2 cmake ..
3 make
4 
5 ./template
6 
7 I‘m in a.cpp
8 I‘m in file b
9 Hello, world!

 

以上是关于Cmake 01的主要内容,如果未能解决你的问题,请参考以下文章

19 01 11 javascript ?????????????????????(???????????????) ??????????????????????????????(代码片段

使用cmake编译非cmake项目

[linux][c/c++]代码片段01

从源代码 Cmake 为 Scientific Linux 7.8 构建

20190110-CMake-Makefile

20190110-CMake-Makefile