24、CMakeLists.txt 使用静态库链接和.a文件的使用

基本思想:将在Android 调用c++ 代码模块,希望是用CMakeList.txt在本地完成静态模块的调用关系梳理,然后本地开发.a包,直接导入Android使用,作为前奏,从熟悉CMakeLists.txt使用静态库开始;

首先文件的组织关系

ubuntu@ubuntu:~/project$ tree
.
├── build
├── CMakeLists.txt
├── include
│   ├── function_1.h
│   └── function.h
├── main.cpp
└── src
    ├── CMakeLists.txt
    ├── function_1.cpp
    └── function.cpp

主文件中的CMakeLists.txt

ubuntu@ubuntu:~/project$ cat CMakeLists.txt 
project(test)
cmake_minimum_required(VERSION 2.8)

aux_source_directory(. DIR_SRCS)
message(${DIR_SRCS})

INCLUDE_DIRECTORIES(
include
)
add_subdirectory(src)


add_executable(${PROJECT_NAME} ${DIR_SRCS})
target_link_libraries(${PROJECT_NAME} static_lib )

main.cpp

ubuntu@ubuntu:~/project$ cat main.cpp 
#include<iostream>
using namespace std;
int fun(int a,int b);
int fun_1(int a,int b);
int main()
{
cout<<fun(2,3)<<endl;
cout<<fun_1(4,5)<<endl;
cout<<"hello world"<<endl;
return 0;
}

project/src 文件夹 CMakeLists.txt

ubuntu@ubuntu:~/project/src$ ls
CMakeLists.txt  function_1.cpp  function.cpp
ubuntu@ubuntu:~/project/src$ cat CMakeLists.txt 
aux_source_directory(. DIR_SUB_SRCS)

ADD_LIBRARY(static_lib STATIC ${DIR_SUB_SRCS}) 

project/src 文件夹 function.cpp

ubuntu@ubuntu:~/project/src$ cat function.cpp 

#include "function.h"


int fun(int a,int b)
{
return a+b;
}

project/src 文件夹 function_1.cpp

#include "function_1.h"


int fun_1(int a,int b)
{
return a+b;
}

project/include/function_1.h

ubuntu@ubuntu:~/project/include$ cat function_1.h 
#include<iostream>
int fun_1(int a,int b);

project/include/function.h

ubuntu@ubuntu:~/project/include$ cat function.h 
#include<iostream>
int fun(int a,int b);

进行编译

ubuntu@ubuntu:~/project/build$ cmake ..
-- The C compiler identification is GNU 5.4.0
-- The CXX compiler identification is GNU 5.4.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
./main.cpp
CMake Warning (dev) at CMakeLists.txt:13 (add_executable):
  Policy CMP0037 is not set: Target names should not be reserved and should
  match a validity pattern.  Run "cmake --help-policy CMP0037" for policy
  details.  Use the cmake_policy command to set the policy and suppress this
  warning.

  The target name "test" is reserved or not valid for certain CMake features,
  such as generator expressions, and may result in undefined behavior.
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /home/10163374L/project/build
ubutnu@ubuntu:~/project/build$ make
Scanning dependencies of target static_lib
[ 20%] Building CXX object src/CMakeFiles/static_lib.dir/function.cpp.o
[ 40%] Building CXX object src/CMakeFiles/static_lib.dir/function_1.cpp.o
[ 60%] Linking CXX static library libstatic_lib.a
[ 60%] Built target static_lib
Scanning dependencies of target test
[ 80%] Building CXX object CMakeFiles/test.dir/main.cpp.o
[100%] Linking CXX executable test
[100%] Built target test
ubuntu@ubuntu:~/project/build$  ./test 
5
9
hello world

主要看 CMakeLists.txt生成了一个静态文件

ubuntu@ubuntu:~/project/build$ cd src/
ubuntu@ubuntu:~/project/build/src$ ls
CMakeFiles  cmake_install.cmake  libstatic_lib.a  Makefile
ubuntu@ubuntu:~/project/build/src$ 

这个文件 就是我们需要后期移植到Android 上的静态包(当然 需要使用arm-linux-androideabi 编译才可以),这里主要探讨静态库的生成和使用

这个的静态库 我们也可以这样生成并且使用 ,首先使用命令行验证一下;

ubuntu@ubuntu:~/project/build/src$ cp libstatic_lib.a ../../
ubuntu@ubuntu:~/project/build/src$ cd ../../
ubuntu@ubuntu:~/project$ g++ main.cpp -o main.cpp -o main libstatic_lib.a 
ubuntu@ubuntu:~/project$ ./main 
5
9
hello world

打包多个静态库文件到一起使用也是同样的效果

ubuntu@ubuntu:~/project/src$ g++ -c ../include/function.h function.cpp 
function.cpp:2:22: fatal error: function.h: No such file or directory
compilation terminated.
ubuntu@ubuntu:~/project/src$ g++ -c function.h function.cpp
 ubuntu@ubuntu:~/project/src$ g++ -c function_1.h function_1.cpp 
ubuntu@ubuntu:~/project/src$ g++ -c ../include/function.h function.cpp 
ubuntu@ubuntu:~/project/src$ g++ -c ../include/function.h function.cpp 
function.cpp:2:22: fatal error: function.h: No such file or directory
compilation terminated.
ubuntu@ubuntu:~/project/src$ cp ../include/function* ./
ubuntu@ubuntu:~/project/src$ ls
CMakeLists.txt  function_1.h  function.h
function_1.cpp  function.cpp  function.h.gch
ubuntu@ubuntu:~/project/src$ g++ -c function.h function.cpp 
ubuntu@ubuntu:~/project/src$ g++ -c function_1.h function_1.cpp 
ubuntu@ubuntu:~/project/src$ ar -crv libstatic_lib.a function.o function_1.o
a - function.o
a - function_1.o
ubuntu@ubuntu:~/project/src$ cp libstatic_lib.a ../
ubuntu@ubuntu:~/project/src$ cd ..
ubuntu@ubuntu:~/project$ g++ main.cpp -o main libstatic_lib.a 
ubuntu@ubuntu:~/project$ ./main 
5
9
hello world
ubuntu@ubuntu:~/project$ 

其中步骤中存在的错误 主要原因是在各自的cpp中使用的#include“function.h” 在本文件中搜索其中函数定义,所以无法找到,拷贝到同一个文件即可以使用连编静态库;

ok  下一步PC端使用Android交叉编译器本地编译c++的静态库进行Android的c++ 开发

©️2020 CSDN 皮肤主题: 大白 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值