mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
started updating examples for cmake v3 syntax
This commit is contained in:
@@ -1,11 +1,7 @@
|
||||
cmake_minimum_required(VERSION 2.6)
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
|
||||
project(hello_library)
|
||||
|
||||
include_directories(
|
||||
${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
############################################################
|
||||
# Create a library
|
||||
############################################################
|
||||
@@ -17,6 +13,11 @@ set(library_SOURCES
|
||||
|
||||
#Generate the shared library from the library sources
|
||||
add_library(hello_library SHARED ${library_SOURCES})
|
||||
add_library(hello::library ALIAS hello_library)
|
||||
|
||||
target_include_directories(hello_library
|
||||
PUBLIC ${PROJECT_SOURCE_DIR}/inc
|
||||
)
|
||||
|
||||
############################################################
|
||||
# Create an executable
|
||||
@@ -32,5 +33,5 @@ add_executable(hello_binary ${binary_SOURCES})
|
||||
|
||||
# link the new hello_library target with the hello_binary target
|
||||
target_link_libraries( hello_binary
|
||||
hello_library
|
||||
PRIVATE hello::library
|
||||
)
|
||||
@@ -6,7 +6,9 @@ toc::[]
|
||||
|
||||
# Introduction
|
||||
|
||||
Shows a hello world example which first creates and links a shared library
|
||||
Shows a hello world example which first creates and links a shared library.
|
||||
|
||||
This also shows how to create an link:https://cmake.org/cmake/help/v3.0/manual/cmake-buildsystem.7.html#alias-targets[alias target]
|
||||
|
||||
The files in this tutorial are below:
|
||||
|
||||
@@ -48,6 +50,17 @@ add_library(hello_library SHARED ${library_SOURCES})
|
||||
This will be used to create a shared library with the name libhello_library.so with
|
||||
the sources from the +library_SOURCES+ variable.
|
||||
|
||||
## Alias Target
|
||||
|
||||
As the name suggests an alias target is an alternative name for a target that can be used instead of the real target name in read-only contexts.
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
add_library(hello::library ALIAS hello_library)
|
||||
----
|
||||
|
||||
As show below, this allows you to reference the target using the alias name when linking it against other targets.
|
||||
|
||||
## Linking a Shared Library
|
||||
|
||||
Linking a shared library is the same as linking a static library. When creating your
|
||||
@@ -58,12 +71,11 @@ executable use the the +target_link_library()+ function to point to your library
|
||||
add_executable(hello_binary ${binary_SOURCES})
|
||||
|
||||
target_link_libraries( hello_binary
|
||||
hello_library
|
||||
hello::library
|
||||
)
|
||||
----
|
||||
|
||||
This tells CMake to link the hello_library against the hello_binary executable
|
||||
during link time.
|
||||
This tells CMake to link the hello_library against the hello_binary executable using the alias target name.
|
||||
|
||||
An example of this being called by the linker is
|
||||
|
||||
|
||||
Reference in New Issue
Block a user