CMake 2.6 Notes: Difference between revisions

From KitwarePublic
Jump to navigationJump to search
(Add explicit preformat markup)
(Remove leading space rectangles from preformatted blocks)
Line 16: Line 16:


<pre>
<pre>
  add_library(mylib src1.c src2.c)
add_library(mylib src1.c src2.c)
  add_executable(myexe main1.c)
add_executable(myexe main1.c)
 
 
  set_property(
set_property(
    DIRECTORY
  DIRECTORY
    PROPERTY COMPILE_DEFINITIONS A AV=1
  PROPERTY COMPILE_DEFINITIONS A AV=1
    )
  )
  set_property(
set_property(
    TARGET mylib
  TARGET mylib
    PROPERTY COMPILE_DEFINITIONS B BV=2
  PROPERTY COMPILE_DEFINITIONS B BV=2
    )
  )
  set_property(
set_property(
    SOURCE src1.c
  SOURCE src1.c
    PROPERTY COMPILE_DEFINITIONS C CV=3
  PROPERTY COMPILE_DEFINITIONS C CV=3
    )
  )
</pre>
</pre>


Line 36: Line 36:


<pre>
<pre>
  src1.c:  -DA -DAV=1 -DB -DBV=2 -DC -DCV=3
src1.c:  -DA -DAV=1 -DB -DBV=2 -DC -DCV=3
  src2.c:  -DA -DAV=1 -DB -DBV=2
src2.c:  -DA -DAV=1 -DB -DBV=2
  main2.c:  -DA -DAV=1
main2.c:  -DA -DAV=1
</pre>
</pre>


Line 46: Line 46:


<pre>
<pre>
  set_property(
set_property(
    SOURCE src1.c
  SOURCE src1.c
    APPEND PROPERTY COMPILE_DEFINITIONS D DV=4
  APPEND PROPERTY COMPILE_DEFINITIONS D DV=4
    )
  )
</pre>
</pre>


Line 57: Line 57:


<pre>
<pre>
  set_property(
set_property(
    TARGET mylib
  TARGET mylib
    PROPERTY COMPILE_DEFINITIONS_DEBUG MYLIB_DEBUG_MODE
  PROPERTY COMPILE_DEFINITIONS_DEBUG MYLIB_DEBUG_MODE
    )
  )
</pre>
</pre>


Line 72: Line 72:


<pre>
<pre>
  /path/to/libfoo.a
/path/to/libfoo.a
  /path/to/libfoo.so
/path/to/libfoo.so
</pre>
</pre>


Line 79: Line 79:


<pre>
<pre>
  target_link_libraries(myexe /path/to/libfoo.a)
target_link_libraries(myexe /path/to/libfoo.a)
</pre>
</pre>


Line 85: Line 85:


<pre>
<pre>
  ... -L/path/to -Wl,-Bstatic -lfoo -Wl,-Bdynamic ...
... -L/path/to -Wl,-Bstatic -lfoo -Wl,-Bdynamic ...
</pre>
</pre>


Line 96: Line 96:


<pre>
<pre>
  ... /path/to/libfoo.a ...
... /path/to/libfoo.a ...
</pre>
</pre>


Line 106: Line 106:


<pre>
<pre>
  add_executable(myexe myexe.c)
add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/libA.so B)
target_link_libraries(myexe /path/to/libA.so B)
</pre>
</pre>


Line 117: Line 117:


<pre>
<pre>
  link_directories(/path/to)
link_directories(/path/to)
  add_executable(myexe myexe.c)
add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/libA.so B)
target_link_libraries(myexe /path/to/libA.so B)
</pre>
</pre>


Line 125: Line 125:


<pre>
<pre>
  add_executable(myexe myexe.c)
add_executable(myexe myexe.c)
  target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)
target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)
</pre>
</pre>


Line 139: Line 139:


<pre>
<pre>
  find_library(M_LIB m)
find_library(M_LIB m)
  target_link_libraries(myexe ${M_LIB})
target_link_libraries(myexe ${M_LIB})
</pre>
</pre>


Line 146: Line 146:


<pre>
<pre>
  /usr/lib/libm.so
/usr/lib/libm.so
</pre>
</pre>


Line 152: Line 152:


<pre>
<pre>
  /usr/lib/libm.so        (ELF o32)
/usr/lib/libm.so        (ELF o32)
  /usr/lib32/libm.so      (ELF n32)
/usr/lib32/libm.so      (ELF n32)
  /usr/lib64/libm.so      (ELF 64)
/usr/lib64/libm.so      (ELF 64)
</pre>
</pre>


Line 160: Line 160:


<pre>
<pre>
  /usr/lib/libm.so          (sparcv8 architecture)
/usr/lib/libm.so          (sparcv8 architecture)
  /usr/lib/sparcv9/libm.so  (sparcv9 architecture)
/usr/lib/sparcv9/libm.so  (sparcv9 architecture)
</pre>
</pre>


Line 167: Line 167:


<pre>
<pre>
  ... /usr/lib/libm.so ...
... /usr/lib/libm.so ...
</pre>
</pre>


Line 175: Line 175:


<pre>
<pre>
  ... -lm ...
... -lm ...
</pre>
</pre>


Line 181: Line 181:


<pre>
<pre>
  /usr/lib/libm.a
/usr/lib/libm.a
  /usr/lib/libm.so
/usr/lib/libm.so
</pre>
</pre>


Line 188: Line 188:


<pre>
<pre>
  ... -Wl,-Bstatic -lm ... -Wl,-Bshared ...
... -Wl,-Bstatic -lm ... -Wl,-Bshared ...
</pre>
</pre>


Line 204: Line 204:


<pre>
<pre>
  add_library(math STATIC IMPORTED)
add_library(math STATIC IMPORTED)
  set_property(TARGET math PROPERTY IMPORTED_LOCATION /usr/lib/libm.a)
set_property(TARGET math PROPERTY IMPORTED_LOCATION /usr/lib/libm.a)
  add_executable(foo foo.c)
add_executable(foo foo.c)
  target_link_libraries(foo math) # will link using full path
target_link_libraries(foo math) # will link using full path
</pre>
</pre>



Revision as of 18:33, 24 April 2018

This page documents some of the changes and new features available in CMake 2.6.

Exporting and Importing Targets

Please see our tutorial on Exporting and Importing Targets.

Packages

Please see our tutorial on Packaging.

Preprocessor Definitions

Preprocessor definitions may now be added to builds with much finer granularity than in previous versions of CMake. There is a new property called COMPILE_DEFINITIONS that is defined directories, targets, and source files. For example, the code

add_library(mylib src1.c src2.c)
add_executable(myexe main1.c)

set_property(
  DIRECTORY
  PROPERTY COMPILE_DEFINITIONS A AV=1
  )
set_property(
  TARGET mylib
  PROPERTY COMPILE_DEFINITIONS B BV=2
  )
set_property(
  SOURCE src1.c
  PROPERTY COMPILE_DEFINITIONS C CV=3
  )

will build the source files with these definitions:

src1.c:   -DA -DAV=1 -DB -DBV=2 -DC -DCV=3
src2.c:   -DA -DAV=1 -DB -DBV=2
main2.c:  -DA -DAV=1

When the add_definitions command is called with flags like "-DX" the definitions are extracted and added to the current directory's COMPILE_DEFINITIONS property. When a new subdirectory is created with add_subdirectory the current state of the directory-level property is used to initialize the same property in the subdirectory.

Note in the above example that the set_property command will actually set the property and replace any existing value. The command provides the APPEND option to help add more definitions without removing existing ones. For example, the code

set_property(
  SOURCE src1.c
  APPEND PROPERTY COMPILE_DEFINITIONS D DV=4
  )

will add the definitions "-DD -DDV=4" when building src1.c.

Definitions may also be added on a per-configuration basis using the COMPILE_DEFINITIONS_<CONFIG> property. For example, the code

set_property(
  TARGET mylib
  PROPERTY COMPILE_DEFINITIONS_DEBUG MYLIB_DEBUG_MODE
  )

will build sources in mylib with -DMYLIB_DEBUG_MODE only when compiling in a Debug configuration.

Link Line Generation

CMake 2.6 implements a new approach to generating link lines for targets.

Consider these libraries:

/path/to/libfoo.a
/path/to/libfoo.so

Previously if someone wrote

target_link_libraries(myexe /path/to/libfoo.a)

CMake would generate this code to link it:

... -L/path/to -Wl,-Bstatic -lfoo -Wl,-Bdynamic ...

This worked most of the time, but some platforms (such as OS X) do not support the -Bstatic or equivalent flag. This made it impossible to link to the static version of a library without creating a symlink in another directory and using that one instead.

Now CMake will generate this code:

... /path/to/libfoo.a ...

This guarantees that the correct library is chosen. However there are some side-effects that affect compatibility with existing projects (documented in the next two subsections).

Missing Linker Search Directories

Projects used to be able to write this (wrong) code and it would work by accident:

add_executable(myexe myexe.c)
target_link_libraries(myexe /path/to/libA.so B)

where "B" is meant to link "/path/to/libB.so". This code is incorrect because it asks CMake to link to B but does not provide the proper linker search path for it. It used to work by accident because the -L/path/to would get added as part of the implementation of linking to A. The correct code would be

link_directories(/path/to)
add_executable(myexe myexe.c)
target_link_libraries(myexe /path/to/libA.so B)

or even better

add_executable(myexe myexe.c)
target_link_libraries(myexe /path/to/libA.so /path/to/libB.so)

In order to support projects that have this bug, we've added a compatibility feature that adds the "-L/path/to" paths for all libraries linked with full paths even though the linker will not need those paths to find the main libraries. See policy CMP0003 for details.

Linking to System Libraries

System libraries on UNIX-like systems are typically provided in /usr/lib or /lib. These directories are considered implicit linker search paths because linkers automatically search these locations even without a flag like -L/usr/lib. Consider the code

find_library(M_LIB m)
target_link_libraries(myexe ${M_LIB})

Typically the find_library command would find the math library

/usr/lib/libm.so

Some platforms provide multiple versions of libraries corresponding to different architectures. For example, on an IRIX machine one might find the libraries

/usr/lib/libm.so         (ELF o32)
/usr/lib32/libm.so       (ELF n32)
/usr/lib64/libm.so       (ELF 64)

On a Solaris machine one might find

/usr/lib/libm.so          (sparcv8 architecture)
/usr/lib/sparcv9/libm.so  (sparcv9 architecture)

Unfortunately find_library may not know about all the architecture-specific system search paths used by the linker. In fact when it finds /usr/lib/libm.so it may be finding a library of incorrect architecture. If the link computation were to produce the line

... /usr/lib/libm.so ...

the linker might complain if /usr/lib/libm.so does not match the architecture it wants.

One solution to this problem is for the link computation to recognize that the library is in a system directory and ask the linker to search for the library. It could produce the link line

... -lm ...

and the linker would search through its architecture-specific implicit link directories to find the correct library. Unfortunately this solution suffers from the original problem of distinguishing between static and shared versions:

/usr/lib/libm.a
/usr/lib/libm.so

In order to ask the linker to find the static system library of the correct architecture it must produce the link line

... -Wl,-Bstatic -lm ... -Wl,-Bshared ...

This solution directly contradicts the original motivation to give the linker paths to libraries instead of -l options: not all platforms have an option like -Bstatic. Fortunately the platforms that do not provide such flags also tend to not have architecture-specific implicit link directories.

The solution used by CMake is:

  • Libraries not in implicit system locations are linked by passing the file path to the linker
  • Libraries in implicit system locations are linked by
    • passing the -l option if a flag like -Bstatic is available
    • passing the file path to the linker otherwise

Users can override this behavior by using the IMPORTED targets feature:

add_library(math STATIC IMPORTED)
set_property(TARGET math PROPERTY IMPORTED_LOCATION /usr/lib/libm.a)
add_executable(foo foo.c)
target_link_libraries(foo math) # will link using full path

CMake Policy Mechanism

CMake 2.6 introduces a new mechanism for backwards compatibility support. See CMake/Policies for more information.



CMake: [Welcome | Site Map]