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!

 

转载于:https://www.cnblogs.com/BlogOfEr/p/10149434.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值