CMake实践(一) Linux下一个包含链接静态链接库的可执行程序的交叉编译

实践步骤如下:

1.Stich-lib工程的目录层次结构

D:\share\Stitch-lib\tree /f

│  CMakeLists.txt
│  GNUlinux_config.cmake
│  test_stitch.cpp
│                    
├─imgFmt
│      Bitmap.cpp
│      Bitmap.h
│      
├─include
│      commn.hpp
│      type.h
│      
└─stitch
        CMakeLists.txt
        Stitching.cpp
        Stitching.hpp

2.CMakeList

根目录下CMakeList.txt

cmake_minimum_required (VERSION 2.8)

project (test_stitch)

#添加可执行程序生成目录
set (EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)

#添加CXX编译选项,支持C++11/softfp/neon等
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -Wall -O3 -std=c++11 -mfloat-abi=softfp -mfpu=neon")

#添加头文件目录
include_directories("${PROJECT_SOURCE_DIR}/include")
include_directories("${PROJECT_SOURCE_DIR}/imgFmt")


#添加stitch子目录
include_directories ("${PROJECT_SOURCE_DIR}/stitch")
add_subdirectory(stitch)

#指定生成目标
add_executable(test_stitch imgFmt/Bitmap.cpp test_stitch.cpp)

#添加链接库
target_link_libraries(test_stitch stitch)

stitch目录下CMakeList.txt

#指定库文件生成路径
set (LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)

#查找当前目录下所有源文件
#将名称保存到变量DIR_LIB_SRCS中
aux_source_directory(. DIR_LIB_SRCS)

#生成链接库
add_library (stitch ${DIR_LIB_SRCS})

3.交叉编译配置相关

GNUlinux_config.cmake

set(GNULINUX_PLATFORM ON)
set(CMAKE_SYSTEM_NAME "Linux")
set(CMAKE_SYSTEM_PROCESSOR "arm")

#指定交叉编译工具
set (CMAKE_C_COMPILER arm-none-linux-gnueabi-gcc)
set (CMAKE_CXX_COMPILER arm-none-linux-gnueabi-g++)

4.编译执行

cmake不提供清理相关命令,一般采用创建build目录方式将编译过程与源文件分离

/stitch-lib$ mkdir build && cd build
/stitch-lib/build$ cmake -DCMAKE_TOOLCHAIN_FILE=../GNUlinux_config.cmake ..
-- The C compiler identification is GNU 4.8.3
-- The CXX compiler identification is GNU 4.8.3
-- Check for working C compiler: /opt/arm-2014.05/bin/arm-none-linux-gnueabi-gcc
-- Check for working C compiler: /opt/arm-2014.05/bin/arm-none-linux-gnueabi-gcc -- 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: /opt/arm-2014.05/bin/arm-none-linux-gnueabi-g++
-- Check for working CXX compiler: /opt/arm-2014.05/bin/arm-none-linux-gnueabi-g++ -- 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: /mnt/shared/stitch-lib/build
bruce@ubuntu16:/mnt/shared/stitch-lib/build$ make
[ 40%] Built target stitch
[100%] Built target test_stitch

5.结果

静态链接库与可执行程序分别在lib&bin目录生成

D:\share\Stitch-lib\build>tree /f
文件夹 PATH 列表
卷序列号为 7CA0-9ECA
D:.
│  CMakeCache.txt
│  cmake_install.cmake
│  Makefile
│
├─bin
│      test_stitch
│
├─CMakeFiles
│  │  cmake.check_cache
│  │  CMakeDirectoryInformation.cmake
│  │  CMakeOutput.log
│  │  feature_tests.bin
│  │  feature_tests.c
│  │  feature_tests.cxx
│  │  Makefile.cmake
│  │  Makefile2
│  │  progress.marks
│  │  TargetDirectories.txt
│  │
│  ├─3.5.1
│  │  │  CMakeCCompiler.cmake
│  │  │  CMakeCXXCompiler.cmake
│  │  │  CMakeDetermineCompilerABI_C.bin
│  │  │  CMakeDetermineCompilerABI_CXX.bin
│  │  │  CMakeSystem.cmake
│  │  │
│  │  ├─CompilerIdC
│  │  │      a.out
│  │  │      CMakeCCompilerId.c
│  │  │
│  │  └─CompilerIdCXX
│  │          a.out
│  │          CMakeCXXCompilerId.cpp
│  │
│  ├─CMakeTmp
│  └─test_stitch.dir
│      │  build.make
│      │  cmake_clean.cmake
│      │  CXX.includecache
│      │  depend.internal
│      │  depend.make
│      │  DependInfo.cmake
│      │  flags.make
│      │  link.txt
│      │  progress.make
│      │  test_stitch.cpp.o
│      │
│      └─imgFmt
│              Bitmap.cpp.o
│
├─lib
│      libstitch.a
│
└─stitch
    │  cmake_install.cmake
    │  Makefile
    │
    └─CMakeFiles
        │  CMakeDirectoryInformation.cmake
        │  progress.marks
        │
        └─stitch.dir
                build.make
                cmake_clean.cmake
                cmake_clean_target.cmake
                CXX.includecache
                depend.internal
                depend.make
                DependInfo.cmake
                flags.make
                link.txt
                progress.make
                Stitching.cpp.o

猜你喜欢

转载自blog.csdn.net/weixin_34417635/article/details/86852192