CMake在ubuntu下的使用(4)

前面学习了很多cmake的使用,这节我们来学习在cmake中加载第三方库,我就写一些我用过的库。

加载boost库

源码路径:demo.

     ~~~~     创建一个新的目录,创建文件main.cpp,例子我是在网上找的:

#include <string>
#include <iostream>
#include <boost/version.hpp>
#include <boost/timer.hpp>
using namespace std;
int main()
{
    boost::timer t;
    cout << "max timespan: " << t.elapsed_max() / 3600 << "h" << endl;
    cout << "min timespan: " << t.elapsed_min() << "s" << endl;
    cout << "now time elapsed: " << t.elapsed() << "s" << endl;
    cout << "boost version" << BOOST_VERSION <<endl;
    cout << "boost lib version" << BOOST_LIB_VERSION <<endl;
    return 0;
}

     ~~~~     添加CMakeLists.txt文件:

project(myboost)

#添加头文件搜索路径
include_directories(/usr/local/include)

#添加库文件搜索路径
link_directories(/usr/local/lib)

#用于将当前目录下的所有源文件的名字保存在变量 DIR_SRCS 中
aux_source_directory(. DIR_SRCS)

add_executable(myboost ${DIR_SRCS})

#在这里根据名字boost_thread去寻找libboost_thread.a文件
target_link_libraries(myboost boost_thread boost_system)

     ~~~~     编译

qilimi@sharenew-desktop:~/test/cmake/10-test$ 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
CMake Warning (dev) in CMakeLists.txt:
  No cmake_minimum_required command is present.  A line of code such as

    cmake_minimum_required(VERSION 3.5)

  should be added at the top of the file.  The version specified may be lower
  if you wish to support older CMake versions for this project.  For more
  information run "cmake --help-policy CMP0000".
This warning is for project developers.  Use -Wno-dev to suppress it.

-- Configuring done
-- Generating done
-- Build files have been written to: /home/qilimi/test/cmake/10-test
qilimi@sharenew-desktop:~/test/cmake/10-test$ make
Scanning dependencies of target myboost
[ 50%] Building CXX object CMakeFiles/myboost.dir/main.o
[100%] Linking CXX executable myboost
[100%] Built target myboost
qilimi@sharenew-desktop:~/test/cmake/10-test$ ./myboost
max timespan: 2.56205e+09h
min timespan: 1e-06s
now time elapsed: 3.7e-05s
boost version106400
boost lib version1_64
qilimi@sharenew-desktop:~/test/cmake/10-test$

加载Thrift库

源码路径:demo.

     ~~~~     Thrift应该有不少人用过,它是一个软件框架,用来进行可扩展且跨语言的服务的开发。它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java, Go,Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml 这些编程语言间无缝结合的、高效的服务。
     ~~~~     创建一个新的目录,用来存放例子,进入,创建文件test.thrift,同时创建一个demo目录,等下用来存放源码:

  namespace cpp test.thrift
         service HelloWorldService {
          string sayHello(1:string username)
}

     ~~~~     输入指令生成thrift文件,需要先行安装好thrift:

qilimi@sharenew-desktop:~/test/cmake/11-test$ thrift -r --gen cpp test.thrift
qilimi@sharenew-desktop:~/test/cmake/11-test$ ls
demo  gen-cpp  test.thrift

     ~~~~     注意gen前面有两个-,这个时候会看到生成了一个新的目录gen-cpp,好的把目录的文件复制到demo里面,同时将HelloWorldService_server.skeleton.cpp文件改名为mian.cpp(个人习惯,可以不改)。
     ~~~~     然后进入demo目录,创建文件CMakeLists.txt文件:

cmake_minimum_required(VERSION 2.8)

project(myboost)

#这里很重要,必须加,C++11是thrift环境,DHAVE_NETINET_IN_H是一个宏
add_definitions(-std=c++11 -DHAVE_NETINET_IN_H)

#添加头文件搜索路径/usr/local/include/thrift /usr/local/include/
include_directories(/usr/local/include)

#添加库文件搜索路径
link_directories(/usr/local/lib)

#用于将当前目录下的所有源文件的名字保存在变量 DIR_SRCS 中
aux_source_directory(. DIR_SRCS)

add_executable(myboost ${DIR_SRCS})

#在这里根据名字查找文件,可以直接写thrift会去找thrift的库,或者写-lthrift
target_link_libraries(myboost -lthrift)

     ~~~~     创建目录build,之前我都是在同一个目录下面直接编译,但是这样会生成很多编译文件,我们可以专门创建一个目录放这些文件,进入build,输入cmake …/,可以在当前目录生成Makefile文件,然后直接make即可。

     ~~~~     就以这两个库做示例了,如果之后还有其他库有特别的问题,我会在下面添加。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值