cmake

helloworld依赖lib1和lib2,主目录包括src

lib1包括hello.h和hello.cpp

lib2包括mymath.h和mymath.cpp

src下包括main.cpp和demo文件夹

demo包括demo.h和demo.cpp

如何组织makefile文件?

 

[xiaoju@vm-076 test_cmake]$ ls -lCRa helloworld/
helloworld/:
. .. CMakeLists.txt lib src

helloworld/lib:
. .. lib1 lib2

helloworld/lib/lib1:
. .. CMakeLists.txt src

helloworld/lib/lib1/src:
. .. hello.cpp hello.h

helloworld/lib/lib2:
. .. CMakeLists.txt src

helloworld/lib/lib2/src:
. .. mymath.cpp mymath.h

helloworld/src:
. .. demo main.cpp

helloworld/src/demo:
. .. demo.cpp demo.h

 

(1)对于lib1

[xiaoju@vm-076 lib]$ ls -lCRa lib1
lib1:
. .. CMakeLists.txt src

lib1/src:
. .. hello.cpp hello.h

编写CMakeLists.txt

 

CMakeLists.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
cmake_minimum_required (VERSION  2 . 6 )
project (lib1)
set (LIB_NAME  "hello_lib" )

file(GLOB_RECURSE LIB_SOURCES 
"src/*.cpp" )
file(GLOB_RECURSE LIB_HEADERS 
"src/*.h" )

set (ROOT $ {CMAKE_CURRENT_SOURCE_DIR} / .. / .. / )
set (CMAKE_CXX_FLAGS  "-g -O0 -pipe -W -Wall -fPIC" )
set (PROJECT_DIR $ {CMAKE_CURRENT_SOURCE_DIR})
message($ {PROJECT_DIR})

add_library($ {LIB_NAME} STATIC $ {LIB_SOURCES})
#set_target_properties(${LIB_NAME} PROPERTIES OUTPUT_NAME 
"xxx" )

# make install
install(TARGETS $ {LIB_NAME} DESTINATION $ {PROJECT_DIR} / output / lib)
install(
FILES  $ {LIB_HEADERS} DESTINATION $ {PROJECT_DIR} / output / include)

首先创建build目录,然后进入build目录,执行cmake ../即可在build下生产Makefile

然后执行make生产lib文件,

最后执行make install将include和lib文件拷贝至output目录下

 

 shell 
1
2
3
4
5
6
7
8
9
10
 
cd  lib1
mkdir  build
cd  build
cmake ../
make 
make install

remove tmp files
rm -rf build
rm -rf output

(2)对于lib2

 

 CMakeLists.txt 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
 
cmake_minimum_required (VERSION  2 . 6 )
project (lib2)
set (LIB_NAME  "mymath_lib" )

file(GLOB_RECURSE LIB_SOURCES 
"src/*.cpp" )
file(GLOB_RECURSE LIB_HEADERS 
"src/*.h" )

set (ROOT $ {CMAKE_CURRENT_SOURCE_DIR} / .. / .. / )
set (CMAKE_CXX_FLAGS  "-g -O0 -pipe -W -Wall -fPIC" )
set (PROJECT_DIR $ {CMAKE_CURRENT_SOURCE_DIR})
message($ {PROJECT_DIR})

add_library($ {LIB_NAME} STATIC $ {LIB_SOURCES})
#set_target_properties(${LIB_NAME} PROPERTIES OUTPUT_NAME 
"xxx" )

# make install
install(TARGETS $ {LIB_NAME} DESTINATION $ {PROJECT_DIR} / output / lib)
install(
FILES  $ {LIB_HEADERS} DESTINATION $ {PROJECT_DIR} / output / include)

首先创建build目录,然后进入build目录,执行cmake ../即可在build下生产Makefile

然后执行make生产lib文件,

最后执行make install将include和lib文件拷贝至output目录下

 

 shell
1
2
3
4
5
6
7
8
9
10
 
cd  lib1
mkdir  build
cd  build
cmake ../
make 
make install

remove tmp files
rm -rf build
rm -rf output

3)对于main.cpp

 

 main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 
#include  <iostream>
#include  <stdio.h>
// lib1
#include   "hello.h"
// lib2
#include   "mymath.h"

// src/demo
#include   "demo.h"
using   namespace  std;

int  main( int  argc,  char  *argv[])
{
    
const   char  *name =  "world" ;
    hello(name);
    
int  result = add( 4 5 );
    printf(
"%d\n" , result);

    printf(
"%d\n" , demo_square( 10 ));
    
return   0 ;
}

CMakeLists.txt------------------------------------------------------------------------

CMakeLists.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
 
cmake_minimum_required(VERSION  2 . 6 )
project(hello_proj)
set (EXE_NAME  "hello_exe" )

1 set  env
message('SYS ENV----------------------------------')
# CMAKE_CURRENT_SOURCE_DIR   where CMakeLists.txt exists?
# proj/
message(${CMAKE_CURRENT_SOURCE_DIR})
# proj/
message(${PROJECT_SOURCE_DIR})
# proj/build/
message(${PROJECT_BINARY_DIR})

set (ROOT ${CMAKE_CURRENT_SOURCE_DIR})
set (CMAKE_CXX_FLAGS  "-g -O0 -pipe -std=c++0x -W -Wall -fPIC" )
set (PROJECT_DIR  ${ROOT})
message('----------------------------------')

2 set  library modules include/lib
set (LIB1_MODULE ${ROOT}/lib/lib1)
set (LIB1_MODULE_INCLUDE ${LIB1_MODULE}/output/include)
file(GLOB_RECURSE LIB1_MODULE_LIB 
"${LIB1_MODULE}/output/lib/*.a" )

set (LIB2_MODULE ${ROOT}/lib/lib2)
set (LIB2_MODULE_INCLUDE ${LIB2_MODULE}/output/include)
file(GLOB_RECURSE LIB2_MODULE_LIB 
"${LIB2_MODULE}/output/lib/*.a" )

3 set  main entry 
file(GLOB_RECURSE MAIN_SOURCES 
"src/*.cpp" )

4 set  include header  path
include_directories(
        ${LIB1_MODULE_INCLUDE}
        ${LIB2_MODULE_INCLUDE}
        )

5 ) generate executable file
add_executable(${EXE_NAME} ${MAIN_SOURCES})

6 ) link libraries
target_link_libraries(${EXE_NAME}
        ${LIB1_MODULE_LIB}
        ${LIB2_MODULE_LIB}
        )

7 copy  executable file to bin  path
install(TARGETS ${EXE_NAME} DESTINATION 
"${PROJECT_DIR}/bin" )

首先创建build目录,然后进入build目录,执行cmake ../即可在build下生产Makefile

然后执行make生产exe文件,

最后执行make install将exe文件拷贝至bin目录下

最后进入bin目录执行exe文件

 shell
1
2
3
4
5
6
7
8
9
 
cd  helloworld
mkdir  build
cd  build
cmake ../
make 
make install

cd  helloworld/bin
./hello_exe

你可能感兴趣的:(cmake)