cmake入门系列总结二

cmake入门系列总结二


版本说明

版本作者日期备注
0.1loon2019.3.12初稿

目录

一、目的

现在我们将为我们的项目添加一个库。该库将由我们自己实现,用于打印一个hellWorld。然后,可执行程序可以使用此库而不是重新调用打印函数打印helloWorld。

二、添加库

在本教程中,我们将把库放入一个名为printHellWorld的子目录中。它将包含以下一行CMakeLists.txt文件:

add_library(printHellWorld printHelloWorld.cpp)

printHelloWorld.cpp如下:

#include <stdio.h>

void printHelloWorld()
{
    printf("HelloWorld-test to add library!\n");

    return;
}

源文件helloWorld.cpp有一个名为printHelloWorld的函数,它提供打印helloWorld的功能。要使用新库,我们在顶级CMakeLists.txt文件中添加add_subdirectory调用,以便构建库。我们还添加了另一个include目录,以便可以找到函数原型的printHellWorld/printHellWorld.h头文件。最后一个更改是将新库添加到可执行文件中。顶级CMakeLists.txt文件的最后几行现在看起来像:

cmake_minimum_required(VERSION 2.8)
project(helloWorld)

#指定头文件位置
include_directories(${PROJECT_SOURCE_DIR}/printHelloWorld)
#添加子目录
add_subdirectory(printHelloWorld)
 
#添加可执行文件
add_executable(helloWorld main.cpp)
#添加生成可执行文件链接的库
target_link_libraries(helloWorld printHelloWorld)

main.cpp如下:

#include "printHelloWorld.h"

int main()
{
    printHelloWorld();

    return 0;
}

目录结构如下:

zy@zy-virtual-machine:~/test/cmake_test/hello_world$ tree ./
./
├── CMakeLists.txt
├── main.cpp
└── printHelloWorld
    ├── CMakeLists.txt
    ├── printHelloWorld.cpp
    └── printHelloWorld.h

1 directory, 5 files

执行过程:

zy@zy-virtual-machine:~/test/cmake_test/hello_world/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
-- Configuring done
-- Generating done
-- Build files have been written to: /home/zy/test/cmake_test/hello_world/build
zy@zy-virtual-machine:~/test/cmake_test/hello_world/build$ make
Scanning dependencies of target printHelloWorld
[ 25%] Building CXX object printHelloWorld/CMakeFiles/printHelloWorld.dir/printHelloWorld.cpp.o
[ 50%] Linking CXX static library libprintHelloWorld.a
[ 50%] Built target printHelloWorld
Scanning dependencies of target helloWorld
[ 75%] Building CXX object CMakeFiles/helloWorld.dir/main.cpp.o
[100%] Linking CXX executable helloWorld
[100%] Built target helloWorld
zy@zy-virtual-machine:~/test/cmake_test/hello_world/build$ ls
CMakeCache.txt  CMakeFiles  cmake_install.cmake  helloWorld  Makefile  printHelloWorld
zy@zy-virtual-machine:~/test/cmake_test/hello_world/build$ ./helloWorld 
HelloWorld-test to add library!

三、让库可选

这部分暂时不要在非GUI系统上用,我在Ubuntu下使用时一直存在问题,暂时放弃。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

昵称系统有问题

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值