mirror of
https://github.com/ttroy50/cmake-examples.git
synced 2025-12-18 20:24:35 +03:00
Update to minimum CMake 3.5
And modernise some examples.
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
cmake_minimum_required(VERSION 3.0)
|
||||
cmake_minimum_required(VERSION 3.5)
|
||||
|
||||
project(hello_library)
|
||||
|
||||
@@ -6,32 +6,28 @@ project(hello_library)
|
||||
# Create a library
|
||||
############################################################
|
||||
|
||||
# Source files to be used in the library
|
||||
set(library_SOURCES
|
||||
#Generate the shared library from the library sources
|
||||
add_library(hello_library SHARED
|
||||
src/Hello.cpp
|
||||
)
|
||||
|
||||
#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}/include
|
||||
PUBLIC
|
||||
${PROJECT_SOURCE_DIR}/include
|
||||
)
|
||||
|
||||
############################################################
|
||||
# Create an executable
|
||||
############################################################
|
||||
|
||||
# Source fles for the binary
|
||||
set(binary_SOURCES
|
||||
# Add an executable with the above sources
|
||||
add_executable(hello_binary
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
# Add an executable with the above sources
|
||||
add_executable(hello_binary ${binary_SOURCES})
|
||||
|
||||
# link the new hello_library target with the hello_binary target
|
||||
target_link_libraries( hello_binary
|
||||
PRIVATE hello::library
|
||||
PRIVATE
|
||||
hello::library
|
||||
)
|
||||
|
||||
@@ -17,14 +17,15 @@ $ tree
|
||||
.
|
||||
├── CMakeLists.txt
|
||||
├── include
|
||||
│ └── Hello.h
|
||||
│ └── shared
|
||||
│ └── Hello.h
|
||||
└── src
|
||||
├── Hello.cpp
|
||||
└── main.cpp
|
||||
```
|
||||
|
||||
* link:CMakeLists.txt[] - Contains the CMake commands you wish to run
|
||||
* link:include/Hello.h[] - The header file to include
|
||||
* link:include/shared/Hello.h[] - The header file to include
|
||||
* link:src/Hello.cpp[] - A source file to compile
|
||||
* link:src/main.cpp[] - The source file with main
|
||||
|
||||
@@ -39,15 +40,13 @@ This is called as follows:
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
set(library_SOURCES
|
||||
add_library(hello_library SHARED
|
||||
src/Hello.cpp
|
||||
)
|
||||
|
||||
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.
|
||||
the sources passed to teh +add_library()+ function.
|
||||
|
||||
## Alias Target
|
||||
|
||||
@@ -67,10 +66,13 @@ executable use the the +target_link_library()+ function to point to your library
|
||||
|
||||
[source,cmake]
|
||||
----
|
||||
add_executable(hello_binary ${binary_SOURCES})
|
||||
add_executable(hello_binary
|
||||
src/main.cpp
|
||||
)
|
||||
|
||||
target_link_libraries( hello_binary
|
||||
hello::library
|
||||
target_link_libraries(hello_binary
|
||||
PRIVATE
|
||||
hello::library
|
||||
)
|
||||
----
|
||||
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include <iostream>
|
||||
|
||||
#include "Hello.h"
|
||||
#include "shared/Hello.h"
|
||||
|
||||
void Hello::print()
|
||||
{
|
||||
std::cout << "Hello Shared Library!" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
#include "Hello.h"
|
||||
#include "shared/Hello.h"
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
Hello hi;
|
||||
hi.print();
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user