导航

windows下CMake使用图文手册 Part 4

Posted on 2016-05-06 22:00  cuiocean  阅读(448)  评论(0编辑  收藏  举报

例子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.