Ubuntu cmake 编译环境搭建及编译方法 (lesson 02)

多个文件、目录、动态库编译

  • 测试不同目录下的工程编译
    • 文件目录
    • 执行编译

测试不同目录下的工程编译

文件目录

先看工程文件的结构Demo02

love@ubuntu:~/workspace/CMakeProject/Demo02$ tree
.
├── CMakeLists.txt
├── main.c
└── Math
    ├── CMakeLists.txt
    ├── MathFunction.c
    └── MathFunction.h

1 directory, 5 files

参考文件*main.c

#include 
#include 
#include "Math/MathFunction.h"

/**
 * power - Calculate the power of number.
 * @param base: Base value.
 * @param exponent: Exponent value.
 *
 * @return base raised to the power exponent.
 */


int main(int argc, char *argv[])
{
    PrintInfo();
}


参考文件CMakeLists.txt

注意,集合了测试和调试的功能

# version required
cmake_minimum_required(VERSION 2.8)

# project name
project(Demo_02)

# serarch source file in current directory
# serarch result name save in DIR_SRCS param
# example 01:
aux_source_directory(. DIR_SRCS)


# target
add_executable(Demo_02 ${DIR_SRCS})

# add the Math sub-directory
add_subdirectory(Math)

# add the link library
target_link_libraries(Demo_02 MathFunction)


# test the cmake 
enable_testing()
add_test(test_run Demo_02)

# support GDB debug
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")

新建目录Math

在该目录下面有源文件,将其编译为动态库

//MathFunction.h
#include

void PrintInfo();

//MathFunction.c
#include "MathFunction.h"

void PrintInfo()
{
    printf("Hello Cmake file Test\n");
    printf("This is a library file\n");
}


CMakelists.txt

# This file is for Math directory
cmake_minimum_required(VERSION 2.8)

# The cmake need to be a static link library; lib*.so file
#project()  # current is not a project


# Search all the source file ,save in DIR_LIB_SRCS
aux_source_directory(. DIR_LIB_SRCS)

# build to *.a file 
#add_library(MathFunction STATIC ${DIR_LIB_SRCS})
add_library(MathFunction SHARED ${DIR_LIB_SRCS})
# assign the library and inclued file report position

执行编译

love@ubuntu:~/workspace/CMakeProject/Demo02$ cmake .
-- The C compiler identification is GNU 7.5.0
-- The CXX compiler identification is GNU 7.5.0
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/love/workspace/CMakeProject/Demo02

编译makefile

love@ubuntu:~/workspace/CMakeProject/Demo02$ make
Scanning dependencies of target MathFunction
[ 25%] Building C object Math/CMakeFiles/MathFunction.dir/MathFunction.c.o
[ 50%] Linking C shared library libMathFunction.so
[ 50%] Built target MathFunction
Scanning dependencies of target Demo_02
[ 75%] Building C object CMakeFiles/Demo_02.dir/main.c.o
[100%] Linking C executable Demo_02
[100%] Built target Demo_02
love@ubuntu:~/workspace/CMakeProject/Demo02$ 

查看目录分支结构,,可以看到编译好的动态库

love@ubuntu:~/workspace/CMakeProject/Demo02/Math$ tree 
.
├── CMakeFiles
│   ├── CMakeDirectoryInformation.cmake
│   ├── MathFunction.dir
│   │   ├── build.make
│   │   ├── C.includecache
│   │   ├── cmake_clean.cmake
│   │   ├── DependInfo.cmake
│   │   ├── depend.internal
│   │   ├── depend.make
│   │   ├── flags.make
│   │   ├── link.txt
│   │   ├── MathFunction.c.o
│   │   └── progress.make
│   └── progress.marks
├── cmake_install.cmake
├── CMakeLists.txt
├── libMathFunction.so
├── Makefile
├── MathFunction.c
└── MathFunction.h

2 directories, 18 files

执行测试

love@ubuntu:~/workspace/CMakeProject/Demo02$ ./Demo_02 
Hello Cmake file Test
This is a library file
love@ubuntu:~/workspace/CMakeProject/Demo02$ m test
m: command not found
love@ubuntu:~/workspace/CMakeProject/Demo02$ make test
Running tests...
Test project /home/love/workspace/CMakeProject/Demo02
    Start 1: test_run
1/1 Test #1: test_run .........................   Passed    0.01 sec

100% tests passed, 0 tests failed out of 1

Total Test time (real) =   0.01 sec
love@ubuntu:~/workspace/CMakeProject/Demo02$ 

你可能感兴趣的:(Linux,Shell,cmake,linux,makefile)