cmake笔记(一)

 用一个简单例子练习。

  1. 编译库testTPL并存储以便后面调用;
  2. 编译cmakeTest主程序,同时编译testLib库,cmakeTest主程序同时调用testLib库和之前写好的testTPL库。

文件结构如下:

.
├── cmakeTest
│   ├── CMakeLists.txt
│   ├── src
│   │   ├── cmakeTest.c
│   │   ├── testMain.c
│   │   └── testMain.h
│   └── testLib
│       ├── CMakeLists.txt
│       ├── testLib.c
│       └── testLib.h
├── cmakeTestInstall
├── testTPL
│   ├── CMakeLists.txt
│   ├── testTPL.c
│   └── testTPL.h
└── testTPLInstall

6 directories, 10 files

先进行第一步,编译库testTPL

testTPL.c如下:

#include "testTPL.h"
void testTPL()
{
    printf("Now in testTPL!\n");
}

testTPL.h如下:

#include "stdio.h"
void testTPL();

CMakeLists.txt如下:

cmake_minimum_required (VERSION 3.1)
project(testTPL)
set(LIB_SRC testTPL.c)
add_library(testTPL SHARED ${LIB_SRC})	#编译出一个库文件且命名为libtestTPL.so
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/lib)
install(FILES "testTPL.h" DESTINATION include)
install(TARGETS testTPL					#安装库libtestTPL.so
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        )

执行如下操作:

wjj@wjj:~/桌面/cmakeTest/testTPL$ mkdir build && cd build
wjj@wjj:~/桌面/cmakeTest/testTPL/build$ cmake -DCMAKE_INSTALL_PREFIX=/home/wjj/ 桌面/cmakeTest/testTPLInstall ..
-- 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/wjj/桌面/cmakeTest/testTPL/build
wjj@wjj:~/桌面/cmakeTest/testTPL/build$ make
Scanning dependencies of target testTPL
[ 50%] Building C object CMakeFiles/testTPL.dir/testTPL.c.o
[100%] Linking C shared library lib/libtestTPL.so
[100%] Built target testTPL
wjj@wjj:~/桌面/cmakeTest/testTPL/build$ make install
[100%] Built target testTPL
Install the project...
-- Install configuration: ""
-- Installing: /home/wjj/桌面/cmakeTest/testTPLInstall/include/testTPL.h
-- Installing: /home/wjj/桌面/cmakeTest/testTPLInstall/lib/libtestTPL.so

成功得到libtestTPL.so库

testTPLInstall
├── include
│   └── testTPL.h
└── lib
    └── libtestTPL.so

2 directories, 2 files

开始make主程序cmakeTest,src文件夹中存储主程序

cakeTest.c如下:

#include "testMain.h"
#include "testLib.h"
#include "testTPL.h"
int main()
{
    testMain();
    testLib();
    testTPL();
    return 0;
}

testMain.c如下:

#include "testMain.h"
void testMain()
{
    printf("Now in testMain!\n");
}

testMain.h如下:

#include "stdio.h"
void testMain();

testLib文件夹用来编译testLib库

testLib.c如下:

#include "testLib.h"
void testLib()
{
    printf("Now in testLib!\n");
}

testLib.h如下:

#include "stdio.h"
void testLib();

CMakeLists.txt如下:

project(testLib)
set(LIB_SRC testLib.c)
add_library(testLib SHARED ${LIB_SRC})	#编译出一个库文件且命名为libtestLib.so
set(LIBRARY_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../lib)
install(FILES "testLib.h" DESTINATION include)
install(TARGETS testLib					#安装库libtestLib.so
	    RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        )

根目录下的CMakeLists.txt如下:

cmake_minimum_required (VERSION 3.1)
project(cmakeTest)

add_subdirectory(testLib)				                #进入testLib中读取CMakeLists.txt
include_directories(${PROJECT_SOURCE_DIR}/testLib)	    #${PROJECT_SOURCE_DIR}为入口CMakeLists文件所在路径

set(TPL_PATH /home/wjj/桌面/cmakeTest/testTPLInstall)	#设置之前写的库的地址
find_library(LIBNAME testTPL ${TPL_PATH}/lib)		    #查找testTPL库:libtestTPL.so
include_directories(${TPL_PATH}/include)		        #包含头文件

set(APP_SRC src/cmakeTest.c src/testMain.c)		        #将cmakeTest.c和testMain.c设置为APP_SRC变量
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/bin)	#${PROJECT_BINARY_DIR}为build路径
add_executable(cmakeTest ${APP_SRC})			        #生成一个可执行文件cmakeTest
target_link_libraries(cmakeTest testLib ${LIBNAME})	    #指定程序依赖的lib文件,testLib库和testTPL库

set_target_properties(cmakeTest PROPERTIES INSTALL_RPATH_USE_LINK_PATH TRUE)	#将第三方库地址链接到cmakeTest上
#INSTALL_RPATH_USE_LINK_PATH是一个布尔值属性,如果它被设置为真,那么在链接器的搜索路径中以及工程之外的目录会被附加到INSTALL_RPATH之后。

install(FILES  "src/testMain.h" DESTINATION include)
install(TARGETS cmakeTest				#安装cmakeTest
	    RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib
        )

执行如下操作:

wjj@wjj:~/桌面/cmakeTest/cmakeTest$ mkdir build && cd build
wjj@wjj:~/桌面/cmakeTest/cmakeTest/build$ cmake -DCMAKE_INSTALL_PREFIX=/home/wjj/桌面/cmakeTest/cmakeTestInstall .. && make && make install
-- 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/wjj/桌面/cmakeTest/cmakeTest/build
Scanning dependencies of target testLib
[ 20%] Building C object testLib/CMakeFiles/testLib.dir/testLib.c.o
[ 40%] Linking C shared library ../lib/libtestLib.so
[ 40%] Built target testLib
Scanning dependencies of target cmakeTest
[ 60%] Building C object CMakeFiles/cmakeTest.dir/src/cmakeTest.c.o
[ 80%] Building C object CMakeFiles/cmakeTest.dir/src/testMain.c.o
[100%] Linking C executable bin/cmakeTest
[100%] Built target cmakeTest
[ 40%] Built target testLib
[100%] Built target cmakeTest
Install the project...
-- Install configuration: ""
-- Up-to-date: /home/wjj/桌面/cmakeTest/cmakeTestInstall/include/testMain.h
-- Installing: /home/wjj/桌面/cmakeTest/cmakeTestInstall/bin/cmakeTest
-- Set runtime path of "/home/wjj/桌面/cmakeTest/cmakeTestInstall/bin/cmakeTest" to "/home/wjj/桌面/cmakeTest/testTPLInstall/lib"
-- Up-to-date: /home/wjj/桌面/cmakeTest/cmakeTestInstall/include/testLib.h
-- Installing: /home/wjj/桌面/cmakeTest/cmakeTestInstall/lib/libtestLib.so

先安装了libtestLib.so库,主程序调用libtestLib.so库和之前安装的libtestTPL.so库,安装目录如下:

cmakeTestInstall
├── bin
│   └── cmakeTest
├── include
│   ├── testLib.h
│   └── testMain.h
└── lib
    └── libtestLib.so

3 directories, 4 files

运行主程序前,需要把bin与lib的路径放置环境变量:

wjj@wjj:~$ export PATH=/home/wjj/桌面/cmakeTest/cmakeTestInstall/bin:$PATH
wjj@wjj:~$ export LD_LIBRARY_PATH=/home/wjj/桌面/cmakeTest/cmakeTestInstall/lib:$LD_LIBRARY_PATH
wjj@wjj:~$ cmakeTest 
Now in testMain!
Now in testLib!
Now in testTPL!
wjj@wjj:~$ 

这里不需要添加第三方库地址,但需要添加同时安装的libtestLib.so库的地址,后面再加以改进。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值