例子4:链接静态库(.lib)

例子3里面我们构建了date.lib, 这个例子里我们调用这个库。

前提:

date.h的头文件在

E:\Playground\CMakeExamples\DateLib\include

date.lib或date.dll文件在

E:\Playground\CMakeExamples\lib

CMakeLists.txt里需要用到这两个地址。

文件结构:

E:.
│  CMakeLists.txt

├─build
└─src
        main.cpp

main.cpp:

// main.cpp
#include <iostream>
#include "date.h"

int main()
{
    Date date(2016,05,05);
    std::cout<<"Year:"<<date.getYear()<<" Month:"<<date.getMonth()
    <<" day:"<<date.getDay()<<std::endl;
}

 

CMakeLists.txt内容

cmake_minimum_required(VERSION 3.5.2)
project(usingLib_test)

set(userLibDir "E:/Playground/CMakeExamples/lib")

#For the static library:
set (PROJECT_LINK_LIBS date)
link_directories(${userLibDir})

#Bring the headers, such as Date.h into the project
set( dateLibInclude "E:/Playground/CMakeExamples/DateLib/include/")
include_directories(${dateLibInclude})

#The file(GLOB...) allows for wildcard additions:
file(GLOB SOURCES "src/*.cpp")
add_executable(testUsingLib ${SOURCES})
target_link_libraries(testUsingLib ${PROJECT_LINK_LIBS} )

 

 

Tips: 注意引用变量时需要用${}而非$().

include_directories命令的帮助:

include_directories
-------------------

Add include directories to the build.

::

include_directories([AFTER|BEFORE] [SYSTEM] dir1 [dir2 ...])

Add the given directories to those the compiler uses to search for
include files.  Relative paths are interpreted as relative to the
current source directory.

The include directories are added to the ``INCLUDE_DIRECTORIES``
directory property for the current ``CMakeLists`` file.  They are also
added to the ``INCLUDE_DIRECTORIES`` target property for each
target in the current ``CMakeLists`` file.  The target property values
are the ones used by the generators.

By default the directories specified are appended onto the current list of
directories.  This default behavior can be changed by setting
``CMAKE_INCLUDE_DIRECTORIES_BEFORE`` to ``ON``.  By using
``AFTER`` or ``BEFORE`` explicitly, you can select between appending and
prepending, independent of the default.

If the ``SYSTEM`` option is given, the compiler will be told the
directories are meant as system include directories on some platforms.
Signalling this setting might achieve effects such as the compiler
skipping warnings, or these fixed-install system files not being
considered in dependency calculations - see compiler docs.

Arguments to ``include_directories`` may use "generator expressions" with
the syntax "$<...>".  See the ``cmake-generator-expressions(7)``
manual for available expressions.  See the ``cmake-buildsystem(7)``
manual for more on defining buildsystem properties.

 

link_directories
----------------

Specify directories in which the linker will look for libraries.

::

link_directories(directory1 directory2 ...)

Specify the paths in which the linker should search for libraries.
The command will apply only to targets created after it is called.
Relative paths given to this command are interpreted as relative to
the current source directory, see ``CMP0015``.

Note that this command is rarely necessary.  Library locations
returned by ``find_package()`` and ``find_library()`` are
absolute paths. Pass these absolute library file paths directly to the
``target_link_libraries()`` command.  CMake will ensure the linker finds
them.

windows下CMake使用图文手册 Part 4的更多相关文章

  1. windows下CMake使用图文手册 Part 3

    例子3: 构建动态库(.dll) 静态库(.lib) 采用和例子2一样的文件,但删除了main.cpp E:.               │  CMakeLists.txt │            ...

  2. windows下CMake使用图文手册 Part 1

    维基百科介绍“CMake是个开源的跨平台自动化建构系统,它用配置文件控制建构过程(build process)的方式和Unix的Make相似,只是CMake的配置文件取名为CMakeLists.txt ...

  3. windows下CMake使用图文手册 Part 2

    例子2:有目录的项目 我现在有个文件夹ProjectDate,有如下文件结构 E:. │  CMakeLists.txt │ ├─include │      Date.h │ └─src       ...

  4. Windows下CMake编译安装OpenCV

    Windows下CMake编译安装OpenCV 这是一个面向新手的在windows上运进opencv, helloword的教程. 在这里我们使用vs2019来编译opencv, 并运行一个hello ...

  5. 在Windows下编译ffmpeg完全手册

    本文的内容几乎全部来自于FFmpeg on Windows,但是由于国内的网络封锁,很难访问这个域名下的内容,因此我一方面按照我自己的理解和实践做了翻译,另一方面也是为了能提供一个方便的参考方法. 注 ...

  6. [转]在Windows下编译ffmpeg完全手册

    本文的内容几乎全部来自于FFmpeg on Windows,但是由于国内的网络封锁,很难访问这个域名下的内容,因此我一方面按照我自己的理解和实践做了翻译,另一方面也是为了能提供一个方便的参考方法. 注 ...

  7. windows下安装Mysql—图文详解

    mysql安装过程及注意事项: 1.1. 下载: 我下载的是64位系统的zip包: 下载地址:https://dev.mysql.com/downloads/mysql/ 下载zip的包: 下载后解压 ...

  8. windows下安装Mysql(图文详解)

      博客园 | 首页 | 新随笔 | 联系 | 订阅 | 管理 mysql安装过程及注意事项: 1.1. 下载: 我下载的是64位系统的zip包: 下载地址:https://dev.mysql.com ...

  9. Windows下C++/Fortran调用.exe可执行文件

    目录 软件环境 Windows下CMake编译配置 设置项目的generator Command Line CMake GUI PreLoad.cmake 设置make 示例程序 CMake 设置Fo ...

随机推荐

  1. iOS中的extern与static

    1.extern #import <Foundation/Foundation.h> extern NSString *DBDefaultName; @interface DataBase ...

  2. python练手项目

    文本操作 逆转字符串--输入一个字符串,将其逆转并输出. 拉丁猪文字游戏--这是一个英语语言游戏.基本规则是将一个英语单词的第一个辅音音素的字母移动到词尾并且加上后缀-ay(譬如"banan ...

  3. Linux虚拟机安装(CentOS 6.5,图文详解,需要自查)

    Linux虚拟机的安装(图文详解) 下篇会接续Hadoop集群安装(以此为基础) 一.安装准备 VMWorkstation.linux系统镜像(以下以CentOS6.5为例) 二.安装过程详解 关闭防 ...

  4. 数据解析(XML和JSON数据结构)

    一   解析 二 XML数据结构 三 JSON 数据结构     一 解析 1  定义: 从事先规定好的格式中提取数据     解析的前提:提前约定好格式,数据提供方按照格式提供数据.数据获取方则按照 ...

  5. mysql 完整性约束

    mysql 完整性约束 数据的完整性概述根据完整性实施的方法将完整性约束分为四类:1.实体完整性 实体完整性的实现:通过在表中设置主键约束.唯一约束或标识列来实现 主键约束:应用于表列的一个约束 用法 ...

  6. Linux下搭建FTP服务器

    实习的公司有一台老服务器转作为FTP服务器,老大把这个任务交给了我.这两天边学边卖的捣腾起来,总算搞成.现在记录下来,加深映像,也以便以后查看复习. 服务器安装的是:Red Hat Enterpris ...

  7. 手机端touch事件 jquery模拟

    ontouchstart实现手机触屏中的hover效果 ontouchstart实现手机触屏中的hover效果 最近一直都在做手机网站的项目,会经常遇到手机屏幕触摸无交互效果,今天在一篇文章中看到模拟 ...

  8. PHPExcel 导出时乱码

    今天遇到了个奇怪的问题..猜测应该是因为php文件的编码造成的,但是没有解决办法. 问题是,用PHPEXCEL导出文件的时候,相同的代码在一个文件中可以导出,在另一个文件中却不行.. 最后没有办法,只 ...

  9. Spring源码学习之: 通过Spring @PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作

    关于在spring  容器初始化 bean 和销毁前所做的操作定义方式有三种: 第一种:通过@PostConstruct 和 @PreDestroy 方法 实现初始化和销毁bean之前进行的操作 第二 ...

  10. u-boot-2010.09移植(B)

    前面我们的u-boot只是在内存中运行,要想在nandflash中运行,以达到开机自启的目的,还需作如下修改 一.添加DM9000网卡支持 1.修改board/fl2440/fl2440.c中的boa ...